【问题标题】:calling a method thats located in main class from another class从另一个类调用位于主类中的方法
【发布时间】:2014-09-01 21:14:58
【问题描述】:

我的类中有一个名为 addNewProduct(String name) 的主方法。 main 创建DesktopGUI 的副本,该对象构成框架和所有组件。我想有一种方法可以从DesktopGUI 调用主类中的方法。

主类(这是有问题的代码行。这不是我的代码的完整表示。代码行也可能与它们在我的实际应用程序中的顺序不同。)

   public List<Product> productList;
    public void addNewProduct(String name){
      Product product = new Product();
      product.setName(name);
      productList.add(product);
    }

    DesktopGUI gui = new DesktopGUI();
    frame = gui.getFrame();

DesktopGUI 的构造函数中,它创建并显示框架。

在我的一段代码中

SaveButton.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent event) {
      System.out.println("Save Button Pressed");
      System.out.println("Name: "+(String)fileNameField.getText());
      /*the addNewProduct() is located in the class for main*/
      addNewProduct((String)fileNameField.getText());
   }
});

如果类是静态的,我可以调用它,但类 productList 的大小始终为零。

举个简单的例子

public class Foo {
   public List<Product> productList;
   public static void main(String[] args) {
      A a = new A();

   }
   public void addNewProduct(String name){
      Product product = new Product();
      product.setName(name);
      productList.add(product);
   }
}

class A {
   SaveButton.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent event) {
          foo.addNewProduct("Hello World");
       }
    });
}

【问题讨论】:

    标签: java swing scope


    【解决方案1】:

    假设您有两个类,A 和 B,并且您希望 B 在 A 中调用 method1(),那么 B 需要 A 的 有效 实例来调用该方法。比如……

    public class Foo {
       public static void main(String[] args) {
          A a = new A();
          B b = new B(a); // pass A reference into B
    
          b.someMethodInB();
       }
    }
    
    class A {
       // the method that we're interested in!
       public void method1() {
          System.out.println("method1 in A");
       }
    }
    
    class B {
       private A a; //give B a field to hold A's reference
    
       // Allow B's constructor to accept the A reference
       public B(A a) {
          this.a = a;  // and assign it to a field
       }
    
       public void someMethodInB() {
          a.method1();   // now B can call A's method
       }
    }
    

    编辑
    根据您的代码进行编辑。请注意为使代码 sn-p 可编译而添加的内容。请记住下次这样做:

    import java.awt.event.*;
    import java.util.List;
    import javax.swing.JButton;
    
    public class Foo {
       public List<Product> productList;
    
       public static void main(String[] args) {
          Foo f = new Foo();
          A a = new A(f);
    
       }
    
       public void addNewProduct(String name) {
          Product product = new Product();
          product.setName(name);
          productList.add(product);
       }
    }
    
    class A {
       private JButton saveButton = new JButton();
       private Foo foo;
    
       public A(final Foo foo) {
          this.foo = foo;
          saveButton.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent event) {
                foo.addNewProduct("Hello World");
             }
          });
       }
    }
    
    class Product {
    
       public void setName(String name) {
          // TODO Auto-generated method stub
    
       }
    
    }
    

    编辑 3
    我刚刚注意到您正在 ProductList 类中创建一个 GUI 实例。通常它以相反的方式完成,但如果这是你想要的方式,那么如果 GUI 不是在 static 世界中创建的,那么通过它的构造函数将你的引用传递给 GUI传入this

    DesktopGUI gui = new DesktopGUI(this); // Note change
    frame = gui.getFrame();
    

    【讨论】:

    • 我正在考虑将主类传递给另一个类,但传递 this 会出错。在main中调用this时是main的作用域还是main类的作用域。
    • @PatrickW.McMahon:见编辑。如果您遇到错误,请简化您的代码(类似于我上面所做的),并向我们展示您的新代码和您的错误/问题。
    • 我已根据您的示例更新了我的问题,但已更改为适合我正在尝试做的事情。
    • @PatrickW.McMahon 将 A a = new A(); 更改为 A a = new A(new Foo()); 并将 public A(Foo foo) { this.foo = foo; } 添加到类 A
    • 我明白了,所以创建 foo 类的实例不会再次执行 main 对吗?
    猜你喜欢
    • 2016-01-08
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多