Issue
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
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();" and it didnt work I am not sure if I should use if statement because I could not find a way to do it ^.^"
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) {
}
}
Solution
For JRadioButtons you have to add them to a ButtonGroup and set one of the buttons as selected.
Here's a small example:
JRadioButton b1 = new JRadioButton('option1);
b1.setSelected(true);
ButtonGroup g = new ButtonGroup();
g.add(b1);
Answered By - user6305847
Answer Checked By - Marie Seifert (JavaFixing Admin)