Java 如何为JRadioButton创建此ItemListener
我正在尝试对数组使用sizeP。使totBill等于(选定的数组元素)Margarita值。但它根本不会改变价值。好像听者没有做什么好事。 我的ItemListener或整个代码中是否有错误?任何帮助都可以。我只是一个三年级的大学生,因为我是自学成才,所以在这方面遇到了麻烦Java 如何为JRadioButton创建此ItemListener,java,swing,Java,Swing,我正在尝试对数组使用sizeP。使totBill等于(选定的数组元素)Margarita值。但它根本不会改变价值。好像听者没有做什么好事。 我的ItemListener或整个代码中是否有错误?任何帮助都可以。我只是一个三年级的大学生,因为我是自学成才,所以在这方面遇到了麻烦 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class tryyy extends JFrame implements
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class tryyy extends JFrame implements ItemListener{
final int [] margheritaPizza = {258, 450, 799, 999};
int totalBill = 0, sizeP = 0;
ButtonGroup pType = new ButtonGroup();
JRadioButton mPizza = new JRadioButton("Margherita Pizza");
ButtonGroup pSize = new ButtonGroup();
JRadioButton small = new JRadioButton("Small");
JRadioButton medium = new JRadioButton("Medium");
JRadioButton large = new JRadioButton("Large");
JRadioButton xl = new JRadioButton("Extra Large");
JLabel pizzaTypes = new JLabel ("Pizza Types");
JLabel sizes = new JLabel ("Sizes ");
JTextField totBill = new JTextField (5);
JPanel mainPanel = new JPanel();
JPanel pTypePanel = new JPanel();
JPanel pSizePanel = new JPanel();
JPanel sizePanel = new JPanel();
public tryyy(){
super ("PIZZA MENU");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(mainPanel);
add(pTypePanel);
add(sizePanel);
add(pSizePanel);
setLayout (new FlowLayout (FlowLayout.LEFT));
mainPanel.add(pizzaTypes);
pType.add(mPizza);
sizePanel.add(sizes);
pSize.add(small);
pSize.add(medium);
pSize.add(large);
pSize.add(xl);
pSizePanel.add(small);
pSizePanel.add(medium);
pSizePanel.add(large);
pSizePanel.add(xl);
add(totBill);
totBill.setText("$" + margheritaPizza[sizeP]);
totBill.setEnabled(true);
small.addItemListener(this);
medium.addItemListener(this);
large.addItemListener(this);
xl.addItemListener(this);
mainPanel.setLayout(new GridLayout(0,2));
pTypePanel.setLayout(new GridLayout(0,3));
pSizePanel.setLayout(new GridLayout(0,4));
sizePanel.setLayout(new GridLayout(1,0));
small.setSelected(true);
}
public static void main(String[] args) {
tryyy aFrame = new tryyy();
aFrame.setSize(325,500);
aFrame.setVisible(true);
}
@Override
public void itemStateChanged(ItemEvent e) {
Object source = e.getSource();
int select = e.getStateChange();
if(source == small)
if (select == ItemEvent.SELECTED)
sizeP = 0;
else if (source == medium)
if (select == ItemEvent.SELECTED)
sizeP = 1;
else if (source == large)
if (select == ItemEvent.SELECTED)
sizeP = 2;
else
if (select == ItemEvent.SELECTED)
sizeP = 3;
}
}
您的代码有点凌乱,但您的特定问题发生了,因为当按钮事件发生时,您没有更改ToBill文本。试试这个
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
Object source = e.getSource();
if(source == small){
sizeP = 0;
} else if (source == medium) {
sizeP = 1;
}else if (source == large) {
sizeP = 2;
}else {
sizeP = 3;
}
totBill.setText("$" + margheritaPizza[sizeP]);
}
}
需要考虑的几件事:
- 您没有将正确的文本重新分配给
中的itemstener
。您只是在更改JTextField
,但这对显示的文本字段没有影响pSize
- 始终使用大括号,否则您将遇到意外的行为。(在本例中,在if块中)
- 类名以大写字母开头
- 您可能需要重新查看布局,因为现在布局非常混乱。最后,您不应该为组件使用固定尺寸,而应该让布局管理器为您完成这项工作。(也许你可以看看这个网站)
- 对于“比萨饼”的选择,如果有更多的比萨饼,也许
更合适JComboBox
- 也许在单选按钮上使用
和ActionListener
方法更适合您。(但这可能是基于观点的,取决于您的进一步用例)setActionCommand()
public class Try extends JFrame implements ActionListener {
....
....
small.setActionCommand("$" + margheritaPizza[0]);
small.addActionListener(this);
medium.setActionCommand("$" + margheritaPizza[1]);
medium.addActionListener(this);
large.setActionCommand("$" + margheritaPizza[2]);
large.addActionListener(this);
xl.setActionCommand("$" + margheritaPizza[3]);
xl.addActionListener(this);
.....
@Override
public void actionPerformed(ActionEvent e) {
totBill.setText(e.getActionCommand());
}
非常感谢。我会记住这一点,并尽力按照你的建议去做。非常感谢,谢谢。我稍后会尝试这个,希望它能解决问题。非常感谢