【发布时间】: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