1

我在使用时创建了MDI(多文档界面),其中我有两个 jbuttons 和一个 jdesktoppane,所以当单击两个按钮时,两个 jinternalframes 都在同一个 jdesktoppane 中打开,所以我想在什么时候打开?javanetbeansclose previous jinternalframeopen new jinternalframejdesktoppane

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

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

2 回答 2

3

只需调用dispose()实例JInternalFrame

为此,您需要将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);
      }
    }   
于 2012-12-11T17:22:51.960 回答
1

试试这个。

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

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

//then add the JInternalFrame into the JDesktopPane

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

于 2015-12-06T17:41:41.407 回答