Home : Course Map : Chapter 6 : Java :
Swing User Interface Components
JavaTech
Course Map
Chapter 6

Introduction
AWT
Swing
Containers
  Demo 1
UI Components
  Demo 2
UI Layout
  Demo 3   Demo 4
Text Display
  Demo 5
Drawing
  Demo 6   Demo 7
Draw Polygons
  Demo 8   Demo 9
Colors
Text Draw 
  Demo 10
Images
  Demo 11
Exercises

    Supplements
AWT
  Demo 1
Drawing
  Demo 2
Text Drawing
  Demo 3
UI Components
  Demo 4

Java2D
Shapes & Areas
  Demo 1   Demo 2
Stroke & Paint
  Demo 3
Transforms
  Demo 4
Gradients&Textures
  Demo 5   Demo 6
Text
  Demo 7   Demo 8
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

As discussed in AWT and Swing sections, we prefer to use the lightweight Swing components if possible since they are so much more flexible than the standard AWT components and there is a greater selection.

On some platforms with limited resources it may be necessary to use the less memory consuming AWT approach. Chapter 6: Supplements provides a brief overview of GUI building within only the AWT.

Note: Do not mix Swing components and AWT components. Use either all Swing or all AWT. Otherwise, the display of the components will become very unstable and cause your your computer to explode. Not really, at least not the last part about an explosion.

So we will concentrate on Swing GUI development. In this chapter we will discuss some basic aspects of interface design, starting here with a demonstration of creating a subclass of JPanel. An interface typically sub-divides the display into several such panels, allowing for a flexible and logical arrangement of the components.

The following applet creates a subclass of JPanel that two buttons. An instance of this JPanel is then added to the applet's content pane.

ButtonsPanelApplet.java
import java.awt.*;
import javax.swing.*;

public class ButtonsPanelApplet extends JApplet {

  public void init() {
    Container content_pane = getContentPane();
    // Create an instance of JButton
    ActionButtonsPanel buttonsPanel =
      new ActionButtonsPanel ();

    // Add the button to the contentPane.
    content_pane.add (buttonsPanel);
  }
}


// JPanel subclass with two buttons .
class ActionButtonsPanel extends JPanel
{
   ActionButtonsPanel () {

     // Create two buttons
     JButton addBut = new JButton("Add");
     JButton multBut = new JButton("Mult");

     // Put a button in each grid cell
     add(addBut);
     add(multBut);
   } // ctor
}
// class ActionButtonsPanel

 

 

Latest update: Oct. 25, 2004

            Tech
Java Tech Graphics
Starting to Plot
  Demo 1
Drawing Panel
  Demo 2
Histogram Display

  Demo 3
Exercises

           Physics
Display Text Data
  Demo 1
Plot Data
  Demo 2
Find Max/Min
  Demo 3
Exercises

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.