Java 如何让用户只选择一个单选按钮?我如何为每一张票定价,让用户为他选择的票付费?

Java 如何让用户只选择一个单选按钮?我如何为每一张票定价,让用户为他选择的票付费?,java,netbeans,Java,Netbeans,这就是我到目前为止所做的,我尝试了添加按钮组,但它一直显示有一个错误 public class tram extends JFrame implements ActionListener, KeyListener { TextArea output = new TextArea(6, 30); JButton cancel = new JButton("Cancel"); JButton exit = new JButton("Exit"); JRadioButton Single = ne

这就是我到目前为止所做的,我尝试了添加按钮组,但它一直显示有一个错误

public class tram extends JFrame implements ActionListener, KeyListener {

TextArea output = new TextArea(6, 30);
JButton cancel = new JButton("Cancel");
JButton exit = new JButton("Exit");

JRadioButton Single = new JRadioButton("Single");
JRadioButton Double = new JRadioButton("Double");
JRadioButton ZoneA = new JRadioButton("Zone A");
JRadioButton ZoneA_B = new JRadioButton("Zone A&B");
我在这里编写了Button.Group,并更改了“newjradiobutton();”,但它不起作用 我不确定是否应该使用if语句,因为我找不到方法来执行它^。^”


对于JRadioButtons,您必须将它们添加到ButtonGroup,并将其中一个按钮设置为选中

下面是一个小例子:

JRadioButton b1 = new JRadioButton('option1);
b1.setSelected(true);

ButtonGroup g = new ButtonGroup();
g.add(b1);

JRadioButton
的文档所示:

单选按钮的一种实现——一个可以选择或选择的项目 取消选择,并向用户显示其状态。与 ButtonGroup对象创建一组按钮,其中只有一个 一次可以选择一个按钮。(创建一个ButtonGroup对象并使用 它的add方法将JRadioButton对象包括在组中


您必须首先创建一个
按钮组
,并在其中添加
JRadioButton

侧注:不要将变量大写。大写很重要,一般来说,编码会说明变量实际上是对方法的静态调用
JRadioButton b1 = new JRadioButton('option1);
b1.setSelected(true);

ButtonGroup g = new ButtonGroup();
g.add(b1);