简体   繁体   English

Java:在一个类中使用Actionlistener来引用另一个类中的变量

[英]Java:Using an Actionlistener in one class that refrences a variable in another class

I have a JTextField and a button in a class called Main. 我在一个名为Main的类中有一个JTextField和一个按钮。 I have an ActionListener in another class called Action. 我在另一个名为Action的类中有一个ActionListener。 I want the ActionListener to be able to refrence the JTextField. 我希望ActionListener能够引用JTextField。 I keep getting a null pointer exception. 我不断收到空指针异常。 I want to keep the JTextField and ActionListener seperate. 我想保持JTextField和ActionListener分开。 I am going to be having many ActionListeners and it would be easier for me to organize it this way. 我将有许多ActionListeners,这样对我来说组织起来会更容易。

public class Main  {

    public JTextField text;

    public JTextField getText(){
        return this.text;
    }

public  static void main(String[] args) {

        Main main=new Main();
        main.blah();
    }

public  void blah(){

    JFrame myWindow=new JFrame("ff");
    myWindow.setSize(500,500);
    myWindow.setVisible(true);
    myWindow.setDefaultCloseOperation(3);
    text=new JTextField(10);
    JLabel lengthL = new JLabel("Enter a number",SwingConstants.CENTER  );
    JButton button=new JButton("Click button");
    myWindow.getContentPane();
    myWindow.setLayout(new GridLayout(4,4));
    myWindow.add(lengthL);
    myWindow.add(text);
    myWindow.add(button);

    Action hand=new Action();
    button.addActionListener(hand);
}
}



public  class Action  implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        Main main=new Main();
        double length=Double.parseDouble(main.text.getText());
        System.out.println(length);

    }
}

Why not pass the JTextField when you create the ActionListener, like so: 为什么在创建ActionListener时不传递JTextField,如下所示:

public  class Action  implements ActionListener{
    private JTextField text;

    public Action(JTextField text) {
        this.text = text;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        double length=Double.parseDouble(text.getText());
        System.out.println(length);
    }
}

//in Main:
public void blah(){
    JFrame myWindow=new JFrame("ff");
    myWindow.setSize(500,500);
    myWindow.setVisible(true);
    myWindow.setDefaultCloseOperation(3);
    text=new JTextField(10);
    JLabel lengthL = new JLabel("Enter a number",SwingConstants.CENTER  );
    JButton button=new JButton("Click button");
    myWindow.getContentPane();
    myWindow.setLayout(new GridLayout(4,4));
    myWindow.add(lengthL);
    myWindow.add(text);
    myWindow.add(button);

    Action hand=new Action(text);  //change this line
    button.addActionListener(hand);
}

I keep getting a null pointer exception 我不断收到空指针异常

That's because you're creating a new Main instance when you handle the event: 这是因为您在处理事件时正在创建一个新的Main实例:

public void actionPerformed(ActionEvent e) {
    Main main=new Main();
    ...

You should better have the action a reference of the container class ( Main ) and a method to access that textfield value: 您最好使该操作具有容器类(Main)的引用以及访问该文本字段值的方法:

   Main aMain; 
   public void actionPerformed(ActionEvent e) {
    this.aMain.getText();
    ....

Or even better: 甚至更好:

    public void actionPerformed(ActionEvent e) {
       double lenght = this.main.getValue();// returns double 
       ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM