2014-11-24 126 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,這意味着每個成分你選擇是要增加奶酪價格。

相關問題