• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 37. User Input and Interactivity... > Using the Abstract Windowing Toolkit

Using the Abstract Windowing Toolkit

As a platform-neutral language, Java makes many of the details of graphical user interface (GUI) programming invisible, but they don't go away. As you saw in Chapter 36, "Developing Java Applets," you can build a simple applet with a GUI interface in just 10 lines of code. It takes just a few more lines to add a main() routine and a standalone Frame, enabling the applet to double as a GUI application. (Listing 37.1 shows such a program.)

Code Listing 37.1. HelloApplication.java—You Can Write an Applet That Will Also Run as an Application

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

public class HelloApplication extends Applet {
  public static void main(String[] args) {
    HelloApplicationFrame theApplication =
      new HelloApplicationFrame("Hello Application");
    theApplication.setSize(200,200);
    theApplication.show();
 }

 public void paint(Graphics theGraphics) {
    theGraphics.drawString("Hello, World!", 0, 50);
 }
}

class HelloApplicationFrame extends Frame {
  private HelloApplication fApplet;

  public HelloApplicationFrame(String name) {
    super(name);
    addWindowListener(new HelloWindowAdapter());
    fApplet = new HelloApplication();
    fApplet.init();
    fApplet.start();
    add(fApplet);
  }
  class HelloWindowAdapter extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      fApplet.stop();
      fApplet.destroy();
      System.exit(0);
    }
  }
}
					

					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint