This happens because you have not set the size and position of you radio button on your panel.
Try adding this before the line add(rb);
rb.setBounds(10, 10, 100, 100);
I would like to suggest a few things about how you have laid your code out though.
The first is you extend JPanel and implemented Action Listener. You are not actually adding any new functionality to JPanel, you are just setting a panel up, so I would not do this. Similar Panel is not an action listener, you just want to use an action listener on one of your components in the panel. Finally, every time you make a Panel, it actually creates and opens a JFrame. This would be very hard to understand just by looking at the name "Panel". These are breaches of the Single Responsibility Principle and probably many others. I would recommend looking up SOLID, explanation from ITNEXT, explanation from Stackify, as it will help you understand why you should not do that.
I also noticed you are using a null
layout on your panel. This is generally very bad. I recommend you read this question as it will help explain why null layouts are bad.
So looking at layout managers, GridBagLayout
gives you a powerful way to set up a UI.
A short example might be
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class Example {
private void run() {
JFrame window = createWindow();
window.setVisible(true);
}
private JFrame createWindow() {
JFrame frame = new JFrame();
frame.setTitle("Testing Stuff");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(createPanel());
frame.pack();
frame.setLocationRelativeTo(null);
return frame;
}
private JPanel createPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1;
constraints.weighty = 1;
String birdString = "Bird";
JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setMnemonic(KeyEvent.VK_B);
birdButton.setActionCommand(birdString);
birdButton.setSelected(true);
constraints.gridy = 0;
panel.add(birdButton, constraints);
String catString = "Cat";
JRadioButton catButton = new JRadioButton(catString);
catButton.setMnemonic(KeyEvent.VK_C);
catButton.setActionCommand(catString);
constraints.gridy = 1;
panel.add(catButton, constraints);
String dogString = "Dog";
JRadioButton dogButton = new JRadioButton(dogString);
dogButton.setMnemonic(KeyEvent.VK_D);
dogButton.setActionCommand(dogString);
constraints.gridy = 2;
panel.add(dogButton, constraints);
String rabbitString = "Rabbit";
JRadioButton rabbitButton = new JRadioButton(rabbitString);
rabbitButton.setMnemonic(KeyEvent.VK_R);
rabbitButton.setActionCommand(rabbitString);
constraints.gridy = 3;
panel.add(rabbitButton, constraints);
String pigString = "Pig";
JRadioButton pigButton = new JRadioButton(pigString);
pigButton.setMnemonic(KeyEvent.VK_P);
pigButton.setActionCommand(pigString);
constraints.gridy = 4;
panel.add(pigButton, constraints);
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);
group.add(dogButton);
group.add(rabbitButton);
group.add(pigButton);
RadioActionListener listener = new RadioActionListener();
//Register a listener for the radio buttons.
birdButton.addActionListener(listener);
catButton.addActionListener(listener);
dogButton.addActionListener(listener);
rabbitButton.addActionListener(listener);
pigButton.addActionListener(listener);
return panel;
}
private class RadioActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new Example().run());
}
}