有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java操作JComboBox

大家好,有人能帮我做节目吗?我有一个JComboBox,里面有几个项目。我想做的是,每当用户单击组合框中的某个项目时,它都应该显示其项目数量。例如,我点击PM1两次,它的数量应该显示为2,如果我点击PM4五次,它的数量应该显示为5。我有两个课程涉及到我的程序和代码如下。顺便说一下,我还想在CopyShowOrder类中显示的项目旁边显示数量。我们将非常感谢您的帮助,请提前向我们致谢

文案代码:

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

}
}

共 (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);
    }
    

    }