简体   繁体   English

更改单击事件上的JButton

[英]Changing a JButton on click event

I'm trying to do something very simple, change the text in a button when it has been clicked. 我正在尝试做一些非常简单的事情,在单击按钮时更改按钮中的文本。

I can't seem to get it to work, can someone show me the correct place to add an ActionListener? 我似乎无法正常工作,有人可以告诉我添加ActionListener的正确位置吗?

Main Class 主班

public class ATM implements ActionListener{

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

atmGUI Class 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);

}

}

EDIT : 编辑:

Here is the error that is produced 这是产生的错误

The type new ActionListener(){} must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent) new ActionListener(){}类型必须实现继承的抽象方法ActionListener.actionPerformed(ActionEvent)

add function anonymous 添加匿名函数

btnLogin.addActionListener(new ActionListener(){

       //do something
       //lblUsername.setText("blah blah");
    });

Detail: 详情:

btnLogin.addActionListener(new ActionListener() {

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

您应该在atmGUI类中实现actionperformed()方法,以处理单击的操作。

You will need to add the onActionPerformed(ActionEvent) method to your atmgui class, do it like: 您将需要在atmgui类中添加onActionPerformed(ActionEvent)方法,如下所示:

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

OR You can also use it while initializing the JButton : 或者您也可以在初始化JButton使用它:

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

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

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