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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13825120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:05:55  来源:igfitidea点击:

How to close previous jInternalFrame when open new jInternalFrame in the jDesktopPane

javaswingnetbeans-7jinternalframejdesktoppane

提问by Ayaz Ali

I created MDI(Multiple Document interface) in javausing netbeansin which i have two jbuttons and one jdesktoppane so when clicked on both button then both jinternalframes are opened in same jdesktoppane so i want how to close previous jinternalframewhen open new jinternalframein the jdesktoppane?

MDIjava使用netbeans中创建了(多文档界面),其中我有两个 jbuttons 和一个 jdesktoppane,所以当点击这两个按钮时,两个 jinternalframes 都在同一个 jdesktoppane 中打开,所以我想在什么close previous jinternalframe时候open new jinternalframe打开jdesktoppane

Check snapshot for more better understand my question what i want... enter image description here

检查快照以更好地了解我的问题我想要什么...... 在此处输入图片说明

First jButton code:

第一个 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);
      }
    }   

Second button Code:

第二个按钮代码:

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);
      }
    } 

回答by David Kroukamp

Simply call dispose()on JInternalFrameinstance.

简单的调用dispose()JInternalFrame的实例。

To do this you will need to move your JInternalFramedeclaration out of the method so that we may check if the instance is not null(thus there is an existing instance and than call dispose()on the instance before creating a new one:

为此,您需要将您的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);
      }
    }   

回答by JairKaulitz89

Try this out.

试试这个。

Add this to your JButton's event:

将此添加到您的 JButton 事件中:

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

//then add the JInternalFrame into the JDesktopPane

I know, I know, this question was 2 years ago, but hope this helps someone else :)

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