gpt4 book ai didi

java - 作为调用父框架的对话框启动应用程序

转载 作者:行者123 更新时间:2023-11-30 03:21:17 26 4
gpt4 key购买 nike

情况如下:

我的应用程序由一个包含 x 元素的对话框和一个按钮组成。用户在与元素交互后按下按钮,并且如果他以特定方式交互它们,则只有对话框所在的父框架才会出现。

为此目的,我目前知道这种方法:

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(false);
jDialog.setVisible(true);
}
});
}

然后在位于 jDialog 内的 Button 上添加此命令:

new NewJFrame().setVisible(true);

这个技巧非常好且简洁,但是前面的实例调用了 using new NewJFrame().setVisible(false);仍在运行(据我所知)。

无论如何我都可以在按钮(位于 jDialog 内部)按下时执行此操作,例如使用以下内容:

NewJFrame.setVisible(true);

(它目前给我错误:Non-static method cannot be referenced from static context)

最佳答案

确保对话框是模态的,您可以简单地执行以下操作:

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
NewJFrame newJFrame = new NewJFrame();
newJFrame.pack();
// no need to set visible false. It already is

MyDialog myDialog = new MyDialog(newJFrame);
// make sure the super constructor makes the dialog modal

myDialog.pack();
myDialog.setVisible(true);

// here the dialog is no longer visible
// and we can extract data from it and send it to the JFrame if needed

newJFrame.setVisible(true); // ****** here
}
});
}

否则,如果您绝对必须在 JDialog 中摆弄 JFrame,只需将 NewJFrame 传递到 JDialog 的构造函数中,无论如何您都需要这样做,因为它应该在 JDialog super 构造函数中使用,请使用它来设置 NewJFrame字段,然后在对话框内的实例上调用 setVisible(true)

关于java - 作为调用父框架的对话框启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31275249/

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