[英]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 并不是很棘手,您所需要的只是
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.