使用其他jinternalframe类将jinternalframe类添加到jdesktoppane [英] Adding jinternalframe class to jdesktoppane using other jinternalframe class

查看:142
本文介绍了使用其他jinternalframe类将jinternalframe类添加到jdesktoppane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个非常简单的程序。
我创建了这个类:
MainJframeClass,JDesktopPaneClass,JinternalFrameClass1和JinternalFrameClass2。
我所做的就是我实例化了我的jdesktoppaneclass并将其命名为desktoppane1并将其添加到MainJframeclass中。我还实例化了2个jinternalframes并将其命名为internal1和internal2。现在,我在mainjframeclass中有按钮,当我按下时,我将internal1添加到desktoppane1。我现在的问题是如何使用放在internal1中某处的按钮将internal2添加到desktoppane1。我知道为什么我可以添加另一个按钮到desktoppane1并添加internal2。但我已经做到了,我只是想解决这个问题。如果你能帮帮我的话。顺便说一句,对不起我的英语。

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.

推荐答案

这只是一个参考问题。向JDesktopPane添加内容的代码必须具有对它的引用,因此您需要通过构造函数参数或方法参数将该引用传递给需要它的类。

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.

编辑1


例如:

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

这篇关于使用其他jinternalframe类将jinternalframe类添加到jdesktoppane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆