我正在 JDesktopPane
中创建多个 JInternalFrame
。每个框架有一个按钮。我想关闭单击按钮的特定框架。
internalFrame = new JInternalFrame("Internal Frame", true, true, true, true);
internalFrame.setSize(300, 300);
internalFrame.setLocation(xPosition * openFrameCount, yPosition
* openFrameCount);
internalFrame.setContentPane(createContentPane());
internalFrame.setJMenuBar(createPopJMenuBar());
internalFrame.setVisible(true);
JButton close = new JButton( "Close Me!" );
close.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
try {
internalFrame.setClosed( true );
} catch (PropertyVetoException e1) {
e1.printStackTrace();
}
}
} );
internalFrame.add( close );
jdpDesktop.add(internalFrame);
通过使用上面的代码,我可以关闭最后创建的框架。其他框架未关闭。
最佳答案
看起来您有 internalFrame
作为封闭类的字段。然后每次创建新的时它都会被覆盖。使用局部变量代替:
// Notice "final"
final JInternalFrame internalFrame = new JInternalFrame("Internal Frame", true, true, true, true);
...
这确保每个操作监听器中的 internalFrame
引用上面几行创建的帧,而不是最后创建的帧。
关于java - 单击按钮时关闭特定的 JInternalFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18288884/