Adding jinternalframe class to jdesktoppane using

2019-02-26 22:21发布

问题:

I'm creating a very simple program. I have created this classes : MainJframeClass, JDesktopPaneClass, JinternalFrameClass1 and JinternalFrameClass2. what ive done is that i instantiated my jdesktoppaneclass and named it desktoppane1 and i added it to the MainJframeclass. i have also instantiated the 2 jinternalframes and named it internal1 and internal2. Now, i have button in mainjframeclass that when i press, i add the internal1 to desktoppane1. what my problem now is how to add the internal2 to desktoppane1 using a button placed somewhere in internal1. i know that why could i just add another button to desktoppane1 and add the internal2. but i have done it already, i just want to solve this problem. if you can help me please. sorry for my english by the way.

回答1:

It's simply a matter of references. The code that adds something to the JDesktopPane must have a reference to it, and so you will need to pass that reference into the class that needs it say via either a constructor parameter or a method parameter.

Edit 1
For example:

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class ReferenceExample extends JPanel {
   private JDesktopPane desktop = new JDesktopPane();
   private Random random = new Random();

   public ReferenceExample() {
      JButton addInternalFrameBtn = new JButton("Add Internal Frame");
      addInternalFrameBtn.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            addInternalFrame();
         }
      });
      JPanel btnPanel = new JPanel();
      btnPanel.add(addInternalFrameBtn);

      setPreferredSize(new Dimension(600, 450));
      setLayout(new BorderLayout());
      add(new JScrollPane(desktop), BorderLayout.CENTER);
      add(btnPanel, BorderLayout.SOUTH);
   }

   public void addInternalFrame() {
      MyInternalFrame intFrame = new MyInternalFrame(ReferenceExample.this);
      int x = random.nextInt(getWidth() - intFrame.getPreferredSize().width);
      int y = random.nextInt(getHeight() - intFrame.getPreferredSize().height);
      intFrame.setLocation(x, y);
      desktop.add(intFrame);
      intFrame.setVisible(true);
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("Reference Eg");
      frame.getContentPane().add(new ReferenceExample());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

class MyInternalFrame extends JInternalFrame {

   // pass in the reference in the constructor
   public MyInternalFrame(final ReferenceExample refEg) {
      setPreferredSize(new Dimension(200, 200));
      setClosable(true);

      JButton addInternalFrameBtn = new JButton("Add Internal Frame");
      addInternalFrameBtn.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            // use the reference here
            refEg.addInternalFrame();
         }
      });
      JPanel panel = new JPanel();
      panel.add(addInternalFrameBtn);
      getContentPane().add(panel);
      pack();
   }
}


回答2:

how to add the internal2 to desktoppane1 using a button placed somewhere in internal1.

In the ActionListener added to your button you can use code like the following to get a reference to the desktop pane:

Container container = SwingUtilities.getAncestorOfClass(JDesktopPane.class, (Component)event.getSource());

if (container != null)
{
    JDesktopPane desktop = (JDesktopPane)container;
    JInternalFrame frame = new JInternalFrame(...);
    desktop.add( frame );
}