请您检查下面我的代码有什么问题。这是一个简单的订购系统,要求用户输入客户名称,收银员名称,是否进餐或取出密码。每次我在组合框中点击一次餐时,它会使我选择的餐点加倍
例如我点击它显示的PM3:
您的订单是: PM3(猪肉烧烤4件) PM3(猪肉烧烤4件)
但它应该是: 您的订单是: PM3(猪肉烧烤4件)
顺便提一下,我有2节课,我将在下面展示他们的代码。
班级代码显示:
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Show{
private static String z = "";
private static String w = "";
private static String x = "";
JComboBox combo;
private static String a = "";
public Show(){
String mgaPagkainTo[] = {"PM1 (Paa/ Spicy Paa with Thigh part)","PM2 (Pecho)","PM3 (Pork Barbeque 4 pcs.)","PM4 (Bangus Sisig)","PM5 (Pork Sisig)","PM6 (Bangus Inihaw)","SM1 (Paa)","SM2 (Pork Barbeque 2 pcs.)","Pancit Bihon","Dinuguan at Puto","Puto","Ensaladang Talong","Softdrinks","Iced Tea","Halo-Halo","Leche Flan","Truon Split"};
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.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
String str = (String)combo.getSelectedItem();
a = str;
ShowOrder messageOrder1 = new ShowOrder();
messageOrder1.ShowOrderPo();
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,250);
frame.setVisible(true);
}
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
boolean ulitinMoPows = true;
boolean tryAgain = true;
System.out.print("\nInput Customer Name: ");
String customerName = inp.nextLine();
w = customerName;
System.out.print("\nInput Cashier Name: ");
String user = inp.nextLine();
z = user;
do{
System.out.print("\nInput either Dine In or Take Out: ");
String dInDOut = inp.nextLine();
x = dInDOut;
if (x.equals("Dine In") || x.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")){
ShowOrder messageShowMenu = new ShowOrder();
messageShowMenu.ShowMenu();
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);
Show j = new Show();
}
public static String kuhaOrder()
{
return a;
}
public static String kuhaUserName()
{
return z;
}
public static String kuhaCustomerName()
{
return w;
}
public static String kuhaSanKainPagkain()
{
return x;
}
}
ShowOrder类的代码:
public class ShowOrder {
public void ShowMenu(){
String user = Show.kuhaUserName();
System.out.print("\n\n\t\tCashier: " +user);
String dInDOut = Show.kuhaSanKainPagkain();
System.out.print(" "+dInDOut);
String customerName = Show.kuhaCustomerName();
System.out.print("\n\t\tCustomer Name: " +customerName);
System.out.print("\t\t\t\n\nYour order is/ are: ");
}
public void ShowOrderPo() {
String order = Show.kuhaOrder();
System.out.print("\t\t\t\t\n " +order);
}
}
答案 0 :(得分:4)
您的ItemListener
代码正在执行两次所选的&取消选择状态变化。您需要检查监听器中的状态更改状态:
if (ie.getStateChange() == ItemEvent.SELECTED) {
...
或只是使用ActionListener
,您不希望检查状态更改。
答案 1 :(得分:2)
用户更改组合框中的选择。因此,第一个ItemEent被触发以告知先前选择的项目已被取消选择,并且第二个被触发以说明已经选择了新选择的项目。
我会使用ActionListener而不是ItemListener。