Better Button

The applet BetterButton.java demonstrates the button and the use of a panel to draw on. We select the BorderLayout as the Layout Manager for the applet. In the North position we add a new Button. In the Center position we add an extension of the java.awt.Panel class which we define: MyPanel.java.

The main applet class extends Applet and contains the buttons and the MyPanel, and is registered as an ActionListener for the button events, it implements ActionListener. When it receives button events, it just forwards those events to MyPanel by invoking MyPanel's changeColor() method. This method changes the color of the square and requests a repaint of the panel.