- 分享
- 0
- 人气
- 0
- 主题
- 3
- 帖子
- 410
- UID
- 59248
- 积分
- 472
- 阅读权限
- 16
- 注册时间
- 2007-1-17
- 最后登录
- 2024-1-23
- 在线时间
- 385 小时
|
当我trigger action listener 我的progressBar不能run~只有走完了才会show frame
test class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class test extends JFrame implements ActionListener{
progressBar bar = new progressBar();
JButton b;
public test(){
b = new JButton("test");
b.addActionListener(this);
/*without actionlistener is fine*/
//bar.showBar();
add(b);
}
public static void main (String args[]){
test demo = new test();
demo.setVisible(true);
demo.pack();
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if(cmd.equals("test")){
/*The progress bar does not show up, it show only till it finish*/
bar.showBar();
}
}
}
progressBar class :
import java.awt.*;
import javax.swing.*;
public class progressBar extends JFrame{
static int counter;
DownloadFile df = new DownloadFile();
JLabel downloadMsg, downloading;
static JProgressBar bar;
public progressBar(){
setUndecorated(true);
bar = new JProgressBar();
JPanel msgPanel = new JPanel();
JPanel barPanel = new JPanel();
barPanel.add(bar);
msgPanel.setLayout(new BoxLayout(msgPanel,BoxLayout.X_AXIS));
JPanel main = new JPanel(new BorderLayout());
msgPanel.setBackground(Color.BLACK);
barPanel.setBackground(Color.BLACK);
downloadMsg = new JLabel("Sydney.csv");
downloadMsg.setForeground(Color.WHITE);
downloading = new JLabel("Downloading : ");
downloading.setForeground(Color.WHITE);
msgPanel.setBorder(BorderFactory.createMatteBorder(16, 0, 0, 0, Color.black));
msgPanel.add(Box.createHorizontalStrut(10));
msgPanel.add(downloading);
msgPanel.add(downloadMsg);
main.add(msgPanel,BorderLayout.CENTER);
main.add(barPanel,BorderLayout.SOUTH);
main.setPreferredSize(new Dimension(300,50));
JSetting();
add(main);
}
private void JSetting(){
bar.setPreferredSize(new Dimension(280,10));
}
public void setProgressMax(int maxProgress)
{
bar.setMaximum(maxProgress);
}
public void setProgress(int progress)
{
final int theProgress = progress;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
bar.setValue(theProgress);
}
});
}
public void setProgress(String message, int progress)
{
final int theProgress = progress;
final String theMessage = message;
setProgress(progress);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
bar.setValue(theProgress);
setMessage(theMessage);
}
});
}
public void setScreenVisible(boolean b)
{
final boolean boo = b;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
bar.setVisible(boo);
}
});
}
private void setMessage(String message)
{
if (message==null)
{
message = "";
bar.setStringPainted(false);
}
else
{
bar.setStringPainted(true);
}
downloadMsg.setText(message);
}
private void resetBar(){
setProgress(0);
}
public void showBar(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
progressBar bar = new progressBar();
bar.setVisible(true);
bar.setAlwaysOnTop(true);
bar.isActive();
bar.pack();
for (int i = 0; i <= 100; i++)
{
for (long j=0; j<500000; ++j)
{
String poop = " " + (j + i);
}
bar.setProgress(i);
}
}
public static void main(String args[]){
progressBar bar = new progressBar();
bar.setVisible(true);
bar.pack();
for (int i = 0; i <= 100; i++)
{
for (long j=0; j<50000; ++j)
{
String poop = " " + (j + i);
}
bar.setProgress(i);
}
bar.resetBar();
}
}
是不是要用到invokelater 还是 multithreading阿? |
|