[英]Adding an ActionListener to a JButton from another class is giving a NullPointerException?
I want to be able to add an AcitonListener to a JButton from another class just to keep the code neat. 我希望能够将AcitonListener从另一个类添加到JButton只是为了保持代码整洁。 The only problem is that it brings up a NullPointerException when trying to add it.
唯一的问题是,在尝试添加它时会引发NullPointerException。
I'm adding the ActionListener through the Handler class which is defined as 'h'. 我通过定义为'h'的Handler类添加ActionListener。
In my Display class: 在我的展示广告类中:
public class Display {
private Handler h; //My handler object
private JFrame frame;
private JButton btnCalculate;
/**
* Launch the application.
*/
public static void createDisplay() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Display window = new Display();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Display() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
btnCalculate = new JButton("Calculate");
btnCalculate.setBounds(66, 208, 89, 23);
btnCalculate.addActionListener(h.getCalculateListener()); //ActionListener is added here.
frame.add(btnCalculate);
}
//Getter for the JButton
public JButton getBtnCalculate() {
return btnCalculate;
}
}
Here's my Handler class that includes mostly getters. 这是我的Handler类,其中大多数包含getter。 It helps make everything a lot more simple by having all the getters in the Handler class instead of having them spread out in multiple different classes:
通过将所有吸气剂放在Handler类中,而不是将它们分散在多个不同的类中,它可以使一切变得更加简单:
public class Handler {
private Display display; //My Display object
private CalculateActionListener calculate; //My CalculateActionListener object
public Handler(Display display, CalculateActionListener calculate) {
this.display = display;
this.calculate = calculate;
}
public JButton getButton() {
return display.getBtnCalculate();
}
public ActionListener getCalculateListener() {
return calculate.getCalculateListener();
}
}
And finally, here's my CalculateActionListener class which contains the actual ActionListener: 最后,这是我的CalculateActionListener类,其中包含实际的ActionListener:
public class CalculateActionListener {
//Here's the actual ActionListener
private ActionListener calculateListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "WORKING!");
}
};
//Getter for my ActionListener
public ActionListener getCalculateListener() {
return calculateListener;
}
}
Note Imports were removed but are there in the actually code. 注意导入已删除,但在实际的代码中。 Imports are not the problem.
进口不是问题。
You are not creating a new instance of Handler
. 您没有创建
Handler
的新实例。 Before you use h
, you need to create an instance of it. 在使用
h
之前,您需要创建它的实例。 Add a line before you create the button in the Display class constructor. 在Display类构造函数中创建按钮之前,添加一行。 Something like this:
像这样:
h = new Handler(this, new CalculateActionListener());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.