Рекомендации:
- Прочитайте Как использовать радио-кнопки учебник
- Прочитайте Как использовать CardLayout учебник
- Узнайте, как использовать
ButtonGroup
, поскольку вы хотите, чтобы за раз выбиралась только одна кнопка
public final class RadioButtonDemo {
private static CardPanel cards;
private static Card cardOne;
private static Card cardTwo;
private static Card cardThree;
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame("RB Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(
new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
cards = new CardPanel();
frame.getContentPane().add(cards);
frame.getContentPane().add(new ControlPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void createCards(){
cardOne = new Card(
"Card 1",
new JLabel("This is card one"),
Color.PINK);
cardTwo = new Card(
"Card 2",
new JLabel("This is card two"),
Color.YELLOW);
cardThree = new Card(
"Card 3",
new JLabel("This is card three"),
Color.CYAN);
}
private static final class Card extends JPanel{
private final String name;
public Card(
final String name,
final JComponent component,
final Color c){
super();
this.name = name;
setBorder(BorderFactory.createLineBorder(Color.BLACK));
setBackground(c);
add(component);
}
public final String getName(){
return name;
}
}
private static final class CardPanel extends JPanel{
public CardPanel(){
super(new CardLayout());
createCards();
add(cardOne, cardOne.getName());
add(cardTwo, cardTwo.getName());
add(cardThree, cardThree.getName());
}
}
private static final class ControlPanel extends JPanel{
private static JRadioButton showCardOneButton;
private static JRadioButton showCardTwoButton;
private static JRadioButton showCardThreeButton;
private static JButton showButton;
public ControlPanel(){
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(createJRadioButtonPanel());
add(createJButtonPanel());
}
private final JPanel createJRadioButtonPanel(){
final JPanel panel = new JPanel();
showCardOneButton = new JRadioButton(cardOne.getName());
showCardOneButton.setSelected(true);
showCardTwoButton = new JRadioButton(cardTwo.getName());
showCardThreeButton = new JRadioButton(cardThree.getName());
ButtonGroup group = new ButtonGroup();
group.add(showCardOneButton);
group.add(showCardTwoButton);
group.add(showCardThreeButton);
panel.add(showCardOneButton);
panel.add(showCardTwoButton);
panel.add(showCardThreeButton);
return panel;
}
private final JPanel createJButtonPanel(){
final JPanel panel = new JPanel();
showButton = new JButton("Show");
showButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
CardLayout cl = (CardLayout) cards.getLayout();
if(showCardOneButton.isSelected()){
cl.show(cards, showCardOneButton.getText());
}
else if(showCardTwoButton.isSelected()){
cl.show(cards, showCardTwoButton.getText());
}
else if(showCardThreeButton.isSelected()){
cl.show(cards, showCardThreeButton.getText());
}
}
});
panel.add(showButton);
return panel;
}
}
}