New to Java? We'll help you get started with our revised beginner's tutorial, or our free online textbook.


Get the latest Java books
h t t p : / /w w w . j a v a c o f f e e b r e a k . c o m /

Java Coffee Break

Menu



Learning Java

Articles
Author Profiles
Lessons
FAQ's
Books
Newsletter
Tutorials
Talk Java!

Using Java

Applets
JavaBeans
Servlets
Resources
Discuss Java


Looking for Java resources? Check out the Java Coffee Break directory!

Writing the AWT version

The code to generate an AWT frame, and a few buttons, is actually quite small. Figure 3 shows the application in action, and you should examine the source code. Next, we'll show you how to convert it into a Swing application.


AWTFrame.java

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

public class AWTFrame
{
  public static void main (String args[]) throws Exception
  {
    Frame  frame = new Frame("AWT Demo");
    Label  label = new Label ("AWT Version");
    Button button = new Button ("Click to close");
    button.addActionListener( new ActionListener() {
      public void actionPerformed(ActionEvent evt)
      {
        System.exit(0);
      }
    });

    // Set layout manager
    frame.setLayout( new FlowLayout() );

    // Add to frame
    frame.add( label  );
    frame.add( button );
    frame.pack();

    // Center the frame
    Toolkit toolkit = Toolkit.getDefaultToolkit();

    // Get the current screen size
    Dimension scrnsize = toolkit.getScreenSize();

    // Get the frame size
    Dimension framesize= frame.getSize();

    // Set X,Y location
    frame.setLocation ( (int) (scrnsize.getWidth()
                         - frame.getWidth() ) / 2 ,
                        (int) (scrnsize.getHeight()
                         - frame.getHeight()) / 2);

    frame.setVisible(true);
  }
}

Figure 3- AWT Version

Writing the Swing version

Back to main


Copyright 1998, 1999, 2000 David Reilly

Privacy | Legal | Linking | Advertise!

Last updated: Monday, June 05, 2006