操作组合框 [英] Manipulating a combo box

查看:54
本文介绍了操作组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人请给我一个程序示例,只需使用组合框和按钮就可以增加和减少值,没有像JDialog甚至JTable这样的其他东西。毕竟我只是一个java的新手,我很难理解复杂的程序,所以我需要一个很容易理解的非常简单的程序。



代码如下:



Would someone please give me an example of a program that increments and decrements values with just the use of combo box and buttons, no other stuffs like a JDialog or even JTable. I''m having a hard time trying to understand complicated programs after all I''m just a newbie in java, so I need a very simple one that is easy to understand.

the code is this:

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

    public class EditStack {
    private JComboBox comboBox = new JComboBox();
    private JButton button = new JButton("Add Order");
    private JButton button_1 = new JButton("Decrease Order");
    JComboBox combo;
    private static String z  = "";
    private static String w  = "";
    private static String x  = "";
    private static String a  = "";
    private int numClick = 0;
    
public EditStack() {

    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);
    panel.add(button);
    panel.add(button_1);
    frame.add(panel);

    combo.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        String str = (String)combo.getSelectedItem();
        a = str;
        EditLang messageOrder1 = new EditLang();
        messageOrder1.ShowOrderPo();
       }
    });

    button.addActionListener(new ActionListener() { //this is the part I'm stuck with
        public void actionPerformed(ActionEvent e) {
            String selectedItem = (String) comboBox.getSelectedItem(); // I want here to display the selected item in the combo box
            numClick++ // and then increment it whenever I clicked on the add button
        }
    }
    );
    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")){
        EditLang messageShowMenu = new EditLang();
        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);

EditStack j = new EditStack();
    }
  public static String kuhaOrder()
  {
  return a;
  }

  public static String kuhaUserName()
  {
  return z;
  }

  public static String kuhaCustomerName()
  {
  return w;
  }

  public static String kuhaSanKainPagkain()
  {
  return x;
  }  
}
//Class of EditLang:

public class EditLang {

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

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

}
}
//I want to do something like that. Any help will be appreciated thanks!

推荐答案

请不要在多个论坛中发布相同的问题。选择一个并坚持下去。
Please do not post the same question in multiple forums. Choose one and stick to it.


这篇关于操作组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆