Guest User

CoreJavaExample Not Working

a guest
Aug 12th, 2010
88
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.EventQueue;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import javax.swing.*;
  5. public class ImageViewer
  6. {
  7.     public static void main(String[] args)
  8.     {
  9.         EventQueue.invokeLater(new Runnable()
  10.         {
  11.           public void run()
  12.           {
  13.             JFrame frame = new ImageViewerFrame();
  14.             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.             frame.setVisible(true);
  16.           }
  17.         });
  18.     }
  19. }
  20. class ImageViewerFrame extends JFrame
  21. {
  22.    public ImageViewerFrame()
  23.    {
  24.     setTitle("ImageViewer");
  25.     setSize(300,400);
  26.     label = new JLabel();
  27.     add(label);
  28.     JMenuBar menuBar = new JMenuBar();
  29.     setJMenuBar(menuBar);
  30.     JMenu menu = new JMenu("File");
  31.     menuBar.add(menu);
  32.     JMenuItem openItem = new JMenuItem("Open");
  33.     menu.add(openItem);
  34.     openItem.addActionListener(new ActionListener()
  35.       {
  36.         public void actionPerformed(ActionEvent event)
  37.         {      
  38.             int result=chooser.showOpenDialog(null);
  39.             if(result==JFileChooser.APPROVE_OPTION)
  40.             {
  41.             String name = chooser.getSelectedFile().getPath();
  42.                 label.setIcon(new ImageIcon(name));
  43.             }
  44.         }
  45.       });
  46.     JMenuItem exitItem = new JMenuItem("Exit");
  47.     menu.add(exitItem);
  48.     exitItem.addActionListener(new ActionListener()
  49.     {
  50.        
  51.         public void actionPerformed(ActionEvent event)
  52.         {
  53.         System.exit(0);
  54.         }
  55.     });
  56.    }
  57.    private JLabel label;
  58.    private JFileChooser chooser;
  59. }
RAW Paste Data