下面的示例允许显示,隐藏,处置和关闭窗口(通过发送事件)。
示例显示,该窗口仅打开一次 - 在第一组可见时。即使窗口被丢弃,然后再次显示,它也不会经历“开放”事件。
为什么?
如何在不编写处理程序并跟踪此事件的情况下知道窗口是否已打开?
如何关闭窗口以便在设置可见时再次进行打开事件?即如何将窗口对象返回初始状态?
是否还有其他可以拥有所需属性的州或事件?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.ListDataListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Tester_JFrame_Closing_01 {
private static Logger log = LoggerFactory.getLogger(Tester_JFrame_Closing_01.class);
protected static Toolkit toolkit = Toolkit.getDefaultToolkit();
protected static EventQueue eventQueue = toolkit.getSystemEventQueue();
public static enum CloseOperation {
DO_NOTHING_ON_CLOSE(JFrame.DO_NOTHING_ON_CLOSE),
HIDE_ON_CLOSE(JFrame.HIDE_ON_CLOSE),
DISPOSE_ON_CLOSE(JFrame.DISPOSE_ON_CLOSE),
EXIT_ON_CLOSE(JFrame.EXIT_ON_CLOSE)
;
public static ComboBoxModel newModel() {
return new ComboBoxModel() {
private CloseOperation selected = HIDE_ON_CLOSE;
@SuppressWarnings("serial")
private final AbstractListModel core = new AbstractListModel() {
@Override
public int getSize() {
return values().length;
}
@Override
public Object getElementAt(int index) {
return values()[index];
}
};
@Override
public int getSize() {
return core.getSize();
}
@Override
public Object getElementAt(int index) {
return core.getElementAt(index);
}
@Override
public void addListDataListener(ListDataListener l) {
core.addListDataListener(l);
}
@Override
public void removeListDataListener(ListDataListener l) {
core.removeListDataListener(l);
}
@Override
public void setSelectedItem(Object anItem) {
selected = (CloseOperation) anItem;
}
@Override
public Object getSelectedItem() {
return selected;
}
};
}
public final int Value;
private CloseOperation(int value) {
this.Value = value;
}
@Override
public String toString() {
switch(this) {
case DISPOSE_ON_CLOSE:
return "DISPOSE_ON_CLOSE";
case HIDE_ON_CLOSE:
return "HIDE_ON_CLOSE";
case DO_NOTHING_ON_CLOSE:
return "DO_NOTHING_ON_CLOSE";
case EXIT_ON_CLOSE:
return "EXIT_ON_CLOSE";
default:
return "<UNKNOWN>";
}
}
}
public static void main(String[] args) {
WindowAdapter windowAdapter = new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
log.debug("windowClosed");
}
@Override
public void windowClosing(WindowEvent e) {
log.debug("windowClosing");
}
@Override
public void windowOpened(WindowEvent e) {
log.debug("windowOpened");
}
};
final JFrame frame = new JFrame("Controlled window");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.addWindowListener(windowAdapter);
final WindowEvent closeEvent = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
@SuppressWarnings("serial")
AbstractAction closeAction = new AbstractAction("close") {
@Override
public void actionPerformed(ActionEvent e) {
eventQueue.postEvent(closeEvent);
}
};
@SuppressWarnings("serial")
AbstractAction hideAction = new AbstractAction("hide") {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
};
@SuppressWarnings("serial")
AbstractAction showAction = new AbstractAction("show") {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(true);
}
};
@SuppressWarnings("serial")
AbstractAction disposeAction = new AbstractAction("dispose") {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
};
JButton closeButton = new JButton(closeAction);
JButton hideButton = new JButton(hideAction);
JButton showButton = new JButton(showAction);
JButton disposeButton = new JButton(disposeAction);
ComboBoxModel closeOperationModel = CloseOperation.newModel();
final JComboBox closeOperationCombo = new JComboBox(closeOperationModel);
closeOperationCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
log.debug("closeOperationCombo.actionPerformed({})", e);
frame.setDefaultCloseOperation(((CloseOperation)closeOperationCombo.getSelectedItem()).Value);
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(showButton);
buttonPanel.add(hideButton);
buttonPanel.add(disposeButton);
buttonPanel.add(closeButton);
buttonPanel.add(closeOperationCombo);
JFrame controlFrame = new JFrame("Controller window");
controlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
controlFrame.setLayout(new FlowLayout());
controlFrame.add(buttonPanel, BorderLayout.SOUTH);
controlFrame.pack();
controlFrame.setLocation(100, 100);
controlFrame.setVisible(true);
frame.setBounds(100, 100 + controlFrame.getHeight(), controlFrame.getWidth(), 480);
}
}
INCREDIBLE
他们有一个名为Window.state
的字段,他们甚至没有初始化(因此它为零),然后他们在show()
方法中测试一次并将其设置为1
它是0
。这是唯一使用state
的地方,此处windowOpened
被触发。没有代码可以将state
返回0
任何地方。此外,他们放弃了state
子类中的Frame
变量,并使用具有相同名称的新变量覆盖它,按位保存一些新状态,包括图标化和类似,并忘记打开的位!酷!
答案 0 :(得分:2)
如何将窗口对象返回初始状态?
如果要将变量和组件返回到初始状态,那么我认为您需要重新创建窗口。
如何关闭窗口以便在设置可见时再次进行打开事件?
您可以通过以下方式跟踪窗口的关闭/打开:
WindowAdapter windowAdapter = new WindowAdapter()
{
boolean closed = false;
@Override
public void windowClosed(WindowEvent e) {
closed = true;
System.out.println("windowClosed");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
}
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened");
closed = false;
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
if (closed)
windowOpened(e);
}
};
要使“关闭”按钮起作用,您还需要使用:
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
答案 1 :(得分:1)
您可以使用此
要在java swing应用程序中查找活动窗口(无论是框架还是对话框),您可以使用以下递归方法:
Window getSelectedWindow(Window[] windows) {
Window result = null;
for (int i = 0; i < windows.length; i++) {
Window window = windows[i];
if (window.isActive()) {
result = window;
} else {
Window[] ownedWindows = window.getOwnedWindows();
if (ownedWindows != null) {
result = getSelectedWindow(ownedWindows);
}
}
}
return result;
}