Chapter 6. GUI Programming: From Appletsto Swing

     

This chapter is all about creating GUIs with Java code in Eclipse. So far, our code has just displayed text in the console. In this chapter, we're going to start creating GUIs using applets, the Abstract Windowing Toolkit (AWT), and Swing. We'll also take a look at using an Eclipse plug-in to create Swing code.

Our first topic is all about building applets. Java applets were Java's first foray into the Internet, and they were popular for quite a while. Browsers first started stocking Java in order to support applets. In time, applets have become less popular because they're necessarily limited and have been superceded by glitzy packages like Flash and Java Web Start. Nonetheless, Eclipse has special provisions for developing and testing applets, so we'll take a brief look here at how that support works.

Creating an applet is much like creating any other Java project in Eclipse. In this case, our applet is just going to display the message "Hello from Eclipse!" Create a new Java project named Ch06_01 , and give it a new class, Ch06_01 , in the org.eclipsebook.ch06 package. To create an applet, we'll need to import java.applet.Applet and java.awt.* :

 import java.applet.Applet; import java.awt.*; 

Then extend the Ch06_01 class from the Applet class:

 public class Ch06_01 extends Applet {         .         .         . 

In an applet, the init method lets you initialize the applet, and we'll set the background color to white in that method. The start , stop , and destroy methods let you handle the associated events in the applet's life cycle. To draw our text in the applet's window, we'll use the Graphics object passed to the paint method. Here's the code to add:

 public class Ch06_01 extends Applet {  public void init( )   {   setBackground(Color.white);   }   public void start( )   {   }   public void paint(Graphics g)   {   g.drawString("Hello from Eclipse!", 60, 100);   }   public void stop( )   {   }   public void destroy( )   {   }  } 

And that's all you need for the applet's code. To test it, select Run As Java Applet, and the applet should run. If Eclipse doesn't recognize your applet, you can specifically set the launch configuration for this code. To do that, select the Ch06_01 project in the Package Explorer and select Run Run to open the Run dialog you see in Figure 6-1. Then double-click the Java Applet configuration in the Configurations box and the Ch06_01 project will automatically be added as an applet. Click the Run button to run the applet. After setting the launch configuration, you should be able to select Run As Java Applet to run the applet.

Figure 6-1. Setting an applet's launch configuration
figs/ecps_0601.gif

You can see the results in the Java applet viewer in Figure 6-2. The applet is working as it should.

Figure 6-2. Running an applet
figs/ecps_0602.gif

To use this applet in a web page, use the generated class file, Ch06_01.class , together with the <APPLET> HTML tag, something like this:

 <APPLET      CODE = "Ch06_01.class"      WIDTH = 300      HEIGHT = 200 > 

That's all it takes; the applet is complete. You can see the entire code for the applet in Example 6-1.

Example 6-1. A simple applet
 package org.eclipsebook.ch06; import java.applet.Applet; import java.awt.*; public class Ch06_01 extends Applet {     public void init( )     {         setBackground(Color.white);     }     public void start( )     {     }     public void paint(Graphics g)     {         g.drawString("Hello from Eclipse!", 60, 100);     }     public void stop( )     {     }     public void destroy( )     {     } } 

When you launch them, Java applets appear in the Java applet viewer by default. That's fine as far as it goes, but there's more to GUIs than applets. The next step is going to be creating our own windowed applications.



Eclipse
Eclipse
ISBN: 0596006411
EAN: 2147483647
Year: 2006
Pages: 114
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net