如何多次创建 Java AWT 动作监听器 运行

How to make a Java AWT Action Listener run multiple times

我有这段代码,我正在尝试使用一个动作侦听器,并多次使用它。我一遍又一遍地测试这段代码,我发现它只是 运行 actionPerformed 一次(当你点击“登录”按钮时)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RyanOSGUI extends Frame implements ActionListener {
    //Login
    TextField password;
    Label passwordLabel;
    Button loginButton;

    //Apps
    Button console;
    Button calendar;

    //Calendar
    TextArea calendarView = new TextArea();
    Label addEvent = new Label("Add event to calendar: ");
    Label eventNameLabel = new Label("Event Name: ");
    TextField eventName = new TextField();
    Label eventDateLabel = new Label("Event Date: ");
    TextField eventDate = new TextField();
    Label eventTimeLabel = new Label("Event Time: ");
    TextField eventTime = new TextField();
    Button submitEvent = new Button("Submit event");

    RyanOSGUI(){
        password = new TextField();
        password.setBounds(175,90,100,20);

        passwordLabel = new Label("Enter Password:");
        passwordLabel.setBounds(175, 50, 100, 30);

        loginButton = new Button("Login");
        loginButton.setBounds(175, 140, 100, 30);
        loginButton.addActionListener(this);

        calendar = new Button("Calendar");
        calendar.setBounds(50,50,83,30);

        console = new Button("Console");
        console.setBounds(183,50,83,30);

        add(password);
        add(passwordLabel);
        add(loginButton);

        add(console);
        add(calendar);

        console.setVisible(false);
        calendar.setVisible(false);

        setResizable(false);
        setSize(450,500);
        setLayout(null);
        setVisible(true);
        setTitle("RyanOS");
        setIconImage(Toolkit.getDefaultToolkit().getImage("D:\Users\rperg\Pictures\Camera Roll\RyanOS.png"));
    }

    public void actionPerformed(ActionEvent a){
        //actionPerformed is only running once
        System.out.println("Ran action event");
        System.out.println(calendar);
        if(a.getSource() == loginButton){
            if(password.getText().equals("Testing123")){
                passwordLabel.setText("Welcome, Ryan!");
                setTitle("Ryan OS Home Screen");
                password.setVisible(false);
                passwordLabel.setVisible(false);
                loginButton.setVisible(false);
                console.setVisible(true);
                calendar.setVisible(true);
            }
            else{
                passwordLabel.setText("Wrong Password, Terminating program...");
                System.exit(69);
            }
        }
        if(a.getSource() == calendar){
            System.out.println("Ran Calendar App");
            console.setVisible(false);
            calendar.setVisible(false);
            runCalendar();
        }
    }

    public void runCalendar(){
        //Positioning
        calendarView.setBounds(10,10,430,300);
        addEvent.setBounds(10,310,100,30);
        eventNameLabel.setBounds(10,350,100,30);
        eventName.setBounds(110,350,100,20);
        eventDateLabel.setBounds(10, 380, 100, 30);
        eventDate.setBounds(110,380,100,20);
        eventTimeLabel.setBounds(10,410,100,30);
        eventTime.setBounds(110,410,100,20);
        submitEvent.setBounds(230, 375,100,30);

        //Adding to scene
        add(calendarView);
        add(addEvent);
        add(eventNameLabel);
        add(eventName);
        add(eventDateLabel);
        add(eventDate);
        add(eventTimeLabel);
        add(eventTime);
        add(submitEvent);
    }

    public static void main(String[] args){
        RyanOSGUI ros = new RyanOSGUI();
    }
}

class CalendarApp implements ActionListener {
    public void actionPerformed(ActionEvent a){

    }
}

有没有办法解决这个问题,以便我可以重复使用 actionPerformed 方法? 我尝试过使用不同的方法来制作动作监听器,但我似乎无法正确使用它们(它们 return 错误。)

it is only running actionPerformed one time (When you click the "Login" button)

loginButton.addActionListener(this);

您只能将它添加到“loginButton”。

您还需要将它添加到您的其他按钮。

但是,共享一个 ActionListener 并不是一个好的设计。侦听器应该只执行一个功能。

I have tried doing different ways of making Action Listeners

是的,为每个按钮创建一个唯一的 ActionListener 是更好的方法。

例如:

loginbutton.addActionListener( (e) -> 
{
    System.out.println("login");
});