【问题标题】:Java Button HandlerJava 按钮处理程序
【发布时间】:2013-05-10 01:01:56
【问题描述】:

我在处理按钮事件时遇到问题。我正在创建一个程序,让用户选择他们想要的比萨饼类型,程序计算比萨饼的价格。 I have my layout setup, however when a medium pizza is selected the button is not processing the proper response.谁能给我一些建议?在过去的一个小时里,我查看了我的代码,但我似乎看不到我正在犯的错误。这是我的代码...

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

}

【问题讨论】:

    标签: java swing actionlistener jradiobutton


    【解决方案1】:

    通过在类的构造函数中重新声明它来隐藏 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? **
       }
    }
    

    【讨论】:

    • 您能否还提到,除了submit 按钮之外,OP 实际上还没有注册ActionListener :P
    • @MadProgrammer:这可能就是他真正应该添加 ActionListener 的全部内容。
    • 致@NicRobertson(他删除了他的答案):我礼貌地不同意。 JRadioButtons 应添加到 ButtonGroup,但不应添加 ActionListener。只有当用户完成创建他们的披萨时按下的按钮,提交 JButton,应该有一个 ActionListener 添加到它。
    • 是的,我意识到了,我的错误。在上面的评论中指出之前,我没有看到提交按钮的动作监听器。
    • @MadProgrammer:哇,看来我的回答对于 OP 来说还不够具体。甚至没有评论。值得相信。
    【解决方案2】:

    更新您的南部地区代码。您正在重新声明隐藏在 actionPerformed 方法。这就是它给出 java.lang.NullPointerException 的方式。

    JTextField totalField = new JTextField(10);
    

    改成

    this.totalField = new JTextField(10);
    

    【讨论】:

    • 非常感谢!我知道我必须遗漏一些简单的东西。我非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 2012-04-12
    相关资源
    最近更新 更多