我的计划不起作用,我不知道为什么?

时间:2014-11-24 14:26:01

标签: java jbutton jlabel jradiobutton

这是我的学校项目代码,我必须列出披萨上的配料列表,并选择尺寸。每一个都没关系,当我勾选香肠,火腿,意大利辣香肠1 $时,当我勾选Pineapple时,BlackOlives,Onion,GreenPepper 0.5 $被添加。然而,当我勾选Extra Cheese时,它会添加10.25或其他东西,而尺寸按钮似乎也不起作用。请向我解释一下D:

import javax.swing.JFrame;
import javax.swing.JRadioButton;
import java.awt.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class RealPizza2 extends JFrame {
    public static void main(String args[]) {

    JOptionPane.showMessageDialog(null, "Welcome to Your Pizza Place! :)", "Your Pizza Place", JOptionPane.PLAIN_MESSAGE);
    JOptionPane.showMessageDialog(null, "Hi, Please choose from our selection of toppings", "Your Pizza Place", JOptionPane.PLAIN_MESSAGE);
    RealPizza2 go = new RealPizza2 ();
    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    go.setSize(300,450);
    go.setVisible(true);

}
private JLabel PS;
private JLabel CT;
private JButton Next;
private JRadioButton Sausage;
private JRadioButton Pepperoni;
private JRadioButton Ham;
private JRadioButton ExtraCheese;
private JRadioButton Pineapple;
private JRadioButton BlackOlives;
private JRadioButton Onion;
private JRadioButton GreenPepper;
private JRadioButton ExtraLargePizza;
private JRadioButton LargePizza;
private JRadioButton MediumPizza;
private JRadioButton SmallPizza;
private ButtonGroup group;

double PizzaPrice;

    public RealPizza2(){
        super("Pizza Menu");
        setLayout(new FlowLayout());
        CT = new JLabel("Choose your Pizza toppings!");
        CT.setFont(new Font("serif", Font.BOLD, 20));

        PS = new JLabel("Choose your Pizza Size!");
        PS.setFont(new Font("serif", Font.BOLD, 20));

        PizzaPrice = 5;


        Sausage = new JRadioButton ("Would you like Sausage", false);
        Sausage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                PizzaPrice ++;
        }
        });
        Pepperoni = new JRadioButton ("Would you like Pepperoni", false);
        Pepperoni.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                PizzaPrice ++;
        }
        });
        Ham = new JRadioButton ("Would you like Ham", false);
        Ham.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                PizzaPrice ++;
        }
        });
        ExtraCheese = new JRadioButton ("Would you like ExtraCheese", false);
        ExtraCheese.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                PizzaPrice ++;
        }
        });
        Pineapple = new JRadioButton ("Would you like Pineapples", false);
        Pineapple.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                PizzaPrice += 0.5;
        }
        });
        BlackOlives = new JRadioButton ("Would you like Black Olives", false);
        BlackOlives.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                PizzaPrice += 0.5;
        }
        });
        Onion = new JRadioButton ("Would you like Onions", false);
        Onion.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                PizzaPrice += 0.5;
        }
        });
        GreenPepper = new JRadioButton ("Would you like Green Pepper", false);
        GreenPepper.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                PizzaPrice += 0.5;
        }
        });

        ExtraLargePizza = new JRadioButton ("Extra Large Pizza");
        ExtraCheese.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                PizzaPrice += (PizzaPrice * 0.75);
        }
        });
        LargePizza = new JRadioButton("Large Pizza", false);
        ExtraCheese.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                PizzaPrice += (PizzaPrice * 0.5);
        }
        });
        MediumPizza = new JRadioButton("Medium Pizza", false);
        ExtraCheese.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                PizzaPrice += (PizzaPrice * 0.25);
        }
        });
        SmallPizza = new JRadioButton("Small Pizza", false);
        ExtraCheese.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                PizzaPrice += (PizzaPrice * 0.10);
        }
        });

        Icon P = new ImageIcon(getClass().getResource("X.png"));
        JButton Next = new JButton ("To Receipt!", P);
        Next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
        {
                JOptionPane.showMessageDialog(null,"$" + PizzaPrice);
        }
        });

        group = new ButtonGroup();
        group.add(ExtraLargePizza);
        group.add(LargePizza);
        group.add(MediumPizza);
        group.add(SmallPizza);


        add(CT);
        add(Sausage);
        add(Pepperoni);
        add(Ham);
        add(ExtraCheese);
        add(Pineapple);
        add(BlackOlives);
        add(Onion);
        add(GreenPepper);
        add(PS);
        add(ExtraLargePizza);
        add(LargePizza);
        add(MediumPizza);
        add(SmallPizza);
        add(Next);

    }
  }

1 个答案:

答案 0 :(得分:1)

你已经把它全部搞定了:

LargePizza = new JRadioButton("Large Pizza", false);
ExtraCheese.addActionListener(new ActionListener() {
^^^^^^^^^^^

对于最后一堆成分,你总是把Listener放到ExtraCheese上,这意味着你选择的每种成分都会增加奶酪的价格。