[英]Can I add an ActionListener to a button in a jFrame and have the actionPerformed method in a different Class?
I have a jFrame where a user enters their data and a controller class where I would like to enter that data into a new object. 我有一个jFrame用户输入他们的数据和一个控制器类,我想将该数据输入到一个新对象。 At the moment I have added a listener to the button in the jFrame but cannot get it to recognise the actionPerformed method in the controller class.
目前我已经在jFrame中为按钮添加了一个监听器,但无法识别控制器类中的actionPerformed方法。
The 'this' part of the listener is stating that it is leaking into the constructor and the class itself is stating: 监听器的'this'部分声明它正在泄漏到构造函数中,并且类本身正在声明:
"CreateAccountGUI is not abstract and does not override abstract method actionPerformed(actionEvent) in Action Listener"
“CreateAccountGUI不是抽象的,并且不会覆盖Action Listener中的抽象方法actionPerformed(actionEvent)”
I do have other buttons but they use the default constructor provided when double clicking the controls. 我有其他按钮,但他们使用双击控件时提供的默认构造函数。
Heres the code for the jFrame constructor: 下面是jFrame构造函数的代码:
public class CreateAccountGUI extends javax.swing.JFrame implements ActionListener{
/**
* Creates new form CreateAccountGUI
*/
public CreateAccountGUI() {
initComponents();
cboAccountType.setVisible(false);
lblAccountType.setVisible(false);
btnCreateAccount.addActionListener(this);
}
And heres the code I currently have in the class where I want the actionPerformed method to reside: 以下是我希望actionPerformed方法驻留在类中的代码:
public class AccountStrategyController implements ActionListener, Observer{
private CreateAccountGUI view = null;
public void setView(CreateAccountGUI view){
this.view = view;
view.setVisible(true);//Show the account creation form
}
@Override
public void actionPerformed(ActionEvent e) {
}
I don't have any errors in this class at the moment. 我此刻这堂课没有任何错误。
I've looked online and it seems that you can have the added listener and the actionPerformed in different classes, I just can't seem to get my head round it. 我看过网上看起来你可以在不同的课程中添加听众和actionPerformed,我似乎无法理解它。
Any help is really appreciated :) 任何帮助真的很感激:)
You could do: 你可以这样做:
public class CreateAccountGUI extends javax.swing.JFrame {
public CreateAccountGUI() {
/**/
JButton btnCreateAccount = new JButton("Create");
AccountStrategyController controller = new AccountStrategyController();
controller.setView(this);
btnCreateAccount.addActionListener(controller);
}
}
class AccountStrategyController implements ActionListener{
private CreateAccountGUI view = null;
public void setView(CreateAccountGUI view){
this.view = view;
view.setVisible(true);//Show the account creation form
}
@Override
public void actionPerformed(ActionEvent e) {
//respond to button click
}
}
Or better: 或更好:
public class CreateAccountGUI extends javax.swing.JFrame {
public CreateAccountGUI() {
/**/
JButton btnCreateAccount = new JButton("Create");
AccountStrategyController controller = new AccountStrategyController();
controller.setView(this);
btnCreateAccount.addActionListener(controller.createAccountListener());
}
}
class AccountStrategyController{
private CreateAccountGUI view = null;
public void setView(CreateAccountGUI view){
this.view = view;
view.setVisible(true);//Show the account creation form
}
public ActionListener createAccountListener(){
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//respond to button click
}
};
}
}
createAccountListener
can be written using lambdas: createAccountListener
可以使用lambdas编写:
public ActionListener createAccountListener(){
return e -> {
//respond to button click
};
}
I would recommend to do this: 1. First of all, as you are working with an MVC (model-view-controller) structure, you must differentiate files. 我建议这样做:1。首先,当您使用MVC(模型 - 视图 - 控制器)结构时,您必须区分文件。 For example, your first code is a class that extends from a JFrame.
例如,您的第一个代码是从JFrame扩展的类。 This class goes to the view package and can't implement ActionListener.
此类转到视图包,无法实现ActionListener。 ActionListener is for controllers.
ActionListener适用于控制器。 To link JFrame class with controllers you will have to create a method in the 'CreateAccountGUI' like this:
要将JFrame类与控制器链接,您必须在'CreateAccountGUI'中创建一个方法,如下所示:
public void registerControllers(AccountStrategyController controller) {
jButton.setActionCommand("JBUTTON");
jButton.addActionListener(controller);
}
Then, in the actionPerformed() method you must write this: 然后,在actionPerformed()方法中,你必须写这个:
public void actionPerformed(ActionEvent e) {
switch(e.getActionCommand()) {
case "JBUTTON":
//Your code goes here
break;
}
}
And to make this work, in the Main class where you init the GUI you must write something like this: 为了使这个工作,在你初始化GUI的Main类中,你必须写这样的东西:
public static void main(String[] args) {
// Elements de la interfície gràfica
CreateAccountGUI gui = new CreateAccountGUI();
AccountStrategyController asc = new AccountStrategyController();
gui.registerControllers(asc);
}
Hope it helps you 希望它能帮到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.