java - 操作 JComboBox

标签 java swing jcombobox

大家好,有人可以帮助我完成我的程序吗?我有一个 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);

}
}

最佳答案

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

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

}

关于java - 操作 JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15138398/

相关文章:

java - 为什么 JAX-WS 需要包装类?

java - 所有 JDK 中的 java 编译器都一样吗?

java - Spring Data 和 JPA 的编译时错误

java - java中将单词的第一个字母更改为大写

java - 如果没有 EventQueue 的 invokeLater,第一个 UserTypedString 不会在 keyTyped (keyListener) 中被识别

java - 如何在 Swing 中更新 JComboBox 实例?

java - Eclipse LibGDX 文件夹共享

java - 方法 hibernate 直到事件触发

java - Java 中的 repaint() 不会立即返回 "re-paint"吗?

java - 重绘后如何继续在图像上绘画?