2013-03-05 88 views
-1

我在這裏有一個簡單的訂購系統,它要求用戶輸入收銀員姓名,客戶名稱和密碼。用戶完成該過程後,它將顯示一個包含不同膳食的組合框。組合框無法正常工作

我想要發生的是,一旦我點了一頓飯,它應該顯示我選擇的膳食名稱,並應該執行我在actionlistener中聲明的動作。

該程序編譯良好,但我想要發生的邏輯是行不通的,我正在盡我所能解決這個問題,但我認爲我不能這樣做我自己請幫助我謝謝!

import java.util.Scanner; 
    import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 


public class SubokUlit { 
    private JComboBox combo; 
    private String a = ""; 
    private static int answer; 
    private static int total = 0; 
    private static int total1 = 0; 
    private static int wew = 0; 
    private static String order1 = ""; 
    private static String order2 = ""; 
    private static Scanner inp = new Scanner(System.in); 


public SubokUlit(){ 
    String mgaPagkainTo[] = {"PM1 (Paa/ Spicy Paa with Thigh part)","PM2 (Pecho)"};  
    JFrame frame = new JFrame("Mang Inasal Ordering System"); 
    JPanel panel = new JPanel(); 
    combo = new JComboBox(mgaPagkainTo); 
    combo.setBackground(Color.gray); 
    combo.setForeground(Color.red); 
    panel.add(combo); 
    frame.add(panel); 
     combo.addActionListener(new ActionListener(){ // The logic in here does not work 
    public void actionPerformed(ActionEvent e){ 
     String str = (String)combo.getSelectedItem(); 
     a = str; 
     if(a.equals("PM1 (Paa/ Spicy Paa with Thigh part)")){ // If I select PM1 in the combo box it should do the following. But it doesn't work 
       System.out.print ("\n\n\n\t\tYou have chosen Paborito Meal 1.\n"); 
        System.out.print ("\t\t\tPlease enter the quantity: "); 
        int quantity1 = inp.nextInt(); 
        total = quantity1 * 99; 
        order1 = quantity1 + " " + "PM1 " + "         " + total +"\n"; 
     } 
     else if(a.equals("PM2 (Pecho)")){ // The same thing should alaso happen here in PM2 
      System.out.print ("\n\n\n\t\tYou have chosen Paborito Meal 2.\n"); 
       System.out.print ("\t\t\tPlease enter the quantity: "); 
       int quantity2 = inp.nextInt(); 
       total1 = quantity2 * 99; 
       order2 = quantity2 + " " + "PM2 " + "         " + total1 +"\n"; 
     } 
    } 
    }); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(300,100); 
     frame.setVisible(true); 
} 


public static void main(String[]args) { 
     Scanner inp = new Scanner(System.in); 
     String userNamePoe = ""; 
     String customerNamePoe = ""; 
     String sanKaKain = ""; 
     boolean ulitinMoPows = true; 
     boolean tryAgain = true; 

     System.out.print("\nInput Customer Name: "); 
     String customerName = inp.nextLine(); 
     customerNamePoe = customerName; 
     System.out.print("\nInput Cashier Name: "); 
     String user = inp.nextLine(); 
     userNamePoe = user; 
    do{ 
     System.out.print("\nInput either Dine In or Take Out: "); 
     String dInDOut = inp.nextLine(); 
     sanKaKain = dInDOut; 
      if (sanKaKain.equals("Dine In") || sanKaKain.equals("Take Out")){ 
      System.out.print(""); 
      ulitinMoPows = false; 
      } 
      else{ 
      JOptionPane.showMessageDialog(null, "Try again! Please input Dine In or Take Out only!","Error", JOptionPane.ERROR_MESSAGE); 
       ulitinMoPows = true; 
       System.out.print ("\f"); 
       } 
    }while(ulitinMoPows); 
    do{ 
     System.out.print("\nInput password: "); 
     String pass = inp.nextLine(); 
     if(pass.equals("admin")){ 
      System.out.print(""); 
      tryAgain = false; 
     } 
     if(!pass.equals("admin")){ 
      JOptionPane.showMessageDialog(null, "Try again! Invalid password!","Error Logging-In", JOptionPane.ERROR_MESSAGE); 
     tryAgain = true; 
     System.out.print ("\f"); 
     } 
}while(tryAgain); 

     System.out.print("\n\n\t\tCashier: " +userNamePoe); 
     System.out.print("       "+sanKaKain); 
     System.out.print("\n\t\tCustomer Name: " +customerNamePoe); 

     SubokUlit j = new SubokUlit(); //Supposedly, once the selecting of meal is done, it should now go to the next part wherein the total bill will display and asks the user how much is his cash. 


    int lahatNgOrderMo = total + total1; 
    double multiplierVat = 0.12; 
    double vatCollected = lahatNgOrderMo * multiplierVat; 
     System.out.print("\n\n\tYour total bill is: "+lahatNgOrderMo); // Displays the bill 
     System.out.print("\n\tCash Tendered: "); // Asks the user how much is his cash 
    double cashTendered = inp.nextInt(); 
    double sukliMo = cashTendered - lahatNgOrderMo; 


     System.out.print("\n\n\t\t       MANG INASAL"); 
     System.out.print("\n\t\t      BLUMENTRITT BRANCH"); 
     System.out.print("\n\t\t   #1631-1633 BLUMENTRITT ST.,"); 
     System.out.print("\n\t\t   STA CRUZ. MANILA 0000"); 
     System.out.print("\n\t\t    (932) 885-5844\n"); 
     System.out.print("\n\t\t   Operated by: R L YU"); 
     System.out.print("\n\t\t   TIN 202-161-017-000 VAT"); 
     System.out.print("\n\t\t  ACC. NO.: 050-204079836-000019"); 
     System.out.print("\n\t\t    Tel. #: (02)493-6801"); 
     System.out.print("\n\n\t\tCashier: " +userNamePoe); 
     System.out.print("\t\t STATION: 2"); 
     System.out.print("\n\t\t---------------------------------------------"); 
     System.out.print("\n\t\tO.R #: 84486"); 
     System.out.print("       "+sanKaKain); 
     System.out.print("\t\t\n    Customer Name: " +customerNamePoe); 
     System.out.print("      24"); 
     System.out.print("\n\t\t---------------------------------------------"); 
     System.out.print("\n\t\t     >>SETTLED<<\n\n"); 
     System.out.print(""+order1); 
     System.out.print(""+order2); 
     System.out.print("\n\n\t\tSUB TOTAL:        "+lahatNgOrderMo); 
     System.out.print("\n\t\tDELIVERY VAT:       0.00"); 
     System.out.print("\n\t\t          ======"); 
     System.out.print("\n\t\tAMOUNT DUE:        "+lahatNgOrderMo); 
     System.out.print("\n\n\t\tVAT 12% COLLECTED     "+vatCollected); 
     System.out.print("\n\n\t\tCASH Tendered:       "+cashTendered); 
     System.out.print("\n\t\t          ======"); 
     System.out.print("\n\t\tCHANGE:         "+sukliMo); 
     System.out.print("\n\t\t    >>Ticket #: 62<<"); 
     System.out.print("\n\t\t  Created: "); 
     System.out.print("\n\t\t  SETTLED: "); 
     System.out.print("\n\n\t\t*********************************************"); 
     System.out.print("\n\t\tTHIS SERVES AS AN OFFICIAL RECEIPT."); 
     System.out.print("\n\n\t\tFor Feedback: TEXT MIO467(Comments/ Suggest"); 
     System.out.print("\n\t\tions) and SEND to 0917-5941111 or CALL US"); 
     System.out.print("\n\t\tat 0917-5596258"); 
     System.out.print("\n\t\tEmail: [email protected]"); 
     System.out.print("\n\n\t\t  THANK YOU FOR DINING WITH US!"); 
     System.out.print("\n\n\t\t*********************************************"); 
     System.out.print("\n\t\tS/N: 120416ASL03/1105-6105-9230"); 
     System.out.print("\n\t\tDT S/N: 41-L6971 (P0S1)"); 
     System.out.print("\n\t\tPERMIT NO: 0412-031-125295-000"); 
     System.out.print("\n\t\tMIN: 120276752"); 


} 
} 
+2

問題出在哪裏?究竟是什麼 ?你有什麼嘗試?你困在哪裏?任何這些回答的問題都可以幫助我們。 – phadaphunk 2013-03-05 14:39:24

+0

@PhaDaPhunk actionListener中的邏輯不起作用。每次我點擊組合框中的一餐,它都不會執行我聲明的操作 – 2013-03-05 14:50:44

+0

@Angel Casi Montoya我編輯了我的答案。你可以嘗試輸入的代碼嗎? – Joetjah 2013-03-05 14:56:17

回答

1

您必須確保eventlistener已連接到您的JComboBox,因此應用程序知道發生什麼事情時該怎麼做。

在這種情況下,您想要附上ItemListener()

您可以使用這樣的事情:

combo.addItemListener(new ItemListener() { 

    @Override 
    public void itemStateChanged(ItemEvent e) { 
     System.out.println("Meal chosen: " + combo.getSelectedItem().toString()); 
     String optionalParameter = combo.getSelectedItem().toString(); 
     DoMethodWhatYouNeedToDoWhenYouSelectedSomething(optionalParameter); 
    } 
}); 

編輯:您的代碼應該是這樣的:

combo.addActionListener(new ItemListener(){ 

    @Override 
    public void itemStateChanged(ItemEvent e){ 
     String str = (String)combo.getSelectedItem(); 
     a = str; 
     if(a.equals("PM1 (Paa/ Spicy Paa with Thigh part)")){ 
      System.out.print ("\n\n\n\t\tYou have chosen Paborito Meal 1.\n"); 
      System.out.print ("\t\t\tPlease enter the quantity: "); 
      int quantity1 = inp.nextInt(); 
      total = quantity1 * 99; 
      order1 = quantity1 + " " + "PM1 " + "         " + total +"\n"; 
     } 
     else if(a.equals("PM2 (Pecho)")){ 
      System.out.print ("\n\n\n\t\tYou have chosen Paborito Meal 2.\n"); 
      System.out.print ("\t\t\tPlease enter the quantity: "); 
      int quantity2 = inp.nextInt(); 
      total1 = quantity2 * 99; 
      order2 = quantity2 + " " + "PM2 " + "         " + total1 +"\n"; 
     } 
    } 
}); 
+0

你的意思是說我必須切換到ItemListener而不是actionListener?請檢查我的代碼 – 2013-03-05 14:56:13

+0

是的,並將'actionPerformed'重命名爲'itemStateChanged'。檢查我的編輯:) – Joetjah 2013-03-05 14:57:18

+0

先生說,不是抽象的,並且不會覆蓋abstarct方法。我該怎麼辦? – 2013-03-05 15:01:24