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

时间:2012-12-11 17:13:13

标签: java swing netbeans-7 jinternalframe jdesktoppane

我在MDI中使用java创建了netbeans(多个文档界面),其中我有两个jbuttons和一个jdesktoppane,因此当点击两个按钮时,两个jinternalframes都在同一个jdesktoppane中打开所以我希望close previous jinternalframe中的open new jinternalframejdesktoppane如何?

检查快照以便更好地了解我的问题我想要什么... 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);
      }
    } 

2 个答案:

答案 0 :(得分:3)

只需在dispose()个实例上调用JInternalFrame即可。

要执行此操作,您需要将JInternalFrame声明移出方法,以便我们可以检查实例是否不是null(因此存在现有实例而不是{{1}在创建新实例之前的实例上:

dispose()

答案 1 :(得分:1)

试试这个。

将此添加到您的JButton事件:



JDesktopPane.removeAll();
JDesktopPane.updateUI();

//then add the JInternalFrame into the JDesktopPane




我知道,我知道,这个问题是2年前,但希望这有助于其他人:)