Adding jinternalframe class to jdesktoppane using other jinternalframe class

前端 未结 2 1734
独厮守ぢ
独厮守ぢ 2021-01-22 10:59

I\'m creating a very simple program. I have created this classes : MainJframeClass, JDesktopPaneClass, JinternalFrameClass1 and JinternalFrameClass2. what ive done is that i i

相关标签:
2条回答
  • 2021-01-22 11:47

    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();
       }
    }
    
    0 讨论(0)
  • 2021-01-22 11:54

    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 );
    } 
    
    0 讨论(0)
提交回复
热议问题