From JavaProgramming '''Overview of graphical components in JavaAwt. Simple examples of an application with a GraphicalUserInterface and a JavaApplet''' http://www.datapharm.dk/ht/java/awtcomp.gif * 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 /* H''''''elloWorldFrame.java */ import java.awt.*; import java.awt.event.*; public class H''''''elloWorldFrame extends Frame { public static void main(String[] args) { H''''''elloWorldFrame frame = new H''''''elloWorldFrame(); Panel panel = new Panel(); frame.add(panel); // Add event listener to close the application frame.addWindowListener(new W''''''indowAdapter() { public void windowClosing(W''''''indowEvent 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 /* H''''''elloWorldApplet.java */ import java.awt.*; import java.applet.*; public class H''''''elloWorldApplet 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 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 /* H''''''elloWorldApplet.html */ Test H''''''elloWorldApplet '''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. /* S''''''tandAloneApplet.java */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class S''''''tandAloneApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 20, 20); } public static void main(String[] args) { Frame frame = new Frame(); S''''''tandAloneApplet applet = new S''''''tandAloneApplet(); frame.add(applet); // Add event listener to close the application frame.addWindowListener(new W''''''indowAdapter() { public void windowClosing(W''''''indowEvent e) { System.exit(0); } }); frame.setTitle("Standalone Applet"); frame.setSize(400,320); frame.setVisible(true); } }