我再弹出的窗口上点了一个按钮,然后窗口就死了,其他的按钮都没办法按了,还有就是我另外写了一个JProgressBar的类,代码如下,我把它封装再一个Panel里面,问题是我再其他Frame里创建他的时候,将其中的timer.start();然后,循环调用setPercentValue(),传入参数,可是进度条老是不动,怎么回事,好像整个窗口锁死了~~~请大家赐教,谢谢!!
public class ClientProgressPanel extends JPanel implements ActionListener,
ChangeListener { private JLabel progressLabel; private JProgressBar progress; public Timer timer; private int percentValue; public ClientProgressPanel() {
this.setLayout(new GridLayout(2, 1));
progress = new JProgressBar();
progress.setOrientation(JProgressBar.HORIZONTAL);
progress.setMinimum(0);
progress.setMaximum(100);
progress.setValue(0);
progress.setStringPainted(true);
progress.addChangeListener(this);
progress.setPreferredSize(new Dimension(150, 20));
progress.setBorderPainted(true);
progress.setBackground(Color.BLUE);
progressLabel = new JLabel(); timer = new Timer(50, this);
timer.start();
this.add(progress);
this.add(progressLabel); } public static void main(String[] args) {
JFrame jf = new JFrame();
ClientProgressPanel cpp = new ClientProgressPanel();
jf.add(cpp);
jf.pack();
jf.setVisible(true);
cpp.timer.start();
int a=0;
for (int i = 0; i < 101; i++) {
// if(i%20==0)
// {
// cpp.setPercentValue(++a);
// }
cpp.setPercentValue(i);
System.out.println(i);

} } public void setPercentValue(int percentValue) {
System.err.println(percentValue+"%");
this.percentValue = percentValue;
} public void actionPerformed(ActionEvent ae) {

if (ae.getSource() == timer) { if (percentValue <= 100) {
progress.setValue(percentValue);
} else {
timer.stop(); }

} } public void stateChanged(ChangeEvent ce) {
if (ce.getSource() == progress) {
progressLabel.setText("目前已完成进度:" + Integer.toString(percentValue)
+ " %");
progressLabel.setForeground(Color.BLUE);
if (percentValue >= 100) { }
}
}}

解决方案 »

  1.   

    你的程序貌似没问题啊 还有start没必要启动两次
    看看这样是不是会更明显一些import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.Timer;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;public class Test extends JPanel implements ActionListener,
            ChangeListener {    private JLabel progressLabel;    private JProgressBar progress;    public Timer timer;    private int percentValue;    public Test() {
            this.setLayout(new GridLayout(2, 1));
            progress = new JProgressBar();
            progress.setOrientation(JProgressBar.HORIZONTAL);
            progress.setMinimum(0);
            progress.setMaximum(100);
            progress.setValue(0);
            progress.setStringPainted(true);
            progress.addChangeListener(this);
            progress.setPreferredSize(new Dimension(150, 20));
            progress.setBorderPainted(true);
            progress.setBackground(Color.BLUE);
            progressLabel = new JLabel();        timer = new Timer(50, this);
            //timer.start();
            this.add(progress);
            this.add(progressLabel);    }    public static void main(String[] args) {
            JFrame jf = new JFrame();
            Test cpp = new Test();
            jf.add(cpp);
            jf.pack();
            jf.setVisible(true);
            cpp.timer.start();
            for (int i = 0; i < 101; i++) {
             try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
                cpp.setPercentValue(i);
            }    }    public void setPercentValue(int percentValue) {
            System.err.println(percentValue+"%");
            this.percentValue = percentValue;
        }    public void actionPerformed(ActionEvent ae) {
            
                if (ae.getSource() == timer) {                if (percentValue <= 100) {
                        progress.setValue(percentValue);
                    } else {
                        timer.stop();                }
                
            }    }    public void stateChanged(ChangeEvent ce) {
            if (ce.getSource() == progress) {
                progressLabel.setText("目前已完成进度:" + Integer.toString(percentValue)
                        + " %");
                progressLabel.setForeground(Color.BLUE);
                if (percentValue >= 100) {            }
            }
        }}
      

  2.   

    是啊,我点下按钮以后,文件就开始接收了,接收文件的时候是用while开始循环了,该如何解决了?