2014-02-22 17 views
0

我在學校學習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不趕點擊?它必須是一個小的邏輯錯誤,我似乎把我的手指上:(

+3

使用'equals()'而不是'=='比較字符串。 –

回答

0

你從來沒有任何的ActionListener添加到您的菜單項。所以,很顯然,當你點擊他們什麼也沒有發生。

在另一方面,您要爲每個菜單添加一個監聽器,但不應該這樣做,因爲單擊菜單時不會發生任何事情(預計它應該自動打開,但它會自動打開)。

接下來的步驟將會是

  • 避免將字符串與進行比較
  • 實際操作分配命令到你的菜單項,因爲你的監聽器使用,要知道點擊了哪個項目

我建議建立每個項目一個監聽器類,而不是使用一個監聽器類處理幾個項目。

+0

我想過那個。我正在關注我的教授的例子。我的教授只需要一個內部類來處理菜單項。我會爲每個課程製作多個課程,但是她想要什麼,但是看起來不那麼有組織。順便說一句,它現在像一個魅力。多謝你們! – user3026473