2013-05-10 185 views
0

我有一個處理按鈕事件的問題。我正在創建一個程序,讓用戶選擇他們想要的披薩類型,程序計算比薩的價格。我有我的佈局設置,但是當選擇一箇中等比薩餅時,按鈕沒有處理正確的響應。任何人都可以給我一些建議嗎?我在過去的一個小時裏查看了我的代碼,而我似乎無法看到我正在製作的錯誤。這是我的代碼...Java按鈕處理程序

public class Prog9Frame extends JFrame implements ActionListener 
{ 
private JLabel title; 
private JLabel size; 
private JLabel toppings; 
private JComboBox crust; 
private JRadioButton mediumRadio; 
private JRadioButton largeRadio; 
private JRadioButton xLargeRadio; 
private JCheckBox pepperoniBox; 
private JCheckBox sausageBox; 
private JCheckBox mushroomsBox; 
private JCheckBox onionsBox; 
private JLabel total; 
private JTextField totalField; 
private JButton submit; 

public Prog9Frame() 
{ 
    super ("Pizzazz Pizza"); 
    setLayout(new BorderLayout(5,5)); 

    //north region 
    title = new JLabel ("Pizzazz Pizza", JLabel.CENTER); 
    add (title, BorderLayout.NORTH); 

    //west region 
    JPanel westPanel = new JPanel(); 
    westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.Y_AXIS)); 

    westPanel.add(Box.createRigidArea(new Dimension(25,1))); 
    size = new JLabel ("Size"); 
    westPanel.add(Box.createVerticalStrut((20))); 
    westPanel.add(size); 
    mediumRadio = new JRadioButton("Medium"); 
    westPanel.add(Box.createVerticalStrut(20)); 
    westPanel.add(mediumRadio); 
    largeRadio = new JRadioButton("Large "); 
    westPanel.add(Box.createVerticalStrut(20)); 
    westPanel.add(largeRadio); 
    xLargeRadio = new JRadioButton("X-Large "); 
    westPanel.add(Box.createVerticalStrut(20)); 
    westPanel.add(xLargeRadio); 
    add(westPanel, BorderLayout.WEST); 

    //center region 
    JPanel centerPanel = new JPanel(); 
    centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS)); 

    toppings = new JLabel ("Toppings"); 
    centerPanel.add(Box.createVerticalStrut((20))); 
    centerPanel.add(toppings); 
    pepperoniBox = new JCheckBox("Pepperoni"); 
    centerPanel.add(Box.createVerticalStrut((20))); 
    centerPanel.add(pepperoniBox); 
    sausageBox = new JCheckBox("Sausage"); 
    centerPanel.add(Box.createVerticalStrut((20))); 
    centerPanel.add(sausageBox); 
    mushroomsBox = new JCheckBox("Mushrooms"); 
    centerPanel.add(Box.createVerticalStrut((20))); 
    centerPanel.add(mushroomsBox); 
    onionsBox = new JCheckBox("Onions"); 
    centerPanel.add(Box.createVerticalStrut((20))); 
    centerPanel.add(onionsBox); 
    add(centerPanel, BorderLayout.CENTER); 

    //east region 
    JPanel eastPanel = new JPanel(); 
    eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS)); 
    eastPanel.add(Box.createHorizontalStrut(20)); 
    eastPanel.add(Box.createVerticalStrut(50)); 
    String[] crustStrings = { "Thin", "Regular", "Deep Dish" }; 
    JComboBox crust = new JComboBox(crustStrings); 
    eastPanel.add(crust); 
    eastPanel.add(Box.createVerticalStrut(200)); 
    add(eastPanel, BorderLayout.EAST); 

    //south region 
    JPanel southPanel = new JPanel(); 
    southPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); 

    JTextField totalField = new JTextField(10); 
    southPanel.add(totalField); 
    JButton submit = new JButton ("Submit"); 
    submit.addActionListener(this); 
    southPanel.add(submit); 
    add(southPanel, BorderLayout.SOUTH); 


} 
//handle button events 
public void actionPerformed(ActionEvent event) 
{ 
if (mediumRadio.isSelected()) 
    { 
     double pizzaMed = 7.95; 
     totalField.setText(new DecimalFormat("###00.00").format(pizzaMed)); 
     } 
    } 

} 

回答

1

更新您的南部地區代碼。您正在重新聲明totalField,它隱藏在 actionPerformed方法中。這就是它給出java.lang.NullPointerException的方法。

JTextField totalField = new JTextField(10); 

變化

this.totalField = new JTextField(10); 
+0

非常感謝!我知道我必須錯過簡單的事情。我非常感謝幫助 – Mike 2013-05-10 01:29:15

3

你通過在類的構造函數中重新聲明totalField變量來映射它。這將導致重新聲明的變量,即具有有效對象引用的變量僅在構造函數中可見,僅在聲明該變量的範圍內,並且將使類字段不構造,從而爲空。如果你嘗試使用它,你會得到一個NullPointerException或NPE。

解決方案不是在構造函數中重新聲明變量,而是在那裏初始化類字段(如果需要,可以在類中聲明)。

因此,而不是:

public class Foo { 
    private JTextField bar; 

    public Foo() { 
    JTextField bar = new JTextField(10); // re-declaring bar here 
    } 
} 

做:

public class Foo { 
    private JTextField bar; 

    public Foo() { 
    bar = new JTextField(10); // ** see the difference? ** 
    } 
} 
+0

+1挑好看... – MadProgrammer 2013-05-10 01:06:31

+0

@MadProgrammer:感謝 – 2013-05-10 01:06:40

+0

你能還提到,OP沒有實際註冊的'ActionListener'對其他任何東西那麼'提交按鈕:P – MadProgrammer 2013-05-10 01:07:27