简体   繁体   English

文字显示两次,而不是一次

[英]Text displays two times instead of once

Would you please checks what is wrong with my code below. 您能否检查下面我的代码有什么问题。 It is a simple ordering system that requires the user to input a customer name, cashier name, whether it is dine in or take out and password. 这是一个简单的订购系统,要求用户输入客户名称,收银员名称(无论是用餐还是外卖)和密码。 Every time I click a meal once in the combo box it doubles the meal I selected 每次我在组合框中单击一餐时,它都会将我选择的餐点加倍

for example I clicked PM3 it displays: 例如,我单击了PM3,它显示:

Your is order is/are: PM3 (Pork Barbeque 4 pcs.) 您的订单是:PM3(猪肉烧烤4个)。
PM3 (Pork Barbeque 4 pcs.) PM3(猪肉烧烤4个)

but it should be: Your is order is/are: PM3 (Pork Barbeque 4 pcs.) 但是应该是:您的订单是:PM3(猪肉烧烤4个)。

By the way I have 2 classes I will show their codes below. 顺便说一下,我有2个课程,我将在下面显示其代码。

Code of class Show: 显示代码:

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;
}  
}

Code of class ShowOrder: 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);

}
}

Your ItemListener code is being executed twice for both the selected & deselected state changes. 您的ItemListener代码针对选定和未选定状态更改执行两次。 You need to check the state change status in your listener: 您需要在侦听器中检查状态更改状态:

if (ie.getStateChange() == ItemEvent.SELECTED) {
  ...

or simply use an ActionListener is you don't wish to check state change. 或只是使用ActionListener而不希望检查状态更改。

See: How to Write an Item Listener 请参阅: 如何编写项目侦听器

The user changes the selection in the combo box. 用户在组合框中更改选择。 So a first ItemEent is fired to tell that the previously selected item has been de-selected, and a second one is fired to say that the newly-selected item has been selected. 因此,第一个ItemEent被触发以告知先前选择的项目已被取消选择,第二个ItemEent被触发以说新选择的项目已被选择。

I would use an ActionListener instead of an ItemListener. 我将使用ActionListener而不是ItemListener。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RotateAnimation旋转两次,而不是旋转一次 - RotateAnimation rotates two times instead of just once 我不明白为什么我的 while 循环循环了两次而不是一次 - I cannot figure out why my while loop is looping two times instead of just once 为什么文本显示在按钮中而不是文本视图中? - Why text displays in a button instead of textview? Actionbar中的ProgressBar显示文本而不是布局文件 - ProgressBar in Actionbar displays text instead of the layout file 为什么我一次只调用一次 TextViews 中的文本会更改两次? - Why is the text in TextViews changing two times when I am calling it only once at a time? 单击两次而不是一次 - Clicking on item two times instead on one JButton打印多次而不是一次。 为什么? - JButton Prints Multiple Times Instead of Once. Why? Android通知会打开多次,而不是只打开一次 - Android notification opens multiple times instead of only once 为什么而不是两个不同的AsyncTasks执行两次? - Why instead of two different AsyncTasks one executes two times? 单击一次按钮后,代码在firebase onDatachange中执行两次 - Code executing two times in firebase onDatachange when button clicked once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM