Complete program listing

A complete listing of the program discussed in this lesson is shown in Listing 1 below.

Listing 1. Complete program listing.
/*File Prob11 Copyright 2012 R.G.Baldwin
The purpose of this program is to demonstrate the use of
a JTextField object to specify the name of an image file,
which is then loaded and displayed in a PictureExplorer
object.

The text field is pre-loaded with the name of an image
file that is in the current directory (Prob11a.jpg).

An image file named Prob11b.jpg is also in the current
directory and can be loaded and displayed by entering
the name in the text field.

Any image file on the disk can be loaded and displayed
by entering the path and name of the image file.
*********************************************************/

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.WindowConstants;

public class Prob11{
  public static void main(String[] args){
    new Prob11Runner();
  }//end main method
}//end class Prob11
//======================================================//

class Prob11Runner extends JFrame{
  private Prob11Runner jFrameObj = null;
  private PictureExplorer explorer = null;
  private Picture pix;

  private JPanel fileNamePanel = new JPanel();
  private JTextField inputFileNameField =
                         new JTextField("Prob11a.jpg",20);

  private String fileName = "no file specified";
  //----------------------------------------------------//

  public Prob11Runner(){//constructor
    fileNamePanel.add(new JLabel("File name: "));
    fileNamePanel.add(inputFileNameField);

    //Add the fileNamePanel to the content pane, adjust
    // to the correct size, and set the title.
    getContentPane().add(fileNamePanel);
    pack();
    setTitle("Dick Baldwin");

    //Make the GUI visible, set the focus, and establish
    // a reference to the GUI object.
    setVisible(true);
    inputFileNameField.requestFocus();
    jFrameObj = this;

    //--------------------------------------------------//
    //Register listeners on the user input field.
    //--------------------------------------------------//

    inputFileNameField.addActionListener(
      new ActionListener(){
        public void actionPerformed(ActionEvent e){
          fileName = inputFileNameField.getText();

          pix = new Picture(fileName);
          pix.addMessage("Dick Baldwin",10,20);
          explorer = new PictureExplorer(pix);

          //Set the location for the control GUI
          // immediately below the PictureExplorer object,
          // and set its default close operation.
          setLocation(0,pix.getHeight() + 128);
          jFrameObj.setDefaultCloseOperation(
                           WindowConstants.EXIT_ON_CLOSE);
        }//end action performed
      }//end newActionListener
    );//end addActionListener
    //--------------------------------------------------//
  }//end constructor
  //----------------------------------------------------//
}//end class Prob11Runner
File: al.htm [Next] [Prev]