通过单击按钮循环显示多个 JFrame

Display multiple JFrame in a loop by click the button

我想通过点击按钮循环显示几个延迟几秒的JFrame。框架来了,但它们很白,有标题但没有 body(按钮不可见)。没有循环因此一旦调用 JFrame,没问题。我应该怎么办? 你有别的想法吗?

主要class:

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

}

并播放 class:

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

一个想法是将 'new Play() ' 移出 ActionListener。

enter.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        start_frame.dispose();
    }
});
Thread.sleep(10000);
start_frame.dispose();
new Play();

通过创建一个新线程,这个问题就解决了。

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