Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 

Help is available for each task, or you can go straight to the solution source code.

Task 1

If you are using JavaTM Development Kit 1.1.x, you need to add the swingall.jar file to your class path to compile your Swing programs. In the Installation magercise, you installed Swing and set the SWINGHOME environment variable to the installation directory. Now, you need to add the archives to the CLASSPATH.

Place command all on one line.

For Solaris:

  
  setenv CLASSPATH ${JDKHOME}/lib/classes.zip:
   ${SWINGHOME}/swingall.jar.

For Windows 95/NT:

    set CLASSPATH=%JDKHOME%\lib\classes.zip;
   %SWINGHOME%\swingall.jar;.

For JDKTM 2, you do not need to do anything special.

Task 2

Starting with the skeleton code, import the swing package.
  
  import javax.swing.*;

Task 3

To have the components appear one on top of each other, modify the skeleton's constructor so that it sets the layout to GridLayout with 3 rows and 1 column.

Add the following method call to the constructor.
    setLayout(new GridLayout(3,1));

Task 4

Create top and bottom JButtons with action event listeners that print "top" and "bottom" to standard output.

Create the top button and event processing via:

    JButton top = new JButton("Top");
top.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    System.out.println("top");
  }
});

Create the bottom button and event processing via:

  JButton bottom = new JButton("Bottom");
bottom.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    System.out.println("bottom");
  }
});

Task 5

Create a JLabel with text "Swing Components are like AWT 1.1". Add it to the first row of the grid.

Add the following code after the button creation code.
   add(new JLabel(
          "Swing Components are like AWT 1.1"));

Task 6

Add the top and bottom button to the grid.
    add(top);
    add(bottom);

Task 7

Save everything and compile the program. Then run it to see the results. Clicking a button displays a message to the screen.

As a precaution, the Save command appends a "1" to the end of the filename you want to save. This can help prevent you from accidently overwriting your source code.

    javac FirstAppl.java
    java FirstAppl

If you are using a 1.1 version of the JDKTM, make sure your CLASSPATH environment variable has the swingall.jar file in it.

Return to Magercise 2

Copyright © 1998-1999 MageLang Institute. All Rights Reserved.


[ This page was updated: 19-Nov-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.