//import javax.swing.*;
//import java.awt.event.*;
//import java.awt.*;
//
//public class Main extends JFrame {
// private JRadioButton[] radio = new JRadioButton [3];
// private String[] text = {"사과", "배", "체리"};
// private ImageIcon[] image = {
// new ImageIcon("apple.png"),
// new ImageIcon("pear (1).png"),
// new ImageIcon("cherries (2).png")};
// private JLabel imageLabel = new JLabel();
//
// public Main() {
// setTitle("라디오버튼 Item Event 예제");
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Container c = getContentPane();
// c.setLayout(new BorderLayout());
//
// JPanel radioPanel = new JPanel();
// radioPanel.setBackground(Color.GRAY);
//
// ButtonGroup g = new ButtonGroup();
// for(int i=0; i<radio.length; i++) {
// radio[i] = new JRadioButton(text[i]);
// g.add(radio[i]);
// radioPanel.add(radio[i]);
// radio[i].addItemListener(new MyItemListener());
// }
//
// radio[2].setSelected(true);
// c.add(radioPanel, BorderLayout.NORTH);
// c.add(imageLabel, BorderLayout.CENTER);
// imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
//
// setSize(250,200);
// setVisible(true);
// }
//
// class MyItemListener implements ItemListener {
// public void itemStateChanged(ItemEvent e) {
//
// if(e.getStateChange() == ItemEvent.DESELECTED)
// return;
// if(radio[0].isSelected())
// imageLabel.setIcon(image[0]);
// else if(radio[1].isSelected())
// imageLabel.setIcon(image[1]);
// else
// imageLabel.setIcon(image[2]);
// }
// }
// public static void main(String[] args) {
// new Main();
// }
//}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Main extends JFrame {
private JRadioButton[] radio = new JRadioButton [2];
private String[] text = {"Red", "Blue"};
Container c;
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c = getContentPane();
c.setLayout(new FlowLayout());
// radio[0] = new JRadioButton(text[0]);
// radio[1] = new JRadioButton(text[1]);
// c.add(radio[0]);
// c.add(radio[1]);
// radio[0].addItemListener(new MyItemListener());
// radio[1].addItemListener(new MyItemListener());
ButtonGroup g = new ButtonGroup();
for(int i=0; i<radio.length; i++) {
radio[i] = new JRadioButton(text[i]);
c.add(radio[i]);
g.add(radio[i]);
radio[i].addItemListener(new MyItemListener());
}
setSize(350,300);
setVisible(true);
}
class MyItemListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
c = getContentPane();
if(radio[0].isSelected())
c.setBackground(Color.RED);
else if(radio[1].isSelected())
c.setBackground(Color.BLUE);
}
}
public static void main(String[] args) {
new Main();
}
}