0

大家好,有人可以帮我完成我的程序吗?我有一个 JComboBox,里面有几个项目。我想要发生的是,每当用户单击组合框中的某个项目时,它应该显示其项目数量。例如,我点击 PM1 两次它应该显示数量为 2,如果我点击 PM4 五次它应该显示数量为 5。我的程序中有两个类,代码如下。顺便说一句,我还想在 CopyShowOrder 类中显示的项目旁边显示数量。任何帮助将不胜感激提前感谢和更多的力量!

CopyShow的代码:

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

public class CopyShow{

  private static String z  = "";
  private static String w  = "";
  private static String x  = "";
  JComboBox combo;
  private static String a  = "";

public CopyShow(){
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","Turon 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.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        String str = (String)combo.getSelectedItem();
        a = str;
        CopyShowOrder messageOrder1 = new CopyShowOrder();
        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")){
        CopyShowOrder messageShowMenu = new CopyShowOrder();
        messageShowMenu.ShowMo();
        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);
CopyShow j = new CopyShow();
    }

  public static String kuhaOrder()
 {
  return a;
  }

public static String kuhaUserName()
{
return z;
}

public static String kuhaCustomerName()
{
return w;
}

public static String kuhaSanKainPagkain()
{
return x;
}  
}

CopyShowOrder 的代码:

public class CopyShowOrder {

public void ShowMo(){
    String user = CopyShow.kuhaUserName();
    System.out.print("\n\n\t\tCashier: " +user);
    String dInDOut = CopyShow.kuhaSanKainPagkain();
    System.out.print("                          "+dInDOut);
    String customerName = CopyShow.kuhaCustomerName();
    System.out.print("\n\t\tCustomer Name: " +customerName);
}

public void ShowOrderPo() {
    String order = CopyShow.kuhaOrder();
    System.out.print("\t\t\t\t\n " +order);

}
}
4

1 回答 1

1

我不确定这是否是你想要的,但它就是这样......你可以有一个想法......

import java.awt.BorderLayout;


public class StackOverflow extends JDialog {

private final JPanel contentPanel = new JPanel();
private JTable table;
private JComboBox comboBox = new JComboBox();
private JButton button = new JButton("+");
private JButton button_1 = new JButton("-");
private JScrollPane scrollPane = new JScrollPane();
private List<String> list = new ArrayList<String>();

/**
 * Launch the application.
 */
public static void main(String[] args) {
    try {
        StackOverflow dialog = new StackOverflow();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Create the dialog.
 */
public StackOverflow() {
    setTitle("StackOverflow");
    setBounds(100, 100, 339, 228);

    comboBox.setModel(new DefaultComboBoxModel(new String[] {"ITEM 1", "ITEM 2", "ITEM 3", "ITEM 4", "ITEM 5", "ITEM 6"}));

    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(null);
    {

        scrollPane.setBounds(10, 43, 303, 131);
        contentPanel.add(scrollPane);
        {
            table = new JTable();
            table.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "Item", "Count"
                }
            ));
            scrollPane.setViewportView(table);
        }
    }

    //Gets the table model and clear it
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    model.setRowCount(0);

    //Add comboBox items to table
    for (int i = 0; i < comboBox.getItemCount(); i++) 
        model.addRow(new Object[] { comboBox.getItemAt(i) , 0 });


    comboBox.setBounds(10, 12, 203, 20);
    contentPanel.add(comboBox);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String selectedItem = (String) comboBox.getSelectedItem();
            for (int i = 0; i < table.getRowCount(); i++) {
                String tableItem = (String) table.getValueAt(i, 0);
                int count = (Integer) table.getValueAt(i, 1)+1;
                if (selectedItem.equals(tableItem)) {
                    table.setValueAt(count, i, 1);
                }
            }
        }
    });

    button.setBounds(223, 11, 41, 23);
    contentPanel.add(button);
    button_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String selectedItem = (String) comboBox.getSelectedItem();
            for (int i = 0; i < table.getRowCount(); i++) {
                String tableItem = (String) table.getValueAt(i, 0);
                int count = (Integer) table.getValueAt(i, 1)-1;
                if (selectedItem.equals(tableItem)) {
                    table.setValueAt(count, i, 1);
                }
            }
        }
    });

    button_1.setBounds(272, 11, 41, 23);
    contentPanel.add(button_1);
}

}

于 2013-02-28T15:18:51.783 回答