1.关于变量放在本类的开头,或者放在构造方法里面出现问题?
主要是这个 pro2的初始化的问题JProgressBar pro2 = new JProgressBar();
想问一下为什么
package java23;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
public class Demo3 extends JFrame{
public JProgressBar pro2 ;
public static void main(String[] args) {
Demo3 demo3 = new Demo3();
demo3.setVisible(true);
}
public Demo3() {
super();
JPanel c = new JPanel();
c.setLayout(new GridLayout(3, 1, 10, 10));
setTitle("使用进度条");
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(c);
JProgressBar pro1 = new JProgressBar();
pro1.setIndeterminate(true);
pro1.setStringPainted(true);
pro1.setString("连接中。。。");
c.add(pro1);
JProgressBar pro2 = new JProgressBar();
pro2.setIndeterminate(false);
pro2.setStringPainted(true);
// pro2.setValue(10);
// pro2.setString("连接中。。。");
c.add(pro2);
MyThead myThead = new MyThead();
myThead.start();
}
class MyThead extends Thread{
@Override
public void run() {
for(int i = 0;i <= 100;i++) {
try {
pro2.setValue(i);
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}