我有一个 JFrame
并添加了一个 JPanel
。当我单击面板中的按钮时,会出现一个 JDialog
(名为 选择对话框
)出现。当我单击对话框上的特定按钮时,我只想将其关闭。
我希望关闭对话框并且框架可用。可能吗?
我尝试使用 setVisible(false)
隐藏对话框,但它隐藏了对话框和框架。然后我尝试执行 choiceDialog.dispose()
但我再次丢失了这两个元素。那时我找到了一种方法来将框架再次设置为可见但不可用。
有人可以帮我吗?我真的不知道该怎么办。
相关代码如下:
if (dimField.isEnabled()){
String dimFieldText = dimField.getText();
if (dimFieldText.equals("") || !isNumeric(dimFieldText)){ //if there's an error when filling the options in the JDialog
errorLabel = new JLabel(noDim, SwingConstants.CENTER);
/*other stuff
...
*/
}else{ //if it's all ok: I want the JDialog close but the JFrame to be usable
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); //to catch the JFrame istance
choiceDialog.dispose();
topFrame.setVisible(true); //to make the JFrame visible again
//choiceDialog.setVisible(false);
}
最佳答案
您将 topFrame 设置为按钮(即对话框本身)的窗口祖先。您需要获取对话框的窗口祖先。假设当您创建对话框时,您指定主框架作为其父级,而不是使用无参数 JDialog 构造函数。
关于java - 如何仅关闭 JDialog 并使 JFrame 仍然可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59759492/