Java 从 JDialog 返回值;dispose(), setVisible(false) - 示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18330494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning value from JDialog; dispose(), setVisible(false) - example
提问by guitar_freak
I know, that this question appears quite frequently in SO like here: but I would like to present some very specific example... I'm simply not sure if I make things right.
我知道,这个问题在 SO 中经常出现,比如这里:但我想提出一些非常具体的例子......我只是不确定我是否做对了。
I've got a JDialog in which I can type some values, select some checkboxes... whatever... I've got also some Response object created in MyDialog which represents the MyDialog's "answer".
我有一个 JDialog,我可以在其中输入一些值,选择一些复选框......无论如何......我还在 MyDialog 中创建了一些 Response 对象,它代表 MyDialog 的“答案”。
In JFrame which calls/creates JDialog:
在调用/创建 JDialog 的 JFrame 中:
MyDialog d = new MyDialog(this, ...);
d.showDialog();
// After MyDialog is closed (it's modal):
MyDialog.Response dialogResponse = d.getDialogResponse();
// Do something with response...
In Dialog (dialog can be closed by clicking "Save" button):
在对话框中(可以通过单击“保存”按钮关闭对话框):
btnSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialogResponse = prepareResponse(); // prepares response on the basis of some data introduced by a user; dialogResponse is called from JFrame after Dialog is closed
setVisible(false);
dispose(); // <-- Important
}
});
My question is:
This solution works, I mean, the line MyDialog.Response dialogResponse = d.getDialogResponse();
returns proper values, but...
if I close the dialog using dispose(), all dialog's resources can be garbage collected (don't have to... hard to predict, am I right?). So is it correct to retrieve my dialog's response it that way... Maybe in this case I should write only setVisible(false);
without dispose()
.
我的问题是:这个解决方案有效,我的意思是,该行MyDialog.Response dialogResponse = d.getDialogResponse();
返回正确的值,但是...如果我使用 dispose() 关闭对话框,所有对话框的资源都可以被垃圾收集(不必...很难预测, 我对吗?)。那么以这种方式检索我的对话的响应是否正确......也许在这种情况下我应该只写 setVisible(false);
没有dispose()
.
采纳答案by MarioP
Quoted from the Javadocs:
引用自Javadocs:
The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed(not accounting for additional modifications between those actions).
通过随后调用 pack 或 show 重建本机资源,可以使 Window 及其子组件再次可显示。重新创建的 Window 及其子组件的状态将与这些对象在处理 Window 时的状态相同(不考虑这些操作之间的额外修改)。
So, your Response will be kept. All dispose()
does is releasing the native screen resources, other members aren't marked for garbage collection.
因此,您的 Response 将被保留。所有dispose()
没有被释放机屏幕资源,其他成员未标记为垃圾回收。
Also, if you want to be extra sure, you could just call dispose()
right after you retrieved your response object.
此外,如果您想更加确定,您可以dispose()
在检索响应对象后立即调用。
回答by mKorbel
if I close the dialog using dispose(), all dialog's resources can be garbage collected (don't have to... hard to predict, am I right?). So is it correct to retrieve my dialog's response it that way... Maybe in this case I should write only setVisible(false); without dispose().
如果我使用 dispose() 关闭对话框,则所有对话框的资源都可以被垃圾收集(不必......很难预测,对吗?)。那么以这种方式检索我的对话框的响应是否正确......也许在这种情况下我应该只写 setVisible(false); 没有处置()。
not true that something is GC'ed only non_important value for Graphics/2D
there isn't some reason to create this Object on runtime, re_use this Object
没有理由在运行时创建这个对象,重新使用这个对象
回答by GoAntonio
why you don't use class variables (private static or public static) and use a factory method
为什么不使用类变量(私有静态或公共静态)并使用工厂方法
//it can be an object too public static Object getResponseValue()
public static Integer getResponseValue(){
myclassContainer container = new myclassContainer(someparent,modal).setvisible(true)
return Myfieldvalue
}
private static int Myfielvalue;
}
回答by user5880243
dialog.add(BorderLayout.CENTER, tree_tmp);
JButton confirm = new JButton("YES");
confirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
confirm.setActionCommand("kkk");
dialog.setVisible(false);
}
});
dialog.add(BorderLayout.SOUTH,confirm);
dialog.setSize(400, 400);
dialog.setVisible(true);
System.out.println(confirm.getActionCommand());