如何在秋千上打开窗户时如何找到 [英] How do i find if a window is opened on swing

查看:142
本文介绍了如何在秋千上打开窗户时如何找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序出现问题,用户一次只能打开多个窗口。我已经添加了dispose()方法来关闭窗口时调用。现在我应该始终保持至少一个窗口打开,以便应用程序不会在未完全关闭的情况下隐藏。如果你不明白,请阅读以下场景:

I have a problem with my application where the user will open more than one window at a time. And i have added dispose() method to call on closing the window. Now i should keep at-least one window open all the time so that the application does not hides without closed fully. If you don't understand read the following scenario:

我有窗口A,窗口B同时打开。现在我可以关闭窗口A或窗口B,但不能同时关闭两者。换句话说,只有在窗口A打开时才允许窗口B关闭,反之亦然。我如何在摇摆中做到这一点??

I have window A and window B opened at the same time. Now i can close either window A or Window B but not both. In other words window B should be allowed to close only if window A is opened and vice versa. How do i do this in swing ??

推荐答案

一种简单的windowManger并不是很棘手,你所需要的只是

A simple kind-of windowManger is not really tricky, all you need is


  • WindowListener用于保存正在收听的Windows的跟踪

  • 创建窗口的已定义位置注册监听器

  • 使窗口无需关闭,并使监听器负责决定是否关闭(除了最后一个之外都将这样做)

一些片段:

    // 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;
    }

这篇关于如何在秋千上打开窗户时如何找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆