[英]Looking to add functionality to more than one button in the same panel in JFrame
這是完整的代碼:
public class HelloWorld implements ActionListener{
private static JLabel label;
private static JTextField usernameText;
private static JLabel password;
private static JPasswordField passwordText;
private static JButton login;
private static JLabel success;
private static JButton Reset;
@Override
public void actionPerformed(ActionEvent e) {
String user = usernameText.getText();
String password = passwordText.getText();
System.out.println(user + "," + password);
String command = e.getActionCommand();
if(command.equals("Reset")){
System.out.print("Hello");
我有String command = e.getActionCommand();
在我的public void actionPerformed(ActionEvent e) {
上面在我的公共 static 主要我有
reset = new JButton("reset");
reset.setBounds(85,170,100,25);
panel.add(reset);
我的按鈕、文本字段和標簽中的所有 my 都已創建。 我也有
if(command.equals("reset")){
在我的@Override
中,但是當我單擊重置按鈕時,什么也沒有發生。
在行動中執行
if(e.getSoruce == buttonNmae) {
// Your code
}
例如:
if(e.getSource == reset) {
resetGame();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.