// @topic T11685 Swing demo A8 -- GUI window with three radio buttons // @brief class FrameRadioButtons extends FrameApplication implements ActionListener /** * @author ik */ package pckg; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.text.*; /** * <code>FrameRadioButtons</code> is the demo class for UI window * with two buttons and three radio buttons. */ public class FrameRadioButtons extends FrameApplication implements ActionListener { // ----- Data Attributes ---------- JButton mButChangeColor; // Button that changes background color JButton mButQuit; // Quit button JPanel mMainPanel; GroupRadioButtons mRadioPanel; Color mSystemColor; // ----- Operations ---------- /** * Class constructor takes care of all basic setup steps. */ public FrameRadioButtons(String aCaption) { super(aCaption); setGridLayout(); this.setVisible(true); } // method: FrameRadioButtons /** * Confugures user interface components */ private void setGridLayout() { // Two panels mMainPanel = new JPanel(); mRadioPanel = new GroupRadioButtons(this); // push buttons mButChangeColor = new JButton("Change Color"); mButQuit = new JButton("Quit"); mButChangeColor.addActionListener(this); mButQuit.addActionListener(this); mMainPanel.add(new JLabel("Click the color button twice to see the effect:")); mMainPanel.add(mButChangeColor); mMainPanel.add(mButQuit); mSystemColor = mMainPanel.getBackground(); // Use grid with 2 rows and one column: this.setLayout(new GridLayout(2, 1)); this.getContentPane().add(mRadioPanel); this.getContentPane().add(mMainPanel); this.pack(); } // method: setGridLayout /** * Handles user clicks */ public void actionPerformed(ActionEvent event) { Object obj = event.getSource(); if (obj == mButChangeColor) switchColors(); if (obj == mButQuit) System.exit(0); } // method: actionPerformed /** * Switches background color from original to green and back */ private void switchColors() { if (mSystemColor == mMainPanel.getBackground()) { mMainPanel.setBackground(mRadioPanel.getColor()); } else { mMainPanel.setBackground(mSystemColor); } this.repaint(); } // method: switchColors } // class: FrameRadioButtons