Running a Graphical Application

   


The Welcome program was not terribly exciting. Next, we will demonstrate a graphical application. This program is a simple image file viewer that just loads and displays an image. Again, let us first compile and run it from the command line.

1.

Open a shell window.

2.

Change to the directory CoreJavaBook/v1ch2/ImageViewer.

3.

Enter:

 javac ImageViewer.java java ImageViewer 

A new program window pops up with our ImageViewer application (see Figure 2-9).

Figure 2-9. Running the ImageViewer application


Now select File -> Open and look for a GIF file to open. (We supplied a couple of sample files in the same directory.)

To close the program, click on the Close box in the title bar or pull down the system menu and close the program. (To compile and run this program inside a text editor or an integrated development environment, do the same as before. For example, for Emacs, choose JDE -> Compile, then choose JDE -> Run App.)

We hope that you find this program interesting and useful. Have a quick look at the source code. The program is substantially longer than the first program, but it is not terribly complex if you consider how much code it would take in C or C++ to write a similar application. In Visual Basic, of course, it is easy to write or, rather, drag and drop, such a program. The JDK does not have a visual interface builder, so you need to write code for everything, as shown in Example 2-2. You learn how to write graphical programs like this in Chapters 7 through 9.

Example 2-2. ImageViewer.java
  1. import java.awt.*;  2. import java.awt.event.*;  3. import java.io.*;  4. import javax.swing.*;  5  6. /**  7.    A program for viewing images.  8. */  9. public class ImageViewer 10. { 11.    public static void main(String[] args) 12.    { 13.       JFrame frame = new ImageViewerFrame(); 14.       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 15.       frame.setVisible(true); 16.    } 17. } 18. 19. /** 20.    A frame with a label to show an image. 21. */ 22. class ImageViewerFrame extends JFrame 23. { 24.    public ImageViewerFrame() 25.    { 26.       setTitle("ImageViewer"); 27.       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 28 29.       // use a label to display the images 30.       label = new JLabel(); 31.       add(label); 32 33.       // set up the file chooser 34.       chooser = new JFileChooser(); 35.       chooser.setCurrentDirectory(new File(".")); 36 37.       // set up the menu bar 38.       JMenuBar menuBar = new JMenuBar(); 39.       setJMenuBar(menuBar); 40 41.       JMenu menu = new JMenu("File"); 42.       menuBar.add(menu); 43 44.       JMenuItem openItem = new JMenuItem("Open"); 45.       menu.add(openItem); 46.       openItem.addActionListener(new 47.          ActionListener() 48.          { 49.             public void actionPerformed(ActionEvent event) 50.             { 51.                // show file chooser dialog 52.                int result = chooser.showOpenDialog(null); 53. 54.                // if file selected, set it as icon of the label 55.                if (result == JFileChooser.APPROVE_OPTION) 56.                { 57.                   String name = chooser.getSelectedFile().getPath(); 58.                   label.setIcon(new ImageIcon(name)); 59.                } 60.             } 61.          }); 62. 63.       JMenuItem exitItem = new JMenuItem("Exit"); 64.       menu.add(exitItem); 65.       exitItem.addActionListener(new 66.          ActionListener() 67.          { 68.             public void actionPerformed(ActionEvent event) 69.             { 70.                System.exit(0); 71.             } 72.          }); 73.    } 74. 75.    private JLabel label; 76.    private JFileChooser chooser; 77.    private static final int DEFAULT_WIDTH = 300; 78.    private static final int DEFAULT_HEIGHT = 400; 79. } 


       
    top



    Core Java 2 Volume I - Fundamentals
    Core Java(TM) 2, Volume I--Fundamentals (7th Edition) (Core Series) (Core Series)
    ISBN: 0131482025
    EAN: 2147483647
    Year: 2003
    Pages: 132

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net