如果while循环为空,则Swing程序不会关闭,但是,如果while循环包含打印,则不会关闭

时间:2019-06-10 20:13:31

标签: java swing

在尝试使用swing创建类似于JOptionPane的小型对话框API时,遇到了一个非常奇怪的“错误”。

基本上,您调用一个打开JFrame的方法,等待选择一个按钮并返回它;等待直到选择了一个按钮,我使用了while循环:while (isVisible()) {}。现在更奇怪的是,如果循环为空(如此处所示),则程序将无法完成,但是如果我在循环中放入System.out.print(),则程序将无法完成。我试图在其中放置其他内容(例如int i = 0;),但没有任何区别。

编辑:即使没有打印和没有断点,使用调试器也可以解决此问题。

由于我不知道可能是什么问题,因此我将发布整个Dialog类:

package Frame.Dialogs;

import GameEngine.EngineMgr;

import javax.swing.*;
import java.awt.*;

public class Dialog extends JFrame {

public enum Buttons {
    OK(Dialog.OK),
    YES(Dialog.YES),
    RETURN(Dialog.RETURN),
    CONFIRM(Dialog.CONFIRM),
    YES_NO(Dialog.YES, Dialog.NO),
    YES_NO_CANCEL(Dialog.YES, Dialog.NO, Dialog.CANCEL),
    YES_CANCEL(Dialog.YES, Dialog.CANCEL),
    YES_RETURN(Dialog.YES, Dialog.RETURN),
    OK_CANCEL(Dialog.OK, Dialog.CANCEL),
    CONFIRM_CANCEL(Dialog.CONFIRM, Dialog.CANCEL),
    CONFIRM_RETURN(Dialog.CONFIRM, Dialog.RETURN);

    private JButton[] buttons;

    Buttons(JButton... buttons) {
        this.buttons = buttons;
    }

    JButton[] val() {
        return buttons;
    }
}

public static final JButton OK = new JButton("OK");
public static final JButton NO = new JButton("NO");
public static final JButton YES = new JButton("YES");
public static final JButton CANCEL = new JButton("CANCEL");
public static final JButton RETURN = new JButton("RETURN");
public static final JButton CONFIRM = new JButton("CONFIRM");

private static boolean initialized = false;

private JPanel button;
private JPanel content;
private JButton selection;

private Dialog(JPanel panel, String title, Buttons buttons) {
    if (!initialized)
        initialize();

    setTitle(title);
    setIconImage(null);

    button = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
    for (JButton b : buttons.val())
        button.add(b);

    content = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
    content.add(panel);
}

@SuppressWarnings("StatementWithEmptyBody")
private JButton display() {
    setLayout(new GridBagLayout());
    setBackground(Color.BLACK);

    GridBagConstraints c1 = new GridBagConstraints();
    c1.fill = GridBagConstraints.BOTH;
    c1.gridx = 0;
    c1.gridy = 0;
    c1.insets = new Insets(10, 10, 10, 10);
    GridBagConstraints c2 = (GridBagConstraints) c1.clone();
    c2.gridy = 1;

    add(content, c1);
    add(button, c2);
    pack();
    Dimension sc = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((sc.width - getWidth()) / 2, (sc.height - getHeight()) / 2);
    setVisible(true);

    //Strange loop

    //Will get stuck:
    //while (isVisible()) {}

    //Works fine:
    while (isVisible())
        System.out.print("");

    return selection;
}

private void initialize() {
    //JButton[] buttons = new JButton[]{YES, NO, OK, CANCEL, RETURN, CONFIRM};
    JButton[] buttons = new JButton[]{OK};
    for (JButton b : buttons) {
        b.setBorder(BorderFactory.createEmptyBorder());
        b.setContentAreaFilled(false);
        b.setIcon(new ImageIcon(EngineMgr.getGraphicEngine().get("bt_" + b.getText()).getScaledInstance(150, 84, Image.SCALE_SMOOTH)));
        b.setSelectedIcon(new ImageIcon(EngineMgr.getGraphicEngine().get("sBt_" + b.getText()).getScaledInstance(150, 84, Image.SCALE_SMOOTH)));
        b.setPressedIcon(new ImageIcon(EngineMgr.getGraphicEngine().get("pBt_" + b.getText()).getScaledInstance(150, 84, Image.SCALE_SMOOTH)));
        b.setText("");
        b.setSize(new Dimension(150, 50));
        b.addActionListener(e -> {
            selection = b;
            dispose();
        });
    }
}

public static JButton showDialog(String msg, String title, Buttons buttons) {
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
    panel.add(new JLabel(msg));
    return new Dialog(panel, title, buttons).display();
}

public static JButton showDialog(JPanel panel, String title, Buttons buttons) {
    return new Dialog(panel, title, buttons).display();
}

}

还有电话:

JButton selection = Dialog.showDialog("Test 1", "Test", Dialog.Buttons.OK);
if (selection == Dialog.OK)
    System.out.println("OK");
System.exit(0);

1 个答案:

答案 0 :(得分:1)

几乎可以肯定, <repositories> <repository> <id>confluent</id> <url>https://packages.confluent.io/maven/</url> </repository> </repositories> 循环正在AWT事件调度线程(EDT)之外运行。使用while进入正确的线程。

如果您确实将Swing / AWT与多个线程一起使用(EDT将是一个线程-因此甚至不要使用主线程),那么您将遇到竞争条件,并且会发生奇怪的事情。 java.awt.EventQueue.invokeLater即使在使用单线程硬件的情况下也会造成一些延迟,这通常会导致恶意行为发生变化。

在EDT上,阻止或运行繁忙的循环将导致UI冻结。因此,您将需要一个AWT / Swing事件处理程序,以将事件发送回您拥有的任何其他线程,或开始一个新任务。