0

ボタンイベントの処理に問題があります。ユーザーが希望するピザの種類を選択し、プログラムがピザの価格を計算できるようにするプログラムを作成しています。レイアウトを設定しましたが、ミディアム ピザを選択すると、ボタンが適切な応答を処理しません。誰でも私にアドバイスをもらえますか?過去 1 時間、自分のコードに目を通しましたが、自分が犯しているエラーが見当たらないようです。これが私のコードです...

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));
        }
    }

}
4

2 に答える 2

3

クラスのコンストラクターで変数を再宣言することで、totalField 変数を隠しています。これにより、再宣言された変数、つまり有効なオブジェクト参照を持つ変数が、コンストラクターでのみ、宣言されたスコープ内でのみ表示され、クラス フィールドが構築されずに null のままになります。使用しようとすると、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? **
   }
}
于 2013-05-10T01:06:07.533 に答える
1

南部地域のコードを更新します。actionPerformed メソッドに隠れている totalField を再宣言しています。そうすると、java.lang.NullPointerException が発生します。

JTextField totalField = new JTextField(10);

への変更

this.totalField = new JTextField(10);
于 2013-05-10T01:20:34.843 に答える