2014-11-24 128 views
-6

这是我的一个学校项目的代码,我必须做一个配料列表,以放在比萨饼上,也可以选择一个大小。每一个都没问题,当我腌香肠,火腿,加了1个意大利辣香肠,当我剔菠萝时,加入了BlackOlives,洋葱,GreenPepper 0.5美元。然而,当我剔额外奶酪增加像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

'它增加了10.25或其他东西'。 *或者其他的东西*?你甚至不能说出确切的问题吗?我为什么会得到这个似曾相识的感觉,上周提出了这个问题的近乎完全相同的版本...... – LittleBobbyTables 2014-11-24 14:28:30

回答

1

你有这遍:

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

对于配料的最后一堆,你总是把监听到ExtraCheese,这意味着每个成分你选择是要增加奶酪价格。

相关问题