gpt4 book ai didi

Java 按钮处理程序

转载 作者:行者123 更新时间:2023-12-01 18:49:02 25 4
gpt4 key购买 nike

我在处理按钮事件时遇到问题。我正在创建一个程序,让用户选择他们想要的披萨类型,然后该程序计算披萨的价格。我有我的布局设置,但是当选择中等披萨时,按钮无法处理正确的响应。谁能给我一些建议吗?在过去的一个小时里我已经检查了我的代码,但我似乎看不到我所犯的错误。这是我的代码...

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

}

最佳答案

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

关于Java 按钮处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16473590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com