JavaAwtComponents

Last edit April 8, 2003
From JavaProgramming

Overview of graphical components in JavaAwt. Simple examples of an application with a GraphicalUserInterface and a JavaApplet

  • Microsoft Windows API is based on Windows and graphical compo-nents on windows (buttons, text fields, etc.)
  • Java AWT (Abstract Window Toolkit) is 100% Object-Oriented. Meaning that all graphical components inherits from the same base classes
  • This goes for both GUI applications and Applets
  • A container is an area, which contains a number of components, i.e. a panel with buttons
  • A panel is a GUI area, which contains components or graphic drawing
  • An Applet is a specialisation of a panel, which can be con-tained within a website
  • A Frame is a window in a GUI application
  • A Frame can contain all components that inherit from class Component (that includes Applets see example of an Applet that can run as a GUI application later in this document)

Write a GUI application
  • Inherit from class Frame
  • Add an event listener for closing the application when receiv-ing a close event
  • Call Frame::setVisible() to make the window visible

  /* HelloWorldFrame.java */
  import java.awt.*;
  import java.awt.event.*;

public class HelloWorldFrame extends Frame { public static void main(String[] args) { HelloWorldFrame frame = new HelloWorldFrame(); Panel panel = new Panel(); frame.add(panel); // Add event listener to close the application frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setTitle("HelloWorldFrame"); frame.setSize(400,320); // Call setVisible() to make the frame visible frame.setVisible(true); panel.getGraphics().drawString("Hello world!", 20, 20); } }

Write an Applet

  • Inherit from class Applet
  • Overwrite the method Applet::paint() to update graphics

  /* HelloWorldApplet.java */
  import java.awt.*;
  import java.applet.*;

public class HelloWorldApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 20, 20); } }

Show an Applet on a website
  • Compile the .java file to a .class file
  • Add tag <applet> to the .html file
  • Set the value code = the .class file
  • Set the value codebase = the path to the .class file relative to placement of the .html file
  • Set the values width and height to determine the size of the Applet

  /* HelloWorldApplet.html */
  <html>
  <head>
  <title>
  Test HelloWorldApplet
  </title>
  </head>
  <body>
  <applet
    code     = "HelloWorldApplet.class"
    codebase = "."
    width    = "400"
    height   = "300"
  >
  </applet>
  </body>
  </html>

Write an Applet that can run as a GUI application

Graphical components may be included in both GUI applications and Applets. The program below demonstrates this.

  /* StandAloneApplet.java */
  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;

public class StandAloneApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 20, 20); } public static void main(String[] args) { Frame frame = new Frame(); StandAloneApplet applet = new StandAloneApplet(); frame.add(applet); // Add event listener to close the application frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setTitle("Standalone Applet"); frame.setSize(400,320); frame.setVisible(true); } }