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

Chapter 38. 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 we saw in Chapter 37, "Developing Java Applets," you can build a simple applet with a GUI interface in just 10 lines of code. (See Listing 37.2, HelloApplet.java .) 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 38.1 shows such a program.)

Code Listing 38.1. HelloApplication.javaIf You're Going to Write an Applet, Consider Adding the Code to Make It a Standalone GUI Application as Well

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