package lab9out; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class RadioTextPanel extends JPanel implements Observer { private JPanel jp1 = new JPanel(new GridLayout(5,1,2,2)); private JPanel jp2 = new JPanel(new GridLayout(1,2,3,0)); private JLabel text = new JLabel("None Clicked"); private JLabel image = new JLabel(new ImageIcon("lab9out/Bird.gif", "Bird"), JLabel.CENTER); private JRadioButton[] jrb= new JRadioButton[5]; private String[] name = {"Bird", "Cat", "Dog", "Rabbit", "Pig"}; private ButtonGroup bg = new ButtonGroup(); private Font font = new Font("Serif", Font.BOLD, 12); private String button_name; Counter count; public RadioTextPanel() { //empty } public RadioTextPanel(Counter myC) { count = myC; ActionHandler ah = new ActionHandler(); for(int i = 0; i < 5; i++) { jrb[i] = new JRadioButton(name[i]); jrb[i].addActionListener(ah); bg.add(jrb[i]); jp1.add(jrb[i]); } jp2.add(image); jp2.add(jp1); jp2.add(text); this.add(jp2); } class ActionHandler implements ActionListener { public void actionPerformed(ActionEvent ae) { Object obj = ae.getSource(); if(obj instanceof JRadioButton) { button_name = ae.getActionCommand(); count.userClickedButton(button_name); } } } public JRadioButton getRadioButtonAt(int index) { return(jrb[index]); } public void update(Observable o,Object arg) { text.setText(button_name + " button clicked " + arg); } public static void main(String[] args) { Gui g = new Gui(); } } class Counter extends Observable { HashMap hm = new HashMap(); Integer value; Counter() { //empty } public void userClickedButton(String clickString) { value = (Integer)hm.get(clickString); if(value == null) { value = new Integer(1); } else { value = new Integer(value.intValue() + 1); } hm.put(clickString, value); setChanged(); notifyObservers(value); } public HashMap getHashMap() { return hm; } } class Gui extends JFrame { private Container cp; private Counter myC = new Counter(); Gui() { super("Lab 9 Out"); this.setDefaultCloseOperation(this.EXIT_ON_CLOSE); cp = this.getContentPane(); RadioTextPanel rtf = new RadioTextPanel(myC); myC.addObserver(rtf); cp.add(rtf,BorderLayout.CENTER); setSize(500,200); setVisible(true); } }