【发布时间】:2021-03-02 09:50:07
【问题描述】:
我有这段代码,我正在尝试使用一个动作侦听器,并多次使用它。我一遍又一遍地测试了这段代码,我发现它只运行了一次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 方法? 我尝试过使用不同的方法来制作动作监听器,但我似乎无法让它们正确(它们返回错误。)
【问题讨论】:
-
您为什么使用 AWT?您至少应该使用 Swing。您也不应该使用空布局和 setBounds()。 Java GUI 旨在与布局管理器一起使用。
-
即使你包含了上层的swing类,你仍然在使用AWT。这在很多层面上都是个坏主意。
标签: java awt actionlistener