Basic Window

Below is the Java Swing program for creating a basic window. Following that, you will find an anchor you can use to run the applet.

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

import javax.swing.*;


public class BasicWindow extends JApplet implements ActionListener, AdjustmentListener, WindowListener {

// adds scrollbars and message display area

JFrame outerBox;

JPanel centerPanel;
JPanel bottomPanel;

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


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

    // *** Global variables for ui components

    JMenuBar menuBar;

    JMenu fileMenu;
      JMenuItem fileNew, fileOpen, fileClose, fileSave, fileQuit;

    JMenu editMenu;
      JMenuItem editCut, editCopy, editPaste, editDelete;

    JMenu helpMenu;
      JMenuItem helpHelp;

    JScrollBar scrollbarV;
    JScrollBar scrollbarH;

    TextField message;

  private void buildWindow ( )  {

    // *** Instantiate objects

    outerBox = new JFrame ( );

    centerPanel = new JPanel ( );
    bottomPanel = new JPanel ( );

    // *** Build Menus

    menuBar = new JMenuBar ( );

    fileMenu = new JMenu ("File");
    fileNew = new JMenuItem ( "New" );
    fileMenu.add ( fileNew );
    fileNew.addActionListener ( this );
    fileOpen = new JMenuItem ( "Open" );
    fileMenu.add ( fileOpen );
    fileOpen.addActionListener ( this );
    fileClose = new JMenuItem ( "Close" );
    fileMenu.add ( fileClose );
    fileClose.addActionListener ( this );
    fileSave = new JMenuItem ( "Save" );
    fileMenu.add ( fileSave );
    fileSave.addActionListener ( this );
    fileQuit = new JMenuItem ( "Quit" );
    fileMenu.add ( fileQuit );
    fileQuit.addActionListener ( this );

    editMenu = new JMenu ("Edit");
    editCut = new JMenuItem ( "Cut" );
    editMenu.add ( editCut );
    editCut.addActionListener ( this );
    editCopy = new JMenuItem ( "Copy" );
    editMenu.add ( editCopy );
    editCopy.addActionListener ( this );
    editPaste = new JMenuItem ( "Paste" );
    editMenu.add ( editPaste );
    editPaste.addActionListener ( this );
    editDelete = new JMenuItem ( "Delete" );
    editMenu.add ( editDelete );
    editDelete.addActionListener ( this );

    helpMenu = new JMenu ("Help");
    helpHelp = new JMenuItem ( "Help" );
    helpMenu.add ( helpHelp );
    helpHelp.addActionListener ( this );

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

    // *** build the scrollbars

    scrollbarV = new JScrollBar (JScrollBar.VERTICAL, 0, 100, 0, 1000 );
    scrollbarV.setName ( "scrollbarV" );
    scrollbarV.addAdjustmentListener ( this );
    scrollbarH = new JScrollBar (JScrollBar.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.setJMenuBar ( menuBar );
    outerBox.getContentPane().setLayout (new BorderLayout ( ) );
    outerBox.getContentPane().add ("Center", centerPanel);
    outerBox.getContentPane().add ("South", bottomPanel);
    outerBox.getContentPane().add ("East", scrollbarV);
    outerBox.setSize (600, 500);
    outerBox.setBackground (Color.white);

    outerBox.addWindowListener ( this );

    outerBox.setVisible ( true );

  }  // end buildWindow ()


  //****************** drawSomething

    // *** Global variables for drawing something

    int offsetX = 0;
    int offsetY = 0;
    int x = 300;
    int y = 250;

    private void drawSomething ( )  {

        Graphics g = centerPanel.getGraphics ();
        Rectangle r = centerPanel.bounds ();
        g.setColor ( centerPanel.getBackground () );
        g.fillRect ( 0, 0, r.width, r.height );
        g.setColor ( new Color(170, 0, 0) );
        g.fillOval ( x + offsetX, y + offsetY, 200, 100 );

    }  // end drawSomething


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

  //****  ActionListener methods

  public void actionPerformed ( ActionEvent e )  {

    Object s = e.getSource();

    // *** process Menu actions

    if ( s instanceof JMenuItem )  {

        // *** file menu items
        if ( s == fileNew )  {
            message.setText ( "JMenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == fileOpen )  {
            message.setText ( "JMenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == fileClose )  {
            message.setText ( "JMenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == fileSave )  {
            message.setText ( "JMenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == fileQuit )  {
            message.setText ( "JMenuItem "+e.getActionCommand()+" detected" );
        }

        // *** edit menu items
        if ( s == editCut )  {
            message.setText ( "JMenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == editCopy )  {
            message.setText ( "JMenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == editPaste )  {
            message.setText ( "JMenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == editDelete )  {
            message.setText ( "JMenuItem "+e.getActionCommand()+" detected" );
        }

        // *** help menu items
        if ( s == helpHelp )  {
            message.setText ( "JMenuItem "+e.getActionCommand()+" detected" );
        }

    }  // end process menu actions

  }  // end actionPerformed


  //****  AdjustmentListener method

  public void adjustmentValueChanged ( AdjustmentEvent e )  {

    Object s = e.getSource();

    // *** process JScrollbar actions

    if ( s instanceof JScrollBar )  {

        if ( s == scrollbarV )  {
            //System.out.println ( "Vertical scrollbar detected; value = "+e.getValue() );
            offsetY = - scrollbarV.getValue();
            message.setText ( "Vertical ScrollBar value: " + scrollbarV.getValue() );
            drawSomething ();
        }

        if ( s == scrollbarH )  {
            //System.out.println ( "Horizontal scrollbar detected; value = "+e.getValue() );
            offsetX = - scrollbarH.getValue();
            message.setText ( "Horlzontal ScrollBar value: " + scrollbarH.getValue() );
            drawSomething ();
        }

    }  // end process scrollbar actions

  }  // end AdjustmentListener

  //****  WindowListener methods

  public void windowActivated ( WindowEvent e )  {
  }

  public void windowDeactivated ( WindowEvent e )  {
  }

  public void windowOpened ( WindowEvent e )  {
  }

   public void windowClosed ( WindowEvent e )  {
  }

  public void windowClosing ( WindowEvent e )  {
    outerBox.setVisible  ( false );
    outerBox.dispose ();
  }

  public void windowIconified ( WindowEvent e )  {
  }

  public void windowDeiconified ( WindowEvent e )  {
  }


}// end BasicWindow


Run the applet