简体   繁体   English

如何让用户只选择一个单选按钮? 以及我如何为每个人定价并让用户支付他选择的门票?

[英]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.

相关问题 我将如何让用户从该列表中选择一个项目并使其完成功能,而不是一个个地解析所有项目? - How would I let the user select an Item from this list and have it complete the function rather than parsing through all of them one by one? 如何让用户输入他们的信息? - How can I let a user input their info? 如何编写代码让用户选择什么都不选择或选择时间? - How can I write the code to let the User to choose nothing or pick a time? 如何让用户中断Java中的无限循环? - How do I let a user interrupt an endless loop in Java? 我如何让用户通过鼠标移动jLabel? - How can i let the user move the jLabels by mouse? 如何让用户输入在 java 中重复我的代码? - How can I let the user input repeat my code in java? 我正在制作一个打电话的应用程序,如何保存多个数字,然后给用户选择其中一个? - I am making an app that makes calls, how to save multiple numbers and then give to the user choose one of them? 如何使用户可以选择一个人的性别? - How do I make it so that the user can choose the gender of a person? 发生错误后,如何让用户再次输入变量? - How do I let the user input the variable again after doing an error? 如何让用户从Java中的单链列表中搜索 - How do I let the user search from a singly linked list in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM