windows 我如何确定窗户是否在秋千上打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5890649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i find if a window is opened on swing
提问by Deepak
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:
我的应用程序有问题,用户一次将打开多个窗口。我添加了 dispose() 方法来调用关闭窗口。现在我应该至少一直打开一个窗口,以便应用程序在没有完全关闭的情况下不会隐藏。如果您不明白,请阅读以下场景:
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 ??
我同时打开了窗口 A 和窗口 B。现在我可以关闭窗口 A 或窗口 B,但不能同时关闭。换句话说,只有在打开窗口 A 时才允许关闭窗口 B,反之亦然。我如何在摇摆中做到这一点?
回答by kleopatra
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
- a defined place to create the windows and register the the listener
- 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)
- WindowListener 跟踪它正在侦听的 Windows
- 一个定义的位置来创建窗口和注册监听器
- 让windows do-nothing-on-close 让监听器负责决定是否关闭(除了最后一个之外的所有窗口都会这样做)
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;
}
回答by gd1
Just a possible approach...
只是一种可能的方法......
Create a class, call it WindowManager
, that manages creation and disposal of windows.
创建一个类,称为WindowManager
,管理窗口的创建和处理。
It could for example retain the countof 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." or something like that.
例如,它可以保留当前打开的窗口的计数,并且仅当有多个窗口“活动”时才允许处理操作,否则显示一条确认消息,JOptionPane
告诉用户“真的关闭?那将终止应用程序。” 或类似的东西。
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 是否内置了这样的东西,我从未见过这样的场景。
回答by Stas Jaro
simply check if the other window is open before closing with window.isVisible();
只需在使用 window.isVisible() 关闭之前检查另一个窗口是否打开;