访问容器字段 [英] Accessing container fields

查看:180
本文介绍了访问容器字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常愚蠢的问题要问。



我正在使用NetBeans构建一个小应用程序,我遇到了以下问题;
我的主要类名为 mainApp ,并且正在扩展 JFrame ,其中包含 JPanel 名为 drawingBoard 我也因各种(和偏离主题)原因而延长..



核心问题是,在某些时候我需要访问 mainApp 的一个字段,但是由于NetBeans实例化我的主类的方式..(作为匿名类)我无法获得对容器的引用(这是我的mainApp)。



我该怎么做才能获得 mainApp 并在 drawingBoard 中设置其字段的值?

解决方案

您可以使用Window win = SwingUtilities.getWindowAncestor(myComponent)获取对顶级窗口的引用;并且传入方法调用对顶级窗口最终持有的任何组件的引用。如果你的主类也是你的顶级JFrame(你没有初始化任何其他JFrame),那么你可以将返回的Window转换为你的顶级类类型并调用它的公共方法。



例如:

  import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;
import javax.swing。*;

公共类Foo1 {
public static void main(String [] args){
MainApp mainApp = new MainApp();
mainApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainApp.pack();
mainApp.setLocationRelativeTo(null);
mainApp.setVisible(true);
}
}

类MainApp扩展JFrame {
public MainApp(){
getContentPane()。add(new DrawingBoard());
}

public void mainAppMethod(){
System.out.println(这是从主应用程序中调用的);
}
}

类DrawingBoard扩展JPanel {
public DrawingBoard(){
JButton button = new JButton(Button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
MainApp mainApp =(MainApp)SwingUtilities.getWindowAncestor(DrawingBoard.this);
mainApp。 mainAppMethod();
}
});
add(button);
}
}

改变由glowcoder的建议完成:

  import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;
import javax.swing。*;

公共类Foo2 {
public static void main(String [] args){
MainApp2 mainApp = new MainApp2();
mainApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainApp.pack();
mainApp.setLocationRelativeTo(null);
mainApp.setVisible(true);
}
}

类MainApp2扩展JFrame {
public MainApp2(){
getContentPane()。add(new DrawingBoard2(this));
}

public void mainAppMethod(){
System.out.println(这是从主应用程序中调用的);
}
}

类DrawingBoard2扩展JPanel {
私有MainApp2 mainApp;

public DrawingBoard2(最终的MainApp2 mainApp){
this.mainApp = mainApp;
JButton button = new JButton(Button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
buttonActonPerformed();
}
});
add(button);
}

private void buttonActonPerformed(){
mainApp.mainAppMethod();

}
}

另一个建议:既然你是学习Swing,你最好不要使用NetBeans为你生成Swing代码,而是手动编写Swing应用程序代码。通过这样做并研究教程,您将获得更深入和更好地理解Swing如何工作的知识,并且如果您需要使用NetBeans代码生成器来创建除最简单的应用程序之外的任何内容,它将帮助您。 / p>

I've got a very silly question to ask.

I'm using NetBeans to built a small app and I'm having the following problem; My main class is called mainApp and is extending a JFrame which in turn contains a JPanel called drawingBoard which I also extend for various (and off-topic) reasons..

The core problem is that at some point I need to access one of the fields of the mainApp but due to the way NetBeans instantiates my main class..(as anonymous class) I can't get a reference to the container(that is my mainApp).

What can I do to get a reference of mainApp and set the value of its field within drawingBoard?

解决方案

You can get a reference to the top level window by using Window win = SwingUtilities.getWindowAncestor(myComponent); and passing into the method call a refrence to any component that the top level window ultimately holds. If your main class is also your top level JFrame (you're not initializing any other JFrames), then you can cast the returned Window to your top level class type and call its public methods.

For example:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Foo1 {
   public static void main(String[] args) {
      MainApp mainApp = new MainApp();
      mainApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      mainApp.pack();
      mainApp.setLocationRelativeTo(null);
      mainApp.setVisible(true);
   }
}

class MainApp extends JFrame {
   public MainApp() {
      getContentPane().add(new DrawingBoard());
   }

   public void mainAppMethod() {
      System.out.println("This is being called from the Main App");
   }
}

class DrawingBoard extends JPanel {
   public DrawingBoard() {
      JButton button = new JButton("Button");
      button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            MainApp mainApp = (MainApp) SwingUtilities.getWindowAncestor(DrawingBoard.this);
            mainApp.mainAppMethod();
         }
      });
      add(button);
   }
}

altered to be done by glowcoder's recommendation:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Foo2 {
   public static void main(String[] args) {
      MainApp2 mainApp = new MainApp2();
      mainApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      mainApp.pack();
      mainApp.setLocationRelativeTo(null);
      mainApp.setVisible(true);
   }
}

class MainApp2 extends JFrame {
   public MainApp2() {
      getContentPane().add(new DrawingBoard2(this));
   }

   public void mainAppMethod() {
      System.out.println("This is being called from the Main App");
   }
}

class DrawingBoard2 extends JPanel {
   private MainApp2 mainApp;

   public DrawingBoard2(final MainApp2 mainApp) {
      this.mainApp = mainApp;
      JButton button = new JButton("Button");
      button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            buttonActonPerformed();
         }
      });
      add(button);
   }

   private void buttonActonPerformed() {
      mainApp.mainAppMethod();

   }
}

Another recommendation: since you're learning Swing, you're far better off not using NetBeans to generate Swing code for you but rather to code your Swing apps by hand. By doing this and studying the tutorials, you'll gain a far deeper and better understanding of just how Swing works, and it will help you should you need to use the NetBeans code generator to make anything more than the most simple of apps.

这篇关于访问容器字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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