import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.GridLayout; import java.awt.Color; import javax.swing.JButton; import java.util.Random; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class ThreePanels extends JFrame implements ActionListener { public static final int WIDTH = 900; public static final int HEIGHT = 600; private JPanel redPanel = new JPanel(); private JPanel whitePanel = new JPanel(); private JPanel bluePanel = new JPanel(); private JButton myButton = new JButton("My-Button"); private Random ranIntGen = new Random(); // No-argument constructor ThreePanels() public ThreePanels( ) { /* * Hierarchy of GUI Classes (in java.awt and javac.swing packages) * AWT: an older package designed for windowing interfaces. * SWING: an improved version of the AWT package. * It did not completely replace AWT. Some AWT classes such as * Layout Manager, ActionEvent and ActionListener are still needed * when using SWING packages. * --//////////////////////////////////////// * Object: * Component (Abstract class): AWT Package (Abstract Window Toolkit) * BorderLayout * FlowLayout * GridLayout * Component -> Container -> Window -> Frame -> JFrame (javac.swing) * Component -> JComponent (javac.swing) * JComponent (abstract class) * JPanel * JLabel * JTextComponent (abstract class) * JTextField * JTextArea * JMenuBar * AbstractButton * JButton * JMenuItem * JMenu *--//////////////////////////////////////////// * The class Container and any of its descendent classes such as JFrame and * JPanel can have components added to them. * These components include JPanel, JLabel, JMenuBar, JButton and JTextArea * and JTextField. * JFrame: An object of class JFrame is what you think of as a window. It automatically has * a border and some basic buttons for minimizing/maximizing the window and similar actions. * It is a container and many components can be added to it. * Some JFrame methods: * 1. Component add (Component addedComponent): of Class Container * 2. void setDefaultCloseOperation (int operation) * sets the action that will happen by default when the user clicks * the Close-Window button. The argument should be one of the following: * a. JFrame.DO_NOTHING_ON_CLOSE * JFrame do nothing. If there is any registered window listeners, * they are invoked. * b. JFrame.HIDE_ON_CLOSE * Hide the frame after invoking any registered window listeners. * c. JFrame.DISPOSE_ON_CLOSE * Hide and dispose the frame after invoking any registered window listeners. * When a window is disposed, it is eliminated but the program * does not end. To end the program, the next argument should be used. * d. JFrame.EXIT_ON_CLOSE * Exit the application using the System exit method. * If no action is specified, then the default action is HIDE_ON_CLOSE. * 3. void setSize (int width, int height) * Pixels are the units of measures. * 4. void setTitle (String title) * 5. void setLayout (LayoutManager manager) * Some layout managers in java.awt package * a. FlowLayout * Displays components from left to right in the order * in which they are added. * b. BorderLayout * Displays components in five areas: north, south, east, west and center. * The second argument of the add method specifies the area such as * BorderLayout.SOUTH or BorderLayout.CENTER * c. GridLayout * Lays out components in a grid, with each component stretched * to fill the grid. * 6. void dispose() * Eliminates the calling JFrame and all subcomponents. * But the program does not end if there are items left other than * the calling JFrame and all subcomponents. *///////////////////////////////////////////////// super("Three Panel Window"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(1,4)); myButton.setBackground (Color.GREEN); myButton.addActionListener(this); add(myButton); redPanel.setBackground(Color.LIGHT_GRAY); add(redPanel); whitePanel.setBackground(Color.LIGHT_GRAY); add(whitePanel); bluePanel.setBackground(Color.LIGHT_GRAY); add(bluePanel); } public void actionPerformed(ActionEvent e) { int ran = ranIntGen.nextInt(3); redPanel. setBackground (Color.LIGHT_GRAY); whitePanel. setBackground (Color.LIGHT_GRAY); bluePanel. setBackground (Color.LIGHT_GRAY); switch (ran) { case 0: { redPanel. setBackground (Color.RED); break; } case 1: { bluePanel. setBackground (Color.BLUE); break; } case 2: { whitePanel. setBackground (Color.WHITE); break; } default: { System.err.println ("ILLEGAL RANDOM INTEGER:::BYE"); System.exit (0); } } // end of switch } // end of actionPerformed() /////////////////////////////////////// public static void main(String[] args) { ThreePanels myWindow = new ThreePanels( ); myWindow.setVisible(true); } } // end of class ThreePanels