Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 组合框不能正常工作_Java_Swing_Jcombobox_Listeners - Fatal编程技术网

Java 组合框不能正常工作

Java 组合框不能正常工作,java,swing,jcombobox,listeners,Java,Swing,Jcombobox,Listeners,我这里有一个简单的订购系统,需要用户输入出纳姓名、客户姓名和密码。用户完成该过程后,将显示一个包含不同膳食的组合框 我想做的是,一旦我单击了一顿饭,它就会显示我选择的那顿饭的名称,并执行我在actionlistener中声明的操作 这个程序编译得很好,但是我想实现的逻辑不起作用,我正在尽力解决这个问题,但是我想我自己做不到,请帮助我,谢谢 import java.util.Scanner; import javax.swing.*; import java.awt.*;

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

我想做的是,一旦我单击了一顿饭,它就会显示我选择的那顿饭的名称,并执行我在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");


} 
}
import java.util.Scanner;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类SubokUlit{
私人JComboBox组合;
私人字符串a=“”;
私有静态int应答;
私有静态整数总计=0;
私有静态整数total1=0;
私有静态int-wew=0;
私有静态字符串order1=“”;
私有静态字符串order2=“”;
专用静态扫描仪inp=新扫描仪(System.in);
公共亚库利特(){
字符串MGAPAGKANTO[]={“PM1(Paa/带大腿部分的辣味Paa)”,“PM2(Pecho)”;
JFrame=新的JFrame(“Mang Insal订购系统”);
JPanel面板=新的JPanel();
组合=新的JComboBox(MGAPAGKANTO);
组合。挫折背景(颜色。灰色);
组合。设置前景(颜色。红色);
面板。添加(组合);
框架。添加(面板);
addActionListener(新建ActionListener(){//此处的逻辑不起作用
已执行的公共无效操作(操作事件e){
String str=(String)combo.getSelectedItem();
a=str;
如果(a.equals(“PM1(Paa/辣味Paa带大腿部分)”){//如果我在组合框中选择PM1,它应该执行以下操作。但它不起作用
System.out.print(“\n\n\n\t\t您选择了Paborito套餐1。\n”);
System.out.print(“\t\t\t请输入数量:”);
int quantity1=inp.nextInt();
总数=数量1*99;
order1=quantity1++“PM1++”总计+“\n”;
}
否则,如果(a.equals(“PM2(Pecho)”){//同样的事情也应该发生在PM2中
System.out.print(“\n\n\n\t\t您选择了Paborito套餐2。\n”);
System.out.print(“\t\t\t请输入数量:”);
int quantity2=inp.nextInt();
total1=数量2*99;
order2=数量2+“”+“PM2”+“”+total1+“\n”;
}
} 
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。设置尺寸(300100);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
扫描仪inp=新扫描仪(System.in);
字符串userNamePoe=“”;
字符串customerNamePoe=“”;
字符串sanKaKain=“”;
布尔ulitinmoows=true;
布尔值tryAgain=true;
系统输出打印(“\n输入客户名称:”);
字符串customerName=inp.nextLine();
customerNamePoe=customerName;
系统输出打印(“\n输入出纳姓名:”);
字符串user=inp.nextLine();
userNamePoe=用户;
做{
System.out.print(“\n输入进餐或外卖:”);
字符串dInDOut=inp.nextLine();
sanKaKain=dInDOut;
如果(sanKaKain.equals(“进餐”)| | sanKaKain.equals(“外卖”)){
系统输出打印(“”);
ulitinmoows=false;
}
否则{
JOptionPane.showMessageDialog(null,“重试!请仅输入进餐或外卖!”,“错误”,JOptionPane.Error_消息);
ulitinmoows=true;
System.out.print(“\f”);
}
}而(ulitinmoows),;
做{
系统输出打印(“\n输入密码:”);
字符串pass=inp.nextLine();
如果(通过等于(“管理”)){
系统输出打印(“”);
tryAgain=false;
}
如果(!pass.equals(“admin”)){
JOptionPane.showMessageDialog(null,“重试!密码无效!”,“登录错误”,JOptionPane.Error_消息);
tryAgain=true;
System.out.print(“\f”);
}
}while(tryAgain);
System.out.print(“\n\n\t\tCashier:+userNamePoe”);
系统输出打印(“+sanKaKain”);
System.out.print(“\n\t\t客户名称:“+customerNamePoe”);
SubokUlit j=new SubokUlit();//假定,一旦完成了对膳食的选择,现在应该转到下一部分,在下一部分中,将显示账单总额,并询问用户他的现金是多少。
int Lahatengordermo=总计+总计1;
双倍增值税=0.12;
双倍增值税=Lahatengordermo*倍增增值税;
System.out.print(“\n\n\t您的账单总额为:“+lahatengordermo”);//显示账单
System.out.print(“\n\tCash Tendered:”);//询问用户他的现金是多少
double cashtended=inp.nextInt();
双层苏克利莫=现金投标-拉哈坦戈尔德莫;
System.out.print(“\n\n\t\t MANG INASAL”);
System.out.print(“\n\t\t BLUMENTRITT分支”);
System.out.print(“\n\t\t#1631-1633 BLUMENTRITT街”);
System.out.print(“\n\t\t STA CRUZ.MANILA 0000”);
系统输出打印(“\n\t\t(932)885-5844\n”);
System.out.print(“\n\t\t由:R L YU操作”);
系统输出打印(“\n\t\t TIN 202-161-017-000 VAT”);
系统输出打印(“\n\t\t附件编号:050-204079836-000019”);
系统输出打印(“\n\t\t电话:(02)493-6801”);
System.out.print(“\n\n\t\tCashier:+userNamePoe”);
系统输出打印(“\t\t站:2”);
System.out.print(“\n\t\t-----------------------------------------------”;
System.out.print(“\n\t\tO.R#:84486”);
系统输出打印(“
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"; 
        }
    } 
});