使用JComboBox计算更新JTextField

时间:2013-04-13 01:32:17

标签: java swing jtextfield jcombobox

在我的代码中,只要有人点击任一组合框中的选项,我就会尝试在JTextField总计价格计算中发布。我不想要名字,即“辣香肠”,而是实际的总计算。现在我必须单击文本框并按Enter键更新数据。

如何使用JTextField计算更新JComboBox

import java.awt.* ;

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

 class JPizza extends JFrame implements ActionListener
 {
    private static final long serialVersionUID = 1L;

    public static void main(String[] args)
        {
            JPizza myFrame = new JPizza();
            myFrame.setVisible(true);
        }

    private static final int WIDTH = 250;
    private static final int HEIGHT = 300;
    private static final int X_ORIGIN = 200;
    private static final int Y_ORIGIN = 100;

      private static final double smallPizzaPrice = 7.00;
      private static final double mediumPizzaPrice = 9.00;
      private static final double largePizzaPrice = 11.00;
      private static final double extraLargePizzaPrice = 14.00;
      private static final int smallPizzaSize = 0;
      private static final int mediumPizzaSize = 1;
      private static final int largePizzaSize = 2;
      private static final int extraLargeSize = 3;

      private static final double cheese = 0.0, pepperoni = 1.00, peppers = 1.00, sausage = 1.00, olives = 1.00;
      private static final int cheesePlace = 0, pepperoniPlace = 1, peppersPlace = 2, sausagePlace = 3, olivesPlace = 4;

      int states = 0 ;
      double totalPrice = 0.0;   
      double pizzaSizePrice = smallPizzaPrice ;
      double toppingPrice = 0.0;

    //Toppings & Panel
    JComboBox<String> pizzaTopping;
    JPanel comboboxPanel2;

    //Pizza Size & Panel
    JComboBox<String> pizzaSize;
    JPanel comboboxPanel;

    //Price Display Field and Panel
    JTextField calculate;
    JPanel messagePanel;

    public JPizza()
    {
        super("Tower of Pizza");      

        JLabel title = new JLabel("Tower of Pizza");
        add(title);

        pizzaSize = new JComboBox<String>();
        pizzaSize.addItem( "Small" );
        pizzaSize.addItem( "Medium" );
        pizzaSize.addItem( "Large" );
        pizzaSize.addItem( "Extra Large" );
        pizzaSize.setSelectedIndex(0);
        pizzaSize.addActionListener(this);

        pizzaTopping = new JComboBox<String>();
        pizzaTopping.addItem( "Cheese" );
        pizzaTopping.addItem( "Pepperoni" );
        pizzaTopping.addItem( "Peppers" );
        pizzaTopping.addItem( "Sausage" );
        pizzaTopping.addItem( "Olives" );
        pizzaTopping.setSelectedIndex(0);
        pizzaTopping.addActionListener(this);

        calculate = new JTextField(10);   
        calculate.addActionListener(this); 

        Container contentPane = getContentPane();
        GridLayout contentpaneLayout = new GridLayout(5,0,10,10);
        contentPane.setLayout(contentpaneLayout);

        comboboxPanel = new JPanel();
        GridLayout comboboxPanelLayout = new GridLayout(2,0);
        comboboxPanel.setLayout(comboboxPanelLayout);
        comboboxPanel.add(pizzaSize);
        contentPane.add(comboboxPanel);

        comboboxPanel2 = new JPanel();
        GridLayout comboboxPanelLayout1 = new GridLayout(2,0);
        comboboxPanel2.setLayout(comboboxPanelLayout1);
        comboboxPanel2.add(pizzaTopping);
        contentPane.add(comboboxPanel2);

        messagePanel = new JPanel();
        GridLayout messagePanelLayout = new GridLayout(2,0);
        messagePanel.setLayout(messagePanelLayout);
        messagePanel.add(calculate);
        contentPane.add(messagePanel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT);
        setResizable(false);
    }


    public void actionPerformed(ActionEvent e)
    {            
        Object source = e.getSource();

                 int pizzaSiz = (int)pizzaSize.getSelectedIndex();  

                 if (source == calculate){

                 if (pizzaSiz == smallPizzaSize){                  
                    pizzaSizePrice = smallPizzaPrice;                   
                  }
                 else if (pizzaSiz == mediumPizzaSize){
                      pizzaSizePrice = mediumPizzaPrice ;

                  }
                 else if (pizzaSiz == largePizzaSize){
                      pizzaSizePrice =  largePizzaPrice;
                  }
                 else if (pizzaSiz == extraLargeSize){
                     pizzaSizePrice =  extraLargePizzaPrice;
                  }

                 int pizzaTop = (int)pizzaTopping.getSelectedIndex(); 

                 if(pizzaTop == cheesePlace ){
                     toppingPrice = cheese;
                 }
                 else if(pizzaTop == pepperoniPlace){
                     toppingPrice = pepperoni;
                 }
                 else if(pizzaTop == peppersPlace){
                     toppingPrice = peppers;
                 }
                 else if(pizzaTop == sausagePlace){
                     toppingPrice = sausage;
                 }
                 else if(pizzaTop == olivesPlace){
                     toppingPrice = olives;      
                 }

                 totalPrice = pizzaSizePrice;  
                 totalPrice +=toppingPrice;
                 calculate.setText("Total Price: $" + totalPrice);             
             }

    }
 } 

1 个答案:

答案 0 :(得分:2)

您通过限制actionPerformed对if测试执行的操作来有意限制您的代码:

if (source == calculate){

摆脱它,该方法将在调用actionPerformed时进行计算。您最好确保在尝试计算之前两个组合框都有有效选择。您可以通过获取组合框的选定索引并测试它们是否> = 0(不是-1)来测试它们。