在Java中创建具有相同变量的对象的多个实例
我有一个非模式对话框,我需要显示它同时显示的多个实例。 我将对话框保留为我新建并显示对话框的类中的成员变量。这里有多个可见的对话框实例,但我将其分配给同一个成员变量。(我需要将其作为成员变量进行某些处理)。它工作正常,但我不明白为什么这样工作。我错过了一些非常明显的事情吗?
public class ABC {
CMyDialog m_dlg;
onSomeEvent() {
m_dlg = new CMyDialog();
}
}
onSomeEvent
被多次调用并显示多个对话框。知道 Java 如何管理这些事情吗?我是否需要保留 CMyDialog 数组作为成员变量,而不仅仅是一个类?
非常感谢任何帮助。
提前致谢。 尼廷·K.
I have a modeless dialog which i need to show multiple instances of it displayed at the same time.
I have kept the dialog as a member variable in a class which i new and show the dialog. Here there are multiple instances of dialog visible but i am assigning it to the same member variable.(I need to have it as member variable for some processing). It is working fine but i dont understand why this is working. Am i missig something very obvious?
public class ABC {
CMyDialog m_dlg;
onSomeEvent() {
m_dlg = new CMyDialog();
}
}
onSomeEvent
is called multiple times and multiple dialogs are shown. Any idea how Java manages these things? Do i need to keep an array of CMyDialog as member variable instead of just a single class?
Any help is highly appreciated.
Thanks in advance.
Nitin K.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认关闭操作 对于
JDialog
来说是HIDE_ON_CLOSE
。如果您不需要多个对话框,您可以只创建一个对话框并使其在onSomeEvent()
中可见。此示例使用切换按钮的itemStateChanged()
处理程序。The default close operation for
JDialog
isHIDE_ON_CLOSE
. If you don't want multiple dialogs, you can create just one and make it visibleonSomeEvent()
. This example uses a toggle button'sitemStateChanged()
handler.尽管有多个可见的对话框实例,但每个实例都在主内存中占据单独的空间。变量名可能相同,但所有对话框实例不共享相同的内存。我希望这就是您所要求的。
Although there are several instances of dialog visible, each of it occupies a separate space in main memory. The variable name may be same, but all the dialog instances do not share same memory. I hope this is what you had asked for.