在我的应用程序中,在 JDesktopPane 中,我添加了一些 JInternalFrames。JInternalFrames 的激活和停用正常发生,直到其中一个 JInternalFrame 最大化。之后,以编程方式激活内部框架,多次触发 internalFrameActivated、internalFrameDeactivated 事件。为什么会被多次调用?这我只在 WindowsLookAndFeel 中观察到
public class IFTest {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(new WindowsLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
JDesktopPane pane = new JDesktopPane();
JInternalFrame if1 = new JInternalFrame("IF1");
JInternalFrame if2 = new JInternalFrame("IF2");
if1.setTitle("IF1");
if2.setTitle("IF2");
pane.add(if1);
pane.add(if2);
frame.getContentPane().add(pane);
frame.setSize(500, 500);
frame.setVisible(true);
if1.setMaximizable(true);
if1.setSize(400, 400);
showInternalFrame(if1);
if1.addInternalFrameListener(new MyInternalFrameListener("IF1"));
if2.setMaximizable(true);
if2.setSize(300, 300);
if2.setVisible(true);
showInternalFrame(if2);
if2.addInternalFrameListener(new MyInternalFrameListener("IF2"));
System.out.println("------------------------------");
try {
if1.setMaximum(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
System.out.println("--------------------------------");
showInternalFrame(if2);
}
static class MyInternalFrameListener extends InternalFrameAdapter {
String name;
public MyInternalFrameListener(String name) {
this.name = name;
}
@Override
public void internalFrameActivated(InternalFrameEvent e) {
System.out.println(name + " activated");
}
@Override
public void internalFrameIconified(InternalFrameEvent e) {
System.out.println(name + " iconfied");
}
@Override
public void internalFrameDeactivated(InternalFrameEvent e) {
System.out.println(name + " deactivated");
}
@Override
public void internalFrameDeiconified(InternalFrameEvent e) {
System.out.println(name + " deiconfied");
}
}
public static void showInternalFrame(JInternalFrame intf) {
try {
if (intf.isIcon())
intf.setIcon(false);
intf.setVisible(true);
intf.moveToFront();
intf.setSelected(true);
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}
}
}