jprogressbar无法正常工作(可能是由于线程)

时间:2013-10-03 21:57:12

标签: java multithreading swing jprogressbar

我的代码如下

 public class panel extends JPanel{
 String sh;
 String su;
 String sp;
 int sp;
 final static int interval = 100;
  int i;
  Timer timer;
 public static JProgressBar pbar;

 public int getsport() {
       return this.sport;
    }
 public String getshost() {
       return this.shost;
    }
 public String getsuser() {
       return this.suser;
    }
 public String getspass() {
       return this.spass;
    }

public panel(){
    Dimension size = getPreferredSize();
    size.width = 300;//694;
    size.height = 200;//600;
    setPreferredSize(size);
    setBorder(BorderFactory.createTitledBorder("Linux Audit"));
    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();


    JLabel labelhost = new JLabel("Host    ");
    JLabel labeluser = new JLabel("User name    ");
    JLabel labelpass = new JLabel("Password    ");
    JLabel labelport = new JLabel("Pass   ");
    final JLabel lb = new JLabel();
    pbar = new JProgressBar(0, 100);
    pbar.setValue(0);
    pbar.setStringPainted(true);
    pbar.setVisible(false);
    final JTextField host = new JTextField(15);
    final JTextField user = new JTextField(15);
    final JTextField pass=(JTextField)new JPasswordField(15);
    final JTextField port = new JTextField(15);
    final JButton start = new JButton("Start ");
    //layout design
    gc.anchor = GridBagConstraints.LINE_END;
    gc.weightx = 0.5;
    gc.weighty = 0.5;
    gc.gridx=0;
    gc.gridy=0;
    add(labelhost,gc);
    gc.gridx=0;
    gc.gridy=1;
    add(labeluser,gc);
    gc.gridx=0;
    gc.gridy=2;
    add(labelpass,gc);
    gc.gridx=0;
    gc.gridy=3;
    add(labelport,gc);
    gc.anchor = GridBagConstraints.LINE_START;
    gc.gridx=1;
    gc.gridy=0;
    add(host,gc);
    gc.gridx=1;
    gc.gridy=1;
    add(user,gc);
    gc.gridx=1;
    gc.gridy=2;
    add(pass,gc);
    gc.gridx=1;
    gc.gridy=3;
    add(port,gc);
    gc.anchor = GridBagConstraints.FIRST_LINE_START;
    gc.weighty=10;
    gc.gridx=1;
    gc.gridy=4;
    add(start,gc);      
    gc.gridx=1;
    gc.gridy=5;
    add(pbar,gc);
    gc.gridx=1;
    gc.gridy=6;
    add(lb,gc);





start.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {

             ///mainFrame.paninst.setVisible(false);
            timer.restart();
            pbar.setVisible(true);
            pbar.setValue(0);



              String str = "<html>" + "<font color=\"#008000\">" + "<b>" + 
              "downloading in progress......." + "</b>" + "</font>" + "</html>";
                lb.setText(str);
                //connection
            String shost = host.getText();
            String suser = user.getText();
            String spass = pass.getText();
            String sportb = port.getText();
            int sport = Integer.parseInt(sportb);

            hConnection s = new hConnection(shost, suser, spass, sport);
            Thread thread = new Thread(s);
            thread.setDaemon(true);
            thread.start();

        }


    });
port.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){

        //mainFrame.paninst.setVisible(false);
        timer.restart();
        pbar.setValue(0);

        pbar.setVisible(true);



          String str = "<html>" + "<font color=\"#008000\">" + "<b>" + 
          "Downloading in progress......." + "</b>" + "</font>" + "</html>";
            lb.setText(str);
            //connection
        String shost = host.getText();
        String suser = user.getText();
        String spass = pass.getText();
        String sportb = port.getText();
        int sport = Integer.parseInt(sportb);

    hConnection s = new hConnection(shost, suser, spass, sport);
        Thread thread = new Thread(s);
        thread.setDaemon(true);
        thread.start();


    }});
//Create a timer.
  timer = new Timer(interval, new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
  if (i == 100){
  Toolkit.getDefaultToolkit().beep();
  timer.stop();
  pbar.setValue(0);
  String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + 
"download completed." + "</b>" + "</font>" + "</html>";
  lb.setText(str);
  }
  i = i + 1; //i++
  pbar.setValue(i);



  }
  });

}


}

你可以看到我正在使用新线程调用新类hconnection,因为在我的主类中我使用了以下代码

   public class download {
    public static void main(String[] arg){

SwingUtilities.invokeLater( new Runnable(){
    public void run(){
    JFrame frame = new mainFrame("download");
    frame.setVisible(true);
    frame.setSize(700,600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.setResizable(false);

    }
});

}
}

所以现在问题是当我点击jprogressbar它会第一次运行但当我再次点击它时它显示进度100而不是从0开始甚至没有达到它的下载完整代码第一次..我可以看到在同一个类中更改标签但是在创建新线程之后它会被停止..请帮助

1 个答案:

答案 0 :(得分:1)

您永远不会在运行之间重置i变量。这意味着在第二次运行中,i已经是100(更高)