从外部类切换Java中的jPanels [英] Switching jPanels in Java from external class

查看:72
本文介绍了从外部类切换Java中的jPanels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 jFrame MainForm ,其中包含 main() placePanel(panel)和下拉菜单。我从下拉菜单中的项目调用 placePanel(panel)方法将特定的面板放置在 jFrame 容器中。

I have jFrame class MainForm which contains main(), placePanel(panel) and drop down menu. From the items in drop down menu I call on placePanel(panel) method to place a specific panel in the jFrame container. This works fine.

但是,当我单击 jPanel 类。当我尝试从任何 jPanel <中调用 jFrame MainForm.placePanel(panel)时/ code>加载到 jFrame 的容器中,出现错误:无法引用非静态内容等。我也尝试过 Mainform.getContainer()。add(panel),但它也无法正常工作。

But, I don't know how to switch panels when I click on a button which is inside a jPanel class. When I try to call jFrame's MainForm.placePanel(panel) from any jPanel which is loaded into jFrame's container, I get the error: cannot reference non-static content etc. I also tried Mainform.getContainer().add(panel), but it doesn't work too.

我不知道如何从另一个类访问 MainForm 的容器,或者如何从另一个面板中使用方法切换面板。

I can't figure out how to access MainForm's container from another class, or how to switch panels with a method from another panel.

谢谢

推荐答案

如果要从另一个对象中调用对象上的方法对象,您将需要对第一个对象的引用,以便您可以在活动对象本身而不是类上调用该方法(就像您当前正在尝试做的那样)。解决此问题的一种方法是将包含JPanels的类的引用传递给具有按钮的动作侦听器代码的类,也许是在后者的构造函数中。换句话说,您需要将对当前处于活动状态且可视化的MainForm对象的引用传递到具有该按钮的ActionListener的类中。

If you want to call a method on an object from within another object, you're going to need a reference to the first object so you can call the method on the active object itself and not on the class (as you're currently trying to do). One way to solve this is to pass a reference to the class that holds the JPanels to the class that has the button's action listener code, perhaps in the latter's constructor. In other words you'll want to pass a reference to the current active and visualized MainForm object into the class that has the ActionListener for the button.

顺便说一下,您是否将JPanels与CardLayout交换了?如果没有,我建议您研究一下,因为通常这是最简单的方法。

By the way, are you swapping JPanels with a CardLayout? If not, I suggest you look into it as it's usually the easiest way to do this.

编辑:

例如,假设您有一个名为MainForm的类,该类具有一个名为swapJPanels的公共方法,该方法可以交换视图,而另一个类MyPanel的JButton希望从MainForm类中调用方法,则可以给MyPanel一个构造函数,该构造函数带有MainForm参数,并允许您将当前MainForm对象(位于类内部)的引用传递到MyPanel对象中:

For example, say you have a class called MainForm that has a public method called swapJPanels that allows it to swap views, and another class MyPanel that has your JButton that wants to call a method from the MainForm class, then you could give MyPanel a constructor that takes a MainForm parameter and allows you to pass a reference from the current MainForm object (this inside of the class), into the MyPanel object:

MainForm:

class MainForm extends JFrame { 

   public MainForm() {
      MyPanel myPanel = new MyPanel(this); // pass "this" or the current MainForm reference to MyPanel
   }

   public void swapJPanels(int panelNumber) {

   }
}

MyPanel:

class MyPanel extends JPanel {
   private MainForm myMainForm; // holds the reference to the current MainForm object

   public MyPanel(MainForm myMainForm) {
      this.myMainForm = myMainForm; // set the reference

      JButton swapPanelBtn = new JButton("Swap Panel");
      swapPanelBtn.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            swapPanelBtnActionPerformed();
         }
      });
   }

   private void swapPanelBtnActionPerformed() {
      myMainForm.swapJPanels(0); // calling a method on the reference to the current MainForm object
   }
}

这篇关于从外部类切换Java中的jPanels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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