gpt4 book ai didi

java - 防止从退出按钮关闭 JDialog

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:26:40 32 4
gpt4 key购买 nike

我在 google 和 stackoverflow 上搜索了很多,它们都表明使用:

d.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

会阻止退出按钮关闭对话框,截至目前,它不是......下面贴出相关的代码片段,似乎有问题:

if (gameArea.hitChest()) {
JDialog d = new JDialog((JFrame) gameArea.getTopLevelAncestor(), "Dialogue", true);
ChestLoot ch = new ChestLoot(player);
d.add(ch);
d.setSize(200, 100);
d.setVisible(true);
d.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

d.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {
System.out.println("Don't Close!");
}

});

System.out.println("Should dispose here");
TileIcon ches = gameArea.getCurrChest();
gameArea.removeChest(ches);
}

最佳答案

您的 JDialog 是模式对话框,因此 setVisible(true) 之后的所有内容都不会影响它。将相关代码移到setVisible(true)之前:

if (gameArea.hitChest()) {
JDialog d = new JDialog((JFrame) gameArea.getTopLevelAncestor(), "Dialogue", true);
ChestLoot ch = new ChestLoot(player);
d.add(ch);
d.setSize(200, 100);

d.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

d.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {
System.out.println("Don't Close!");
}

});

d.setVisible(true); //code pauses here and waits for the dialog to be handled

System.out.println("Should dispose here");
TileIcon ches = gameArea.getCurrChest();
gameArea.removeChest(ches);
}

当您完成所有选项的设置后,只将对话框设置为可见是一个好习惯,即使是非模态对话框也是如此。

参见 JDialog ,它是这样说的:

dialog blocks user input to other top-level windows when shown

An Overview of Dialogs 中也提到了.

关于java - 防止从退出按钮关闭 JDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20358023/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com