簡體   English   中英

從另一個類調用位於主類中的方法

[英]calling a method thats located in main class from another class

我在類中有一個方法,它包含main,名為addNewProduct(String name)在我的一個JPanel中,我有一個JButton,我有一個actionPerformed()這是在另一個名為DesktopGUI類中。 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");
       }
    });
}

假設你有兩個類,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
   }
}

編輯
根據您的代碼進行編輯。 注意做使代碼添加片斷編譯 請記住下次這樣做:

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,然后通過傳遞通過您的參考為通過其構造的GUI this

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM