大家好,我的代码是关于汉堡的餐厅菜单,我只是想结合复选框和单选按钮....我的输出是0.0由于某种原因,我不知道为什么...可以有人plz告诉我,我做错了什么????
代码:
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;
}
}
}
答案 0 :(得分:3)
您正在更新total
的值,但再也没有将该值分配给a
TextField
在两个侦听器类的itemStateChanged
方法中,在所有if
结束之后,设置a
的值,如a.setText("Total is "+total);
此外,您必须将a
移到方法之外,并将其作为实例变量,以便可以在其他地方访问。
建议:遵循命名约定总是很好。 a
或t
没有为您正在使用的组件提供有意义的名称。