如何使javaawt动作监听器多次运行

disho6za  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(493)

我有这个代码,我正试图让一个动作监听器被使用,它被多次使用。我反复测试了这段代码,发现它只运行了一次actionperformed(当您单击“login”按钮时)

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方法?我试过用不同的方法来做动作听众,但我似乎不能让他们正确(他们会返回错误)

3gtaxfhh

3gtaxfhh1#

它只运行一次actionperformed(当您单击“login”按钮时)

loginButton.addActionListener(this);

你只能把它添加到“登录”按钮。
您还需要将它添加到其他按钮中。
然而,共享actionlistener并不是一个好的设计。侦听器应该只执行一个函数。
我试过用不同的方法让动作听众听
是的,为每个按钮创建一个唯一的actionlistener是更好的方法。
例如:

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

相关问题