简体   繁体   English

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

[英]jprogressbar not working properly( may be due to threading)

i have code as follows 我有如下代码

 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);



  }
  });

}


}

as you can see i am calling new class hconnection using new thread because in my main class i have used the following code 如您所见,我正在使用新线程调用新类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);

    }
});

}
}

so now the problem is when i click on jprogressbar it will run for the first time but when i click it again it is showing me the progress 100 and not starting from 0 and even not reaching the download complete code which it was for the first time..i can see changing of lable within the same class but after creation of new thread it get stopped..please help 所以现在的问题是,当我单击jprogressbar时,它将第一次运行,但是当我再次单击它时,它向我显示进度100,而不是从0开始,甚至没有达到第一次下载的完整代码..i可以在同一类中看到标签的更改,但是在创建新线程后它会停止..请帮助

You never reset the i variable between runs. 您永远不会在两次运行之间重置i变量。 This means on the second run, i is already 100 (greater) 这意味着在第二轮中, i已经是100(更大)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM