我在學校學習Java,我們正在學習製作GUI。Java動作監聽器不輸出結果
Toady我們正在做Menubars等,我想我明白,並添加menuitems,我明白。但是當我希望他們在用戶點擊它們時做些什麼。我搞不清楚了。
這裏是我的UI代碼:
package UserInterface;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
*
* @author Jesse
*/
public class AirlineReservation {
private JFrame frame;
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem addAirport;
private JMenuItem addAirline;
private JMenuItem addFlight;
private JMenuItem exit;
private JMenu aboutMenu;
private JMenuItem bookFlight;
private JMenuItem aboutInfo;
private JMenu bookMenu;
public AirlineReservation() {
initComponents();
}
private void initComponents() {
frame = new JFrame("Airline Reservation ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
// Initializing the JMenuBar
menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
// Initializing menu items
addAirport = new JMenuItem("Add Airport");
addAirline = new JMenuItem("Add Airline");
addFlight = new JMenuItem("Add Flight");
exit = new JMenuItem("Exit");
// Initializing the JMenu and adding JMenuItems
fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
fileMenu.add(addAirport);
fileMenu.add(addAirline);
fileMenu.add(addFlight);
fileMenu.add(exit);
menuBar.add(fileMenu);
fileMenu.addActionListener(new FileMenuAction());
//initializing the Jmenu and adding JMenuItems (bookFlight)
bookFlight = new JMenuItem("Flight Reservation");
bookMenu = new JMenu("Book");
bookMenu.setMnemonic('B');
bookMenu.add(bookFlight);
bookMenu.addActionListener(new BookMenuAction());
menuBar.add(bookMenu);
aboutInfo = new JMenuItem("dhdhd");
aboutMenu = new JMenu("About");
aboutMenu.add(aboutInfo);
aboutMenu.addActionListener(new AboutMenuAction());
menuBar.add(aboutInfo);
}
private class FileMenuAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "Add Airport") {
System.out.println("Add Airport Clicked");
} else if (e.getActionCommand() == "Add Airline") {
System.out.println("Add Airline Clicked");
} else if (e.getActionCommand() == "Add Flight") {
System.out.println("Add Flight Clicked");
}
}
}
private class BookMenuAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "sjsjsjs") {
System.out.println("fjfhfhf");
}
}
}
private class AboutMenuAction implements ActionListener {
public void actionPerformed(ActionEvent e) {}
}
}
的問題是,當我點擊一個的菜單項,就好像我的ActionListener不趕點擊?它必須是一個小的邏輯錯誤,我似乎把我的手指上:(
使用'equals()'而不是'=='比較字符串。 –