我希望能够检测到JInternalFrame
中JDesktopPane
的所有实例何时关闭。我一直在查看桌面窗格触发的事件,但我看不到适用的事件。我已经尝试为添加到桌面窗格的每个人添加JInternalFrameListener
并收听已关闭的活动,但这似乎无效。
答案 0 :(得分:0)
这是一个工作示例:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class InternalFrameDemo extends JFrame {
JDesktopPane desktop;
InternalFrameAdapter adapter;
int frameCount = 0;
public InternalFrameDemo() {
super("InternalFrameDemo");
adapter = new InternalFrameAdapter() {
public void internalFrameClosed(InternalFrameEvent e) {
frameCount--;
if (frameCount == 0) {
JOptionPane.showMessageDialog(InternalFrameDemo.this, "All internal frames closed.");
}
}
};
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
desktop = new JDesktopPane();
createFrame();
createFrame();
createFrame();
setContentPane(desktop);
}
protected void createFrame() {
JInternalFrame frame = new JInternalFrame("title", true, true);
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
frameCount++;
frame.addInternalFrameListener(adapter);
desktop.add(frame);
}
private static void createAndShowGUI() {
InternalFrameDemo frame = new InternalFrameDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
}
catch (ClassNotFoundException e) {
// handle exception
}
catch (InstantiationException e) {
// handle exception
}
catch (IllegalAccessException e) {
// handle exception
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
你基本上在每个内部框架上添加一个监听器,并跟踪打开和关闭的框架(使用int变量)。