有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我是一名初级程序员,这是我第一次尝试JFrames之类的东西,所以请随意批评任何违反良好编程实践的行为。我尝试过很多东西,但都没有真正奏效,所以我被卡住了

我试图用JFrame创建两个弹出窗口,一个要求按下按钮,然后打开另一个“模拟”标签(在本例中只是一个文本字符串)系统地移动。我把它放在一个循环中,希望它每次都能更新文本,模拟它在窗口上移动

但问题是,它只是在循环期间显示一个空白屏幕,只有在循环完成后,才会在窗口上显示文本。我如何修复它,使其每次在窗口上打印文本,而不仅仅是在最后?我已经设置了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()方法的编辑版本,它没有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);
        });
    }

当我运行程序时,我只看到一个空白窗口,即使我按下了按钮。只有当我手动重新缩放窗口时,文本才会显示在正确的坐标中。它应该是这样工作的吗?有没有办法让它在每次按下按钮时自动更新?(我没有做过任何布局或东西,所以我不知道它是如何工作的。)


共 (0) 个答案