在循环中显示JFrame弹出窗口的输出

时间:2018-04-15 09:29:33

标签: java loops jframe

我是初学程序员,这是我第一次尝试使用JFrame和其他东西,所以请随意批评任何违反良好编程习惯的行为。我尝试了很多东西,但没有一个真的有效,所以我被卡住了。

我正在尝试使用JFrame创建2个弹出窗口,其中一个请求按下按钮,然后打开另一个“模拟”标签(在这种情况下只是一串文本)系统移动的窗口。我把它放在一个循环中,并希望它每次都会更新文本,模拟它在窗口上移动。

但问题是它只是在循环的持续时间内显示一个空白屏幕,并且只有在它完成时才显示窗口上的文本。如何修复它以在窗口上每次打印文本,而不仅仅是在最后?我已经放了200毫秒的延迟,所以我可以发现文字在移动,但如果它甚至不打印文本就没用了。

这是我的代码:

public class PopUpWindow {

JButton button1, button2;
JLabel text1, text2;
JFrame window1, window2;

public PopUpWindow() {
    this.window1 = new JFrame("Input");
    this.window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.window1.setLayout(null);
    this.window2 = new JFrame("Simulation");
    this.window2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.window2.setLayout(null);
}

public void createWindow1() {
    this.button1 = new JButton("Start");
    this.button2 = new JButton("Quit");

    this.button1.setBounds(50, 50, 100, 30);
    this.button2.setBounds(180, 50, 100, 30);

    this.window1.add(button1);
    this.window1.add(button2);

    this.window1.setSize(350, 200);
    this.window1.setVisible(true);
    this.window1.setResizable(true);
    this.window1.setLocationRelativeTo(null);

    analyzeInput();
}

public void analyzeInput() {
    this.button1.addActionListener((ActionEvent e) -> {
        createWindow2();
    });

    this.button2.addActionListener((ActionEvent e) -> {
        int question = JOptionPane.showConfirmDialog(this.window1, "Exit program?");
        if (question == JOptionPane.YES_OPTION) {
            this.window1.dispose();
        }
    });
}

public void createWindow2() {
    this.button1 = new JButton("Button 1");
    this.button2 = new JButton("Button 2");
    this.text1 = new JLabel("x");
    this.text2 = new JLabel("x");
    int x = 80;
    int y = 0;

    for (int i = 0; i < 10; i++) {
        this.button1.setBounds(50, 120, 100, 30);
        this.button2.setBounds(180, 120, 100, 30);
        this.text1.setBounds(x, y, 100, 100);
        this.text2.setBounds(x, (y + 30), 100, 100);

        this.window2.add(text1);
        this.window2.add(text2);

        this.window2.setSize(350, 250);
        this.window2.setResizable(true);
        this.window2.setVisible(true);
        this.window2.setLocationRelativeTo(null);

        x += 5;
        y += 10;

        try {
            Thread.sleep(200);
        } catch (InterruptedException ex) {
            Logger.getLogger(PopUpWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

编辑: 好吧,让我们暂时忽略for循环。我主要想知道的是,尽管我改变了内容,但JFrame窗口不会更新或更改的原因。例如,这是createWindow2()-method的编辑版本,它没有for循环,而是对另一个按钮作出反应。

    public void createWindow2() {
        this.window2.setSize(500, 500);
        this.window2.setVisible(true);
        this.window2.setResizable(true);
        this.window2.setLocationRelativeTo(null);

        this.text1 = new JLabel("xfdfgdf");
        this.text2 = new JLabel("xdfgdfgd");
        this.button3 = new JButton("dfsadfas");
        this.button3.setBounds(50, 50, 100, 30);
        this.window2.add(this.button3);

        this.button3.addActionListener((ActionEvent e) -> {
            this.text1.setBounds(200, 200, 100, 100);
            this.text2.setBounds(200, 230, 100, 100);

            this.window2.add(text1);
            this.window2.add(text2);
        });
    }

当我运行程序时,即使按下按钮,我也只看到一个空白窗口。只有当我手动重新调整窗口时,文本才会显示在正确的坐标中。它应该像那样工作吗?有没有办法让它自己按下每个按钮更新? (我没有对Layouts或其他东西做过任何事情,所以我不知道它是如何工作的。)

0 个答案:

没有答案
相关问题