I am learning Java in school, and we are learning to make GUI's.
Toady we are doing Menubars and such, which I think I understand, and adding menuitems, which I understand. But when I want them to do something when a user clicks on them. I am lost.
Here is my code of the 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) {}
}
}
the problem is that when I click on a menuitems, it seems as if my actionlistener does not catch the click? It has to be a small logical error, and I can seem to put my finger on it :(