gpt4 book ai didi

java - 在 jDesktopPane 中打开新的 jInternalFrame 时如何关闭以前的 jInternalFrame

转载 作者:行者123 更新时间:2023-11-29 03:39:27 34 4
gpt4 key购买 nike

我使用 netbeansjava 中创建了 MDI(多文档界面),其中我有两个 jbuttons 和一个 jdesktoppane 所以当点击两个按钮然后两个 jinternalframes 在同一个 jdesktoppane 中打开,所以我想如何在 jdesktoppane打开新的 jinternalframe关闭以前的 jinternalframe

检查快照以更好地理解我的问题我想要什么... enter image description here

第一个jButton代码:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
try
{

tst t = new tst();
JInternalFrame internalFrame1 = new JInternalFrame("Test Window1");
internalFrame1.add(t.getContentPane());
internalFrame1.pack();

internalFrame1.setVisible(true);
q.add(internalFrame1);

internalFrame1.setClosable(true);

BasicInternalFrameUI ui = (BasicInternalFrameUI)internalFrame1.getUI();
Container north = (Container)ui.getNorthPane();
north.remove(0);
north.validate();
north.repaint();

for(MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) internalFrame1.getUI()).getNorthPane().getMouseListeners()){
((javax.swing.plaf.basic.BasicInternalFrameUI) internalFrame1.getUI()).getNorthPane().removeMouseListener(listener);
}

internalFrame1.setSelected(true);

}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
}

第二个按钮代码:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
try
{

zxy z = new zxy();
JInternalFrame internalFrame = new JInternalFrame("Test Window2");
internalFrame.add(z.getContentPane());
internalFrame.pack();
internalFrame.setSize(570,420);

internalFrame.setVisible(true);
q.add(internalFrame);

internalFrame.setClosable(true);

BasicInternalFrameUI ui = (BasicInternalFrameUI)internalFrame.getUI();
Container north = (Container)ui.getNorthPane();
north.remove(0);
north.validate();
north.repaint();

for(MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) internalFrame.getUI()).getNorthPane().getMouseListeners()){
((javax.swing.plaf.basic.BasicInternalFrameUI) internalFrame.getUI()).getNorthPane().removeMouseListener(listener);
}

internalFrame.setSelected(true);

}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
}

最佳答案

只需在 JInternalFrame 实例上调用 dispose()

为此,您需要将 JInternalFrame 声明移出该方法,以便我们可以检查该实例是否不是 null(因此存在一个现有实例然后在创建新实例之前在实例上调用 dispose():

JInternalFrame internalFrame1;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try
{

if(internalFrame1 !=null) {//make sure its not null
internalFrame1.dispose();//close the previos internalframe
}

tst t = new tst();
internalFrame1 = new JInternalFrame("Test Window1"); //create new instance of internal frame
internalFrame1.add(t.getContentPane());
internalFrame1.pack();

internalFrame1.setVisible(true);
q.add(internalFrame1);

internalFrame1.setClosable(true);

BasicInternalFrameUI ui = (BasicInternalFrameUI)internalFrame1.getUI();
Container north = (Container)ui.getNorthPane();
north.remove(0);
north.validate();
north.repaint();

for(MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) internalFrame1.getUI()).getNorthPane().getMouseListeners()){
((javax.swing.plaf.basic.BasicInternalFrameUI) internalFrame1.getUI()).getNorthPane().removeMouseListener(listener);
}

internalFrame1.setSelected(true);

}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
}

关于java - 在 jDesktopPane 中打开新的 jInternalFrame 时如何关闭以前的 jInternalFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13825120/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com