// @topic T11693 Swing demo A9 -- GUI window with radio buttons and menu // @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. */ 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; JMenuBar mBar; JMenu mFile; JMenuItem mFileOpen; JMenuItem mFileExit; JMenu mHelp; JMenuItem mHelpAbout; // Menu action listeners MenuFileExitListener mMenuFileExitListener; // ----- Operations ---------- /** * Class constructor takes care of all basic setup steps. */ public FrameRadioButtons(String aCaption) { super(aCaption); setMenu(); setGridLayout(); this.setVisible(true); } // method: FrameRadioButtons /** * Confugures menu items */ private void setMenu() { mMenuFileExitListener = new MenuFileExitListener("Exit"); mBar = new JMenuBar(); //menu bar this.setJMenuBar(mBar); //add to JFrame mFile = new JMenu("File"); //top-level menu mFile.setMnemonic('F'); mBar.add(mFile); //add to menu bar mFileOpen = new JMenuItem("Open"); //menu items mFileExit = new JMenuItem("Exit"); mFileExit.setMnemonic('x'); mFileExit.addActionListener(mMenuFileExitListener); mFile.add(mFileOpen); //add to menu mFile.addSeparator(); //put in separator mFile.add(mFileExit); mHelp = new JMenu("Help"); mFile.setMnemonic('H'); mBar.add(mHelp); mHelpAbout = new JMenuItem("About"); mHelp.add(mHelpAbout); }//void setMenu /** * 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 push button 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 custom and back */ private void switchColors() { if (mSystemColor == mMainPanel.getBackground()) { mMainPanel.setBackground(mRadioPanel.getColor()); } else { mMainPanel.setBackground(mSystemColor); } this.repaint(); } // method: switchColors } // class: FrameRadioButtons