【问题标题】:combo box does not work properly [closed]组合框无法正常工作[关闭]
【发布时间】:2013-03-05 14:34:30
【问题描述】:

我这里有一个简单的订购系统,需要用户输入收银员姓名、客户姓名和密码。用户完成该过程后,它将显示一个包含不同餐点的组合框。

我想要发生的是,一旦我点击了一顿饭,它应该会显示我选择的饭菜的名称,并且应该执行我在 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: feedback@manginasal.com");
        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");


} 
}

【问题讨论】:

  • 问题出在哪里?究竟是什么 ?你试过什么?你被困在哪里了?回答这些问题中的任何一个都可以帮助我们。
  • @PhaDaPhunk actionListener 中的逻辑不起作用。每次我单击组合框中的一顿饭时,它都不会执行我声明的操作
  • @Angel Casi Montoya 我已经编辑了我的答案。你可以试试那里输入的代码吗?

标签: java swing jcombobox listeners


【解决方案1】:

您必须确保将事件监听器附加到您的 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"; 
        }
    } 
});

【讨论】:

  • 你的意思是说我必须切换到ItemListener而不是actionListener?请查看我上面的代码
  • 是的,并将actionPerformed 重命名为itemStateChanged。检查我的编辑:)
  • 先生,它说 不是抽象的,也不会覆盖抽象方法。我该怎么办?
  • 你在哪一行得到错误?
  • 上线combo.addActionListener(new ItemListener(){
猜你喜欢
  • 2012-05-16
  • 1970-01-01
  • 2014-06-09
  • 2019-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-15
相关资源
最近更新 更多