我希望通过单击按钮在循环中显示几个延迟几秒的JFrame。框架来了,但它们很白,有标题,但它们没有身体(按钮不可见)。没有循环因此一旦调用JFrame,没问题。我该怎么办? 你还有其他想法吗?
主要课程:
public class Game3 {
game3.NewJFrame2 start_frame = new game3.NewJFrame2();
public Game3() throws InterruptedException {
this.start_frame.setSize(500,500);
start_frame.setVisible(true);
final JButton enter = new JButton("Enter");
enter.setBounds(10,10,50,50);
start_frame.add(enter);
enter.setVisible(true);
enter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
start_frame.dispose();
try {
new Play();
} catch (InterruptedException ex) {
Logger.getLogger(Game3.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public static void main(String[] args) throws InterruptedException {
new Game3();
}
}
和Play课程:
public class Play {
game3.NewJFrame2 start_frame1 = new game3.NewJFrame2();
public Play() throws InterruptedException {
this.select_rnd_word();
}
public static void select_rnd_word() throws InterruptedException {
for (int i = 0; i < 5; i++) {
game3.NewJFrame2 frame = new game3.NewJFrame2();
frame.setSize(200, 200);
JButton b = new JButton("A");
b.setBounds(0, 0, 30, 30);
frame.add(b);
b.setVisible(true);
frame.setVisible(true);
Thread.sleep(2000);
frame.dispose();
}
}
}
关注代码也存在这个问题:
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 3; i++) {
new Game3();
}
}
答案 0 :(得分:0)
一个想法是移动新的Play()&#39;离开ActionListener。
enter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
start_frame.dispose();
}
});
Thread.sleep(10000);
start_frame.dispose();
new Play();
答案 1 :(得分:0)
通过创建一个新线程,这个问题就解决了。
enter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
start_frame.dispose();
new Thread() {
@Override
public void run() {
try {
new Play();
} catch (InterruptedException ex) {
Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
}
}
}.start();
}
});
了解更多信息:Event Dispatch Thread