//package lab5out; import java.awt.*; import javax.swing.*; public class RadioButtonTest extends JFrame { private Container cp; private String names[] = {"Bird", "Pig", "Cat", "Dog", "Rabbit"}; private JRadioButton radio[]; private JCheckBox check[]; private JButton button[]; private JLabel label[]; private JTextField text[]; private JLabel image; public RadioButtonTest() { } public RadioButtonTest(String title) { super(title); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); radio = new JRadioButton[5]; check = new JCheckBox[5]; button = new JButton[3]; label = new JLabel[3]; text = new JTextField[3]; image = new JLabel(new ImageIcon("Bird.gif","Bird"), JLabel.CENTER); cp = this.getContentPane(); cp.setLayout(new BorderLayout(5,5)); JPanel p1 = new JPanel(new GridLayout(5,1)); JPanel p2 = new JPanel(new GridLayout(5,1)); JPanel p3 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5)); JPanel p4 = new JPanel(new GridLayout(3,1)); Font font = new Font("Serif", Font.BOLD, 12); ButtonGroup bg = new ButtonGroup(); for(int i = 0; i < names.length; i++) { radio[i] = new JRadioButton(names[i]); bg.add(radio[i]); p1.add(radio[i]); check[i] = new JCheckBox(names[i]); p2.add(check[i]); } for(int j = 0; j < 3; j++) { text[j] = new JTextField("TextField", 12); text[j].setFont(font); button[j] = new JButton("Button " + j); button[j].setFont(font); label[j] = new JLabel("Label " + j, JLabel.RIGHT); } for(int j = 0; j < 3; j++) { JPanel p5 = new JPanel(); p5.add(label[j]); p5.add(button[j]); p5.add(text[j]); p4.add(p5); } p3.add(image); p3.add(p1); p3.add(p2); cp.add(p3,BorderLayout.NORTH); cp.add(p4,BorderLayout.SOUTH); setSize(400,300); setVisible(true); } //Returns the radio button at the specified index.- index varies from 0 – N-1 public JRadioButton getRadioButtonAt(int index) { return(radio[index]); } //Returns the check box at the specific intex – index varies from 0 – N-1 public JCheckBox getCheckBoxAt(int index) { return(check[index]); } //Returns the push button at the specified index.- index varies from 0 – N-1 public JButton getButtonAt(int index) { return(button[index]); } //Returns the label at the specified index. Index - varies from 0 – N-1 public JLabel getLabelAt(int index) { return(label[index]); } //Returns the textfield at the specified index.- index varies from 0 – N-1 public JTextField getTextAt(int index) { return(text[index]); } //Returns JLabel containing the gif image. public JLabel getImage() { return(image); } public static void main(String[] args) { RadioButtonTest radiobuttontest = new RadioButtonTest("Test"); } }