0

こんにちはみんな私のプログラムを手伝ってくれませんか?私はJComboBoxを持っていて、その中にいくつかのアイテムがあります。私がしたいのは、ユーザーがコンボボックス内の特定のアイテムをクリックするたびに、そのアイテムの数量を表示する必要があるということです。たとえば、PM1を2回クリックすると、2が表示され、PM4を5回クリックすると、5が表示されます。私のプログラムには2つのクラスがあり、コードは以下のとおりです。ちなみに、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 に答える