0
我有代碼如下的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因爲在我的主類我已經使用了下面的代碼
所以現在的問題是,當我的JProgressBar將首次運行的點擊,但是當我再次點擊它顯示我的進度100,而不是從0開始,甚至沒有達到下載完整的代碼這是第一次..我可以看到變化的機智欣同一班,但創建新的線程後,它停止..請幫助
謝謝你的回覆,但第一次也是100.i猜測和你的建議後,我已經改變它爲0,50但沒有運氣,仍然是問題是一樣的。 – user2829506
哦,得到了解決方案..我已經初始化i = 0;再次在按鈕現在工作正常....感謝您的答覆的方式.. – user2829506
請[接受](http://meta.stackexchange.com/a/65088/155831)答案,如果它有助於解決問題。 –