Background

To establish a context for this discussion, we begin by reviewing the code developed in the Java User Interface I lesson in which we built the visual protion of a basic application window:

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

public class step3 extends Applet implements ActionListener, AdjustmentListener {


// adds scrollbars and message display area

MyFrame outerBox;

Panel centerPanel;
Panel bottomPanel;

public void init ( ){
buildWindow ();
} // end init

//****************** buildWindow

// *** Global variables for ui components

MenuBar menuBar;

Menu fileMenu;
MenuItem neww, open, close, save, quit;

Menu editMenu;
MenuItem cut, copy, paste, delete;

Menu helpMenu;
MenuItem help;

Scrollbar scrollbarV;
Scrollbar scrollbarH;

TextField message;

private void buildWindow ( ) {

// *** Instantiate objects

outerBox = new MyFrame ( );

centerPanel = new Panel ( );
bottomPanel = new Panel ( );

// *** Build Menus

menuBar = new MenuBar ( );

fileMenu = new Menu ("File");
neww = new MenuItem ( "New" );
fileMenu.add ( neww );
neww.addActionListener ( this );
open = new MenuItem ( "Open" );
fileMenu.add ( open );
open.addActionListener ( this );
close = new MenuItem ( "Close" );
fileMenu.add ( close );
close.addActionListener ( this );
save = new MenuItem ( "Save" );
fileMenu.add ( save );
save.addActionListener ( this );
quit = new MenuItem ( "Quit" );
fileMenu.add ( quit );
quit.addActionListener ( this );

editMenu = new Menu ("Edit");
cut = new MenuItem ( "Cut" );
editMenu.add ( cut );
cut.addActionListener ( this );
copy = new MenuItem ( "Copy" );
editMenu.add ( copy );
copy.addActionListener ( this );
paste = new MenuItem ( "Paste" );
editMenu.add ( paste );
paste.addActionListener ( this );
delete = new MenuItem ( "Delete" );
editMenu.add ( delete );
delete.addActionListener ( this );

helpMenu = new Menu ("Help");
help = new MenuItem ( "Help" );
helpMenu.add ( help );
help.addActionListener ( this );

menuBar.add ( fileMenu );
menuBar.add ( editMenu );
menuBar.add ( helpMenu );
menuBar.setHelpMenu ( helpMenu );

// *** build the scrollbars

scrollbarV = new Scrollbar (Scrollbar.VERTICAL, 0, 100, 0, 1000 );
scrollbarV.addAdjustmentListener ( this );
scrollbarH = new Scrollbar (Scrollbar.HORIZONTAL, 0, 100, 0, 1000 );
scrollbarH.addAdjustmentListener ( this );

// *** build the message display area

message = new TextField ( "Messages Displayed Here", 80 );
message.setEditable ( false );

// *** build the bottom panel

bottomPanel.setLayout ( new BorderLayout ( ) );
bottomPanel.add ( "North", scrollbarH );
bottomPanel.add ( "South", message );

// *** Set colors

centerPanel.setBackground (Color.white);
bottomPanel.setBackground (Color.white);


// *** build the window

outerBox.setMenuBar ( menuBar );
outerBox.setLayout (new BorderLayout ( ) );
outerBox.add ("Center", centerPanel);
outerBox.add ("South", bottomPanel);
outerBox.add ("East", scrollbarV);
outerBox.resize (600, 500);
outerBox.setBackground (Color.white);

outerBox.addWindowListener ( outerBox );

outerBox.show( );

} // end buildWindow ()


//*********** Interface Methods ***********

//**** ActionListener methods

public void actionPerformed ( ActionEvent e ) {
System.out.println ( "ActionEvent received" );
}

//**** AdjustmentListener methods

public void adjustmentValueChanged ( AdjustmentEvent e ) {
System.out.println ( "AdjustmentEvent received" );
}

}// end step3

Run the applet

The discussion continues with Step 4.