确定打开了多少顶级容器

发布于 2024-11-14 20:06:49 字数 2829 浏览 2 评论 0原文

哪个方法可以返回,如何找到打开的顶级容器的数量

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SuperConstructor extends JFrame {

    private static final long serialVersionUID = 1L;

    public SuperConstructor() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(300, 300));
        setTitle("Super constructor");
        Container cp = getContentPane();
        JButton b = new JButton("Show dialog");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                FirstDialog firstDialog = new FirstDialog(SuperConstructor.this);
            }
        });
        cp.add(b, BorderLayout.SOUTH);
        pack();
        setVisible(true);
    }

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

            @Override
            public void run() {
                SuperConstructor superConstructor = new SuperConstructor();
            }
        });
    }

    private class FirstDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        FirstDialog(final Frame parent) {
            super(parent, "FirstDialog");
            setPreferredSize(new Dimension(200, 200));
            setLocationRelativeTo(parent);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
            JButton bNext = new JButton("Show next dialog");
            bNext.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    SecondDialog secondDialog = new SecondDialog(parent, false);
                }
            });
            add(bNext, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    }
    private int i;

    private class SecondDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        SecondDialog(final Frame parent, boolean modal) {
            //super(parent); // Makes this dialog unfocusable as long as FirstDialog is visible
            setPreferredSize(new Dimension(200, 200));
            setLocation(300, 50);
            setModal(modal);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setTitle("SecondDialog " + (i++));
            JButton bClose = new JButton("Close");
            bClose.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            });
            add(bClose, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    }
}

Which Method can returns, how can I find number of opened Top-Level Containers

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SuperConstructor extends JFrame {

    private static final long serialVersionUID = 1L;

    public SuperConstructor() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(300, 300));
        setTitle("Super constructor");
        Container cp = getContentPane();
        JButton b = new JButton("Show dialog");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                FirstDialog firstDialog = new FirstDialog(SuperConstructor.this);
            }
        });
        cp.add(b, BorderLayout.SOUTH);
        pack();
        setVisible(true);
    }

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

            @Override
            public void run() {
                SuperConstructor superConstructor = new SuperConstructor();
            }
        });
    }

    private class FirstDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        FirstDialog(final Frame parent) {
            super(parent, "FirstDialog");
            setPreferredSize(new Dimension(200, 200));
            setLocationRelativeTo(parent);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
            JButton bNext = new JButton("Show next dialog");
            bNext.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    SecondDialog secondDialog = new SecondDialog(parent, false);
                }
            });
            add(bNext, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    }
    private int i;

    private class SecondDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        SecondDialog(final Frame parent, boolean modal) {
            //super(parent); // Makes this dialog unfocusable as long as FirstDialog is visible
            setPreferredSize(new Dimension(200, 200));
            setLocation(300, 50);
            setModal(modal);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setTitle("SecondDialog " + (i++));
            JButton bClose = new JButton("Close");
            bClose.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            });
            add(bClose, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

回忆那么伤 2024-11-21 20:06:49

Window[] allWindows = Window.getWindows();

Window[] allWindows = Window.getWindows();
?

我不会写诗 2024-11-21 20:06:49

由于 J2EE 在非常具体的含义中使用“容器”一词,因此您最好在示例中说“有多少个顶级对话框”。请注意,在 Swing 中,“顶部”也具有非常特定的含义,因此只能有一个“顶部”项目,即(或将)绘制在所有其他项目之上的项目。由于答案“一”可能不是您需要的答案,我猜您的真正意思是“打开了多少个对话框?”

计算打开对话框数量的方法是向您的 SuperConstructor 类添加一个可以保存打开对话框“计数”的成员。在按钮的操作侦听器中,您创建一个新对话框。您可以将代码放在表示对话框的类中或创建对话框的操作侦听器中以增加计数。两种技术都可以,但如果您需要偏好,我更喜欢将其放入操作侦听器(嵌入 SuperConstructor 类中)。

如果您需要的计数不仅仅是打开的对话框的计数,则需要侦听对话框关闭事件并在对话框关闭时减少计数。

请注意,将对话框设置为可见与删除对话框不同,因此请小心查看可见性或存在性(取决于您所需的需求),但不要编写在存在时递增但在可见性时递减的代码(或反之亦然)。

Since J2EE uses the word "container" in a very specific meaning of the word, you are better off saying "how many top level dialogs" in your example. Note that in Swing "top" has a very specific meaning too, so there can only be one "top" item, the item that is (or would be) drawn on top of all other items. Since the answer "one" is likely not the answer you need, I'm going to guess you really meant "How many dialogs are opened?"

The way to count open dialogs is to add to your SuperConstructor class a member which can hold the "count" of opened dialogs. In your button's action listener, you create a new dialog. You can either put the code to increment the count in the class representing the dialog, or in the action listener that created the dialog. Either technique is fine, but if you need a preference, putting it in the action listener (embedded in the SuperConstructor class) is my preference.

If you need the count to be more than just a count of dialogs opened, you need to listen for dialog closing events and decrement the count as dialogs are closed.

Note that setting a dialog to be Visible is not the same as removing the dialog, so be careful to either look at visibility or existence (depending on your desired need) but don't write code which increments on existence but decrements on visibility (or vice versa).

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