[英]How can I let the user choose only one radiobutton? and how do I put a price for each one of them and let the user pay for the ticket he choosed?
This is what ive done so far I tried the add the ButtonGroup but it keeps showing me that there is an error T_T这是我到目前为止所做的我尝试添加 ButtonGroup 但它一直显示有错误 T_T
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");
I wrote the Button.Group here with changing the "new JRadioButton();"我在这里通过更改“new JRadioButton();”编写了 Button.Group。 and it didnt work I am not sure if I should use if statement because I could not find a way to do it ^.^"它没有用我不确定我是否应该使用 if 语句,因为我找不到方法来做到这一点 ^.^”
public tram(){
JLabel title = new JLabel("Please select the type of ticket you wish to purchase");
setLayout(new BorderLayout());
setSize(450, 300);
setTitle("Redwich Tram");
JFrame frm = new JFrame();
JPanel top = new JPanel();
top.setBackground(Color.white);
top.add(title);
title.setFont(new Font("Courier", Font.BOLD, 12));
add("North", top);
JPanel middle = new JPanel();
middle.setBackground(Color.WHITE);
top.add(new JLabel("Select an option by clicking one of the buttons"));
add("Center", middle);
middle.add(Single, BorderLayout.NORTH);
Single.setBackground(Color.white);
middle.add(Double, BorderLayout.CENTER);
Double.setBackground(Color.white);
middle.add(ZoneA, BorderLayout.SOUTH);
ZoneA.setBackground(Color.white);
middle.add(ZoneA_B, BorderLayout.SOUTH);
ZoneA_B.setBackground(Color.white);
middle.setBackground(Color.WHITE);
middle.add(output);
JPanel bottom = new JPanel();
bottom.setBackground(Color.white);
add("South", bottom);
bottom.add(cancel,"South");
cancel.setBackground(Color.white);
bottom.add(exit,"South");
exit.setBackground(Color.white);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new tram();
}
public void onRadioButtonClicked(View view) {
}
}
For JRadioButtons you have to add them to a ButtonGroup and set one of the buttons as selected.对于 JRadioButton,您必须将它们添加到 ButtonGroup 并将其中一个按钮设置为选中状态。
Here is the Oracle tutorial . 这里是Oracle教程。
Here's a small example:这是一个小例子:
JRadioButton b1 = new JRadioButton('option1);
b1.setSelected(true);
ButtonGroup g = new ButtonGroup();
g.add(b1);
As seen in the documentation of JRadioButton
:如JRadioButton
的文档中所示:
An implementation of a radio button -- an item that can be selected or deselected, and which displays its state to the user.单选按钮的实现——可以选择或取消选择的项目,并向用户显示其 state。 Used with a ButtonGroup object to create a group of buttons in which only one button at a time can be selected.与 ButtonGroup object 一起使用以创建一组按钮,其中一次只能选择一个按钮。 (Create a ButtonGroup object and use its add method to include the JRadioButton objects in the group (创建一个 ButtonGroup object 并使用其 add 方法将 JRadioButton 对象包含在该组中
You must first create a ButtonGroup
and add the JRadioButton
in it您必须首先创建一个ButtonGroup
并在其中添加JRadioButton
for this--How can I let the user choose only one radio button为此--如何让用户只选择一个单选按钮
see this it will help.
ToggleGroup radioGroup=new ToggleGroup();
option1radio.setToggleGroup(radioGroup);
option2radio.setToggleGroup(radioGroup);
option3radio.setToggleGroup(radioGroup);
option4radio.setToggleGroup(radioGroup);
here option1,2,3,4radio are the radiobtn id's;这里 option1,2,3,4radio 是 radiobtn id; Go through this it will help. Go 通过这个它会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.