简体   繁体   English

我如何查找 swing 上是否打开了 window

[英]How do i find if a window is opened on swing

I have a problem with my application where the user will open more than one window at a time.我的应用程序存在问题,用户一次会打开多个 window。 And i have added dispose() method to call on closing the window.我添加了 dispose() 方法来调用关闭 window。 Now i should keep at-least one window open all the time so that the application does not hides without closed fully.现在我应该保持至少一个 window 一直打开,这样应用程序就不会在没有完全关闭的情况下隐藏。 If you don't understand read the following scenario:如果您不明白,请阅读以下场景:

I have window A and window B opened at the same time.我同时打开了 window A 和 window B。 Now i can close either window A or Window B but not both.现在我可以关闭 window A 或 Window B 但不能同时关闭两者。 In other words window B should be allowed to close only if window A is opened and vice versa.换句话说,window B 只有在 window A 打开时才允许关闭,反之亦然。 How do i do this in swing??我如何在 swing 中做到这一点?

A simple kind-of windowManger is not really tricky, all you need is一种简单的 windowManger 并不是很棘手,您所需要的只是

  • WindowListener which keeps tracks of the Windows it's listening to WindowListener 跟踪它正在收听的 Windows
  • a defined place to create the windows and register the the listener创建 windows 并注册监听器的定义位置
  • make the windows do-nothing-on-close and make the listener responsible for the decision of whether to close or not (will do so for all except the last)使 windows 在关闭时不执行任何操作,并让侦听器负责决定是否关闭(除了最后一个)

Some snippet:一些片段:

    // the listener (aka: WindowManager)
    WindowListener l = new WindowAdapter() {
        List<Window> windows = new ArrayList<Window>();

        @Override
        public void windowOpened(WindowEvent e) {
            windows.add(e.getWindow());
        }

        @Override
        public void windowClosing(WindowEvent e) {
            if (windows.size() > 1) {
                windows.remove(e.getWindow());
                e.getWindow().dispose();
            }
        }
    };
    // create the first frame
    JFrame frame = createFrame(l);
    frame.setVisible(true);


// a method to create a new window, config and add the listener
    int counter = 0;
    private JFrame createFrame(final WindowListener l) {
        Action action = new AbstractAction("open new frame: " + counter) {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame frame = createFrame(l);
                frame.setVisible(true);

            }
        };
        JFrame frame = new JFrame("someFrame " + counter++);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.add(new JButton(action));
        frame.addWindowListener(l);
        frame.pack();
        frame.setLocation(counter * 20, counter * 10);
        return frame;
    }

Just a possible approach...只是一种可能的方法...

Create a class, call it WindowManager , that manages creation and disposal of windows.创建一个 class,称为WindowManager ,它管理 windows 的创建和处置。

It could for example retain the count of the windows currently open, and allow a dispose operation only if there are more than one windows "alive", otherwise show a confirm message with JOptionPane telling the user "Really close? That would terminate the application."例如,它可以保留当前打开的 windows 的计数,并且仅当有多个 windows“活动”时才允许处理操作,否则会显示一条带有JOptionPane的确认消息,告诉用户“真的关闭?这将终止应用程序。 " or something like that.或类似的东西。

The "tricky" part is that you have to do this kind of window-related operations throughout the WindowManager , otherwise everything would screw up. “棘手”的部分是您必须在整个WindowManager中执行这种与窗口相关的操作,否则一切都会搞砸。

Dunno if Swing has something like this built-in, I've never seen such a scenario.不知道 Swing 是否内置了类似的东西,我从未见过这样的场景。

simply check if the other window is open before closing with window.isVisible();在使用 window.isVisible() 关闭之前,只需检查另一个 window 是否打开;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 摆动检查窗口是否打开 - Swing check if a window is opened 当我打开文件时,第一次打开文件但如果选择第二次未打开同一文件,如何在java swing中进行呢? - When i open file first time file is opened but if choose the second time same file not opened how to do it in java swing? 一次只打开一个秋千窗 - only one swing frame window opened at time 如何添加带有摆动的图像,如何使其尺寸达到窗口的大小? - How can i add an image with swing and how do i make it the size of my window? 我如何知道Vaadin中是否打开了窗口? - How could I know if the window is opened or not in Vaadin? 如何使 JList 与 netbeans Swing 应用程序中的窗口一起自动拉伸? - How do I make a JList automatically stretch with the window in a netbeans Swing app? 如何打印一个摆动窗口,使其非常适合一页 - How do I print a swing window so it fits nicely in one page Swing:我如何从AWT线程运行作业,但是在布置窗口之后? - Swing: How do I run a job from AWT thread, but after a window was layed out? 如何使元素根据JAVA swing中窗口的大小自动调整其大小? - How do I make the elements automatically adapt their sizes according to the size of window in JAVA swing? 如何在Swing中堆叠标签? - How do I stack labels in Swing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM