【问题标题】:Changing a JButton on click event在单击事件上更改 JButton
【发布时间】:2014-01-22 02:23:48
【问题描述】:

我正在尝试做一些非常简单的事情,在单击按钮时更改按钮中的文本。

我似乎无法让它工作,有人可以告诉我添加 ActionListener 的正确位置吗?

主类

public class ATM implements ActionListener{

  public static void main(String[] args) {
      atmGUI gui = new atmGUI();        
      gui.login();      
  }
}

atmGUI 类

public class atmGUI implements ActionListener {

public JTextField usernameField;
public JTextField pinField;
public String userName;
public int pin;

/**
 * @wbp.parser.entryPoint
 */

public void login() {

    JFrame frame = new JFrame();
    frame.getContentPane().setLayout(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 300);
    frame.setTitle("Virtual Bank Account");

    JLabel lblUsername = new JLabel("Username");
    lblUsername.setBounds(16, 88, 82, 16);
    frame.getContentPane().add(lblUsername);

    usernameField = new JTextField();
    usernameField.setBounds(13, 107, 124, 28);
    frame.getContentPane().add(usernameField);
    usernameField.setColumns(10);

    JLabel lblPin = new JLabel("PIN");
    lblPin.setBounds(16, 140, 61, 16);
    frame.getContentPane().add(lblPin);

    pinField = new JTextField();
    pinField.setBounds(13, 157, 124, 28);
    frame.getContentPane().add(pinField);
    pinField.setColumns(10);

    JButton btnLogin = new JButton("Login");
    btnLogin.setBounds(355, 232, 117, 29);
    btnLogin.addActionListener(new ActionListener(){
        btnLogin.setText("Clicked");
    });
    frame.getContentPane().add(btnLogin);


    frame.setResizable(false);
    frame.setAlwaysOnTop(true);
    frame.setVisible(true);

}

}

编辑:

这是产生的错误

new ActionListener(){} 类型必须实现继承的抽象方法 ActionListener.actionPerformed(ActionEvent)

【问题讨论】:

    标签: java swing jbutton actionlistener


    【解决方案1】:

    添加匿名函数

    btnLogin.addActionListener(new ActionListener(){
    
           //do something
           //lblUsername.setText("blah blah");
        });
    

    详情:

    btnLogin.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent evt) {
                    //do something
                    //lblUsername.setText("blah blah");
                }
            });
    

    【讨论】:

    • 这是我得到的错误,类型 new ActionListener(){} 必须实现继承的抽象方法 ActionListener.actionPerformed(ActionEvent)
    • 修复了最初的错误,得到了另一个错误,无法在不同方法中定义的内部类中引用非最终变量 btnLogin。错误在于 btnLogin 变量
    • 您必须将您在 actionPerformed() 中使用的所有变量设置为最终变量。例如“final JLabel lblUsername...”或“final JLabel lblPin...”
    • 太棒了!感谢您的宝贵时间!
    【解决方案2】:

    您应该在 atmGUI 类中实现actionperformed() 方法来处理点击操作。

    【讨论】:

    • 问题是我不确定如何使用,当我查找如何使用 actionlisteners 的解决方案时,我在实现它们时似乎遇到了更多错误。你觉得你能告诉我把方法放在哪里以及如何使用它吗?
    • @Thanh Le 已经用一些代码写了一个答案,看看吧。
    【解决方案3】:

    您需要将onActionPerformed(ActionEvent) 方法添加到您的atmgui 类中,如下所示:

    public void onActionPerformed(ActionEvent avt)
    {
        //code to handle the button click
    }
    

    或 你也可以在初始化JButton时使用它:

    JButton b=new JButton("Click Me!" );
    b.addActionListener(new ActionListener() {
        //handle the button click here
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      相关资源
      最近更新 更多