在Java中创建具有相同变量的对象的多个实例

发布于 10-29 20:10 字数 412 浏览 5 评论 0原文

我有一个非模式对话框,我需要显示它同时显示的多个实例。 我将对话框保留为我新建并显示对话框的类中的成员变量。这里有多个可见的对话框实例,但我将其分配给同一个成员变量。(我需要将其作为成员变量进行某些处理)。它工作正常,但我不明白为什么这样工作。我错过了一些非常明显的事情吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴者2024-11-05 20:10:38

默认关闭操作 对于 JDialog 来说是 HIDE_ON_CLOSE。如果您不需要多个对话框,您可以只创建一个对话框并使其在 onSomeEvent() 中可见。此示例使用切换按钮的 itemStateChanged() 处理程序。

import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/5528408 */
public class DialogToggle extends JPanel {

    private static final String show = "Show Dialog";
    private static final String hide = "Hide Dialog";
    MyDialog myDialog = new MyDialog();

    public DialogToggle() {
        final JToggleButton b = new JToggleButton(show);
        b.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (b.isSelected()) {
                    myDialog.setVisible(true);
                    b.setText(hide);
                } else {
                    myDialog.setVisible(false);
                    b.setText(show);
                }
            }
        });
        this.add(b);
    }

    private class MyDialog extends JDialog {

        public MyDialog() {
            this.setLocationRelativeTo(DialogToggle.this);
            this.add(new JLabel("Hello, world!", JLabel.CENTER));
        }
    }

    private void display() {
        JFrame f = new JFrame("ABC");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DialogToggle().display();
            }
        });
    }
}

The default close operation for JDialog is HIDE_ON_CLOSE. If you don't want multiple dialogs, you can create just one and make it visible onSomeEvent(). This example uses a toggle button's itemStateChanged() handler.

import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/5528408 */
public class DialogToggle extends JPanel {

    private static final String show = "Show Dialog";
    private static final String hide = "Hide Dialog";
    MyDialog myDialog = new MyDialog();

    public DialogToggle() {
        final JToggleButton b = new JToggleButton(show);
        b.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (b.isSelected()) {
                    myDialog.setVisible(true);
                    b.setText(hide);
                } else {
                    myDialog.setVisible(false);
                    b.setText(show);
                }
            }
        });
        this.add(b);
    }

    private class MyDialog extends JDialog {

        public MyDialog() {
            this.setLocationRelativeTo(DialogToggle.this);
            this.add(new JLabel("Hello, world!", JLabel.CENTER));
        }
    }

    private void display() {
        JFrame f = new JFrame("ABC");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DialogToggle().display();
            }
        });
    }
}
柠檬心2024-11-05 20:10:38

尽管有多个可见的对话框实例,但每个实例都在主内存中占据单独的空间。变量名可能相同,但所有对话框实例不共享相同的内存。我希望这就是您所要求的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文