// @topic T11679 Swing demo A7 -- GUI window with two buttons // @brief class FrameTwoButtons 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>FrameTwoButtons</code> is the demo class for UI window * with two buttons. */ public class FrameTwoButtons extends FrameApplication implements ActionListener { // ----- Data Attributes ---------- JButton mButChangeColor; // Button that changes background color JButton mButQuit; // Quit button JPanel mMainPanel; Color mSystemColor; // ----- Operations ---------- /** * Class constructor takes care of all basic setup steps. */ public FrameTwoButtons(String aCaption) { super(aCaption); setFrameDimension(); setMainPanel(); this.setVisible(true); } // method: FrameTwoButtons /** * Sets the frame size */ private void setFrameDimension() { this.setSize(new Dimension(250, 100)); } // method: setFrameDimension /** * Confugures user interface components */ private void setMainPanel() { mMainPanel = new JPanel(); //central panel this.getContentPane().add(mMainPanel); //create and add buttons mButChangeColor = new JButton("Change Color"); mButQuit = new JButton("Quit"); mButChangeColor.addActionListener(this); mButQuit.addActionListener(this); mMainPanel.add(mButChangeColor); mMainPanel.add(mButQuit); mSystemColor = mMainPanel.getBackground(); } // method: setMainPanel /** * 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(Color.green); } else { mMainPanel.setBackground(mSystemColor); } this.repaint(); } // method: switchColors } // class: FrameTwoButtons