-4

大家好,我的代码是关于汉堡的餐厅菜单的代码,我只是想将复选框和单选按钮结合起来......我的输出由于某种原因是 0.0,我不知道为什么......有人可以告诉我我我做错了吗???

代码 :

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.*;

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

public class RMenu extends JFrame
{
final double TAX = 0.06;
private JTextField t, a;
private JCheckBox Lettuce, Mustard ,Cheese , Tomatoes , Onions , Jalapeno , Pickels;
private JRadioButton Ham, Steak , Chicken , Pork;
private ButtonGroup  g;
double total = 0.00;


public RMenu()
{
super( "Menu of our Restaurant for Burger");
Container c = getContentPane();
c.setLayout(new FlowLayout());

t = new JTextField ( " Welcome to our burger restauraunt",26);
t.setFont( new Font( "TimesRoman", Font.PLAIN, 10 ) );
t.setEditable(false);
c.add(t);

Lettuce = new JCheckBox( "Lettuce");
c.add(Lettuce);
Mustard = new JCheckBox("Mustard");
c.add(Mustard);
Cheese =  new JCheckBox("Cheese");
c.add(Cheese);
Tomatoes = new JCheckBox("Tomatoes");
c.add(Tomatoes);
Onions = new JCheckBox("Onions");
c.add(Onions);
Jalapeno = new JCheckBox("Jalapeno");
c.add(Jalapeno);
Pickels = new JCheckBox("Pickels");
c.add(Pickels);
Ham = new JRadioButton("Ham",false);
c.add(Ham);
Steak = new JRadioButton("Steak",false);
c.add(Steak);
Chicken = new JRadioButton("Chicken",false);
c.add(Chicken);
Pork = new JRadioButton("Pork",false);
c.add(Pork);

g = new ButtonGroup();
g.add(Ham);
g.add(Steak);
g.add(Chicken);
g.add(Pork);

CheckBoxHandler handler1 = new CheckBoxHandler();
Lettuce.addItemListener(handler1);
Mustard.addItemListener(handler1);
Cheese.addItemListener(handler1);
Tomatoes.addItemListener(handler1);
Onions.addItemListener(handler1);
Jalapeno.addItemListener(handler1);
Pickels.addItemListener(handler1);

RadioButtonHandler handler = new RadioButtonHandler();
Ham.addItemListener(handler);
Steak.addItemListener(handler);
Chicken.addItemListener(handler);
Pork.addItemListener(handler);

setSize(400,400);
setVisible(true);

a = new JTextField(" Your total amount is : $" + total,20);
a.setEditable(false);
c.add(a);
}
public static void main(String[] args) 
{
    RMenu app = new RMenu();
    app.addWindowListener
      (
         new WindowAdapter() 
         {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );

}



private class CheckBoxHandler implements ItemListener 
{

     public void itemStateChanged( ItemEvent e )
     {

         if (e.getSource() == Lettuce)
             if ( e.getStateChange() == ItemEvent.SELECTED )
                 total += 0.95;
        if (e.getSource() == Mustard)
            if ( e.getStateChange() == ItemEvent.SELECTED )
                total += 0.85;
         if (e.getSource() == Cheese)
             if ( e.getStateChange() == ItemEvent.SELECTED )
                 total+= 0.45;
         if (e.getSource() == Tomatoes)
             if ( e.getStateChange() == ItemEvent.SELECTED )
                 total += 1.05;
         if (e.getSource() == Onions)
             if ( e.getStateChange() == ItemEvent.SELECTED )
                 total += 0.55;
         if (e.getSource() == Jalapeno)
             if ( e.getStateChange() == ItemEvent.SELECTED )
                 total += 0.35;
         if (e.getSource() == Pickels)
             if ( e.getStateChange() == ItemEvent.SELECTED )
                 total +=0.65;

     }



}

private class RadioButtonHandler implements ItemListener 
{
      public void itemStateChanged( ItemEvent e )
      {
         if (e.getSource()== Ham)
             total = total + 4.95 + 4.95*TAX;
         else if (e.getSource() == Steak)
             total = total + 5.05 + 5.05*TAX;
         else if (e.getSource() == Chicken)
             total = total + 3.75 + 3.75 *TAX;
         else if (e.getSource()== Pork)
             total = total + 6.25 + 6.25*TAX;

      }

  }
}
4

1 回答 1

3

您正在更新 的值,total但再也不会将该值分配给两个侦听器类的a TextField InitemStateChanged方法,然后在所有ifs 结束后,设置 的值a,就像a.setText("Total is "+total); 另外,您将不得不移出a方法并将其作为实例变量,以便它可以在其他地方访问。

建议:遵循命名约定总是好的。a或者t没有为您正在使用的组件提供有意义的名称。

于 2013-06-13T15:29:19.120 回答