使用其他 jinternalframe 类将 jinternalframe 类添加到 jdesktoppane

发布于 2024-11-27 06:47:26 字数 440 浏览 5 评论 0原文

我正在创建一个非常简单的程序。 我创建了这个类: MainJframeClass、JDesktopPaneClass、JinternalFrameClass1 和 JinternalFrameClass2。 我所做的是实例化我的 jdesktoppaneclass 并将其命名为 Desktoppane1 并将其添加到 MainJframeclass 中。我还实例化了 2 个 jinternalframes 并将其命名为“internal1”和“internal2”。现在,我在 mainjframeclass 中有一个按钮,当我按下该按钮时,我会将内部 1 添加到桌面窗格 1 中。我现在的问题是如何使用放置在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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无尽的现实 2024-12-04 06:47:26

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

编辑 1
例如:

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

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();
   }
}
抚笙 2024-12-04 06:47:26

如何使用放置在internal1中某处的按钮将internal2添加到desktoppane1。

在添加到按钮的 ActionListener 中,您可以使用如下代码来获取对桌面窗格的引用:

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

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

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 );
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文