我有一个框架,当我在tester2框架上单击确定按钮时,应该看到tester1框架,并且当单击showbumber按钮时,应该在我的标签中显示一个随机数。JButton和JLabel不显示在JDialog上并且睡眠不起作用
但我看不到这个生成的数字,而我使用睡眠方法!
感谢您的帮助。
public class tester2 extends JFrame implements ActionListener {
public tester2() {
setTitle("Hello");
setLayout(new FlowLayout());
JButton okButton = new JButton("Ok");
okButton.addActionListener(this);
add(okButton);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(40, 50, 300, 400);
}
@Override
public void actionPerformed(ActionEvent e) {
tester1 tester1 = new tester1(tester2.this);
tester1.setVisible(true);
}
public static void main(String[] args) {
new tester2().setVisible(true);
}
}
测试仪1:
public class tester1 extends JDialog implements ActionListener {
JLabel lbl1;
JButton showButton;
public tester1(JFrame owner) {
super(owner, "tester1", true);
showButton = new JButton("Show Number");
showButton.addActionListener(this);
lbl1 = new JLabel(" ");
this.add(showButton);
this.add(lbl1);
this.setBounds(40, 50, 300, 400);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == showButton) {
GenerateNumber();
tester1.this.dispose();
}
}
public void GenerateNumber() {
Random rnd1 = new Random();
try {
Thread.sleep(1000);
lbl1.setText(String.valueOf(rnd1.nextInt(100)));
} catch (InterruptedException inrptdEx) {
}
}
}
您要处理你的框架,在标签设置文本之后。为什么?它不会让你看到你设定的文字。同时遵循[命名约定](http://www.oracle.com/technetwork/java/codeconv-138413.html),同时在java中进行编码。 – 2013-02-19 09:53:02
在EDT内睡觉将阻止Swing执行任何重绘。而不是使用Thread.sleep,请使用javax.swing.Timer – MadProgrammer 2013-02-19 10:00:52
看看我的帖子,看看我的答案 – 2013-02-19 10:30:18