hypesystem

GE - ImageViewer.java

Nov 14th, 2011
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. /*
  2.  * GE.1 i) The Quit menu item has an action listener, that calls ImageViewer's
  3.  * actionPerformed method (addActionListener(this)). In actionPerformed, a line
  4.  * is printed containing the string "Item: " followed by the name of the command
  5.  * (event) triggering the method, in this case Quit. On clicking the button, the
  6.  * console will print the text "Item: Quit".
  7.  */
  8. package ge;
  9.  
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import javax.swing.*;
  13.  
  14. /**
  15.  * ImageViewer is the main class of the image viewer application. It builds
  16.  * and displays the application GUI.
  17.  *
  18.  * To start the application, create an object of this class.
  19.  *
  20.  * @author Michael Kolling and David J Barnes
  21.  * @author hypesystem <[email protected]>
  22.  * @version 0.2 modified by hypesystem
  23.  */
  24. public class ImageViewer
  25.     implements ActionListener
  26. {
  27.     private JFrame frame;
  28.    
  29.     /**
  30.      * Create an ImageViewer show it on screen.
  31.      */
  32.     public ImageViewer()
  33.     {
  34.         makeFrame();
  35.     }
  36.  
  37.     /**
  38.      * Receive notification of an action. This is deprecated, but would work, if
  39.      * makeFrame set the actionlisteners of items to this
  40.      * (addActionListener(this)).
  41.      * @param event Details of the action.
  42.      */
  43.     @Override
  44.     public void actionPerformed(ActionEvent event)
  45.     {
  46.         System.out.println("Item: " + event.getActionCommand());
  47.         if(event.getActionCommand().equals("Open")) openFile();
  48.         else if(event.getActionCommand().equals("Save")) saveFile();
  49.         else if(event.getActionCommand().equals("Quit")) quit();
  50.         else System.out.println("Item called not known");
  51.     }
  52.  
  53.     /**
  54.      * part of deprecated actionPerformed logic
  55.      */
  56.     private void openFile() {
  57.         //...
  58.     }
  59.    
  60.     /**
  61.      * part of deprecated actionPerformed logic
  62.      */
  63.     private void saveFile() {
  64.         //...
  65.     }
  66.    
  67.     /**
  68.      * part of deprecated actionPerformed logic
  69.      */
  70.     private void quit() {
  71.         frame.dispose();
  72.     }
  73.    
  74.     // ---- swing stuff to build the frame and all its components ----
  75.    
  76.     /**
  77.      * Create the Swing frame and its content.
  78.      */
  79.     private void makeFrame()
  80.     {
  81.         frame = new JFrame("ImageViewer");
  82.         makeMenuBar(frame);
  83.        
  84.         Container contentPane = frame.getContentPane();
  85.        
  86.         JLabel label = new JLabel("I am a label. I can display some text.");
  87.         contentPane.add(label);
  88.  
  89.         // building is done - arrange the components and show        
  90.         frame.pack();
  91.         frame.setVisible(true);
  92.     }
  93.    
  94.     /**
  95.      * Create the main frame's menu bar.
  96.      * @param frame   The frame that the menu bar should be added to.
  97.      */
  98.     private void makeMenuBar(JFrame frame)
  99.     {
  100.         JMenuBar menubar = new JMenuBar();
  101.         frame.setJMenuBar(menubar);
  102.        
  103.         // create the File menu
  104.         JMenu fileMenu = new JMenu("File");
  105.         menubar.add(fileMenu);
  106.        
  107.         JMenuItem openItem = new JMenuItem("Open");
  108.         openItem.addActionListener(new OpenActionListener());
  109.         fileMenu.add(openItem);
  110.        
  111.         JMenuItem saveItem = new JMenuItem("Save");
  112.         saveItem.addActionListener(new SaveActionListener());
  113.         fileMenu.add(saveItem);
  114.        
  115.         // added seperator to seperate the categories of menu items
  116.         fileMenu.add(new JPopupMenu.Separator());
  117.  
  118.         JMenuItem quitItem = new JMenuItem("Quit");
  119.         quitItem.addActionListener(new QuitActionListener());
  120.         fileMenu.add(quitItem);
  121.     }
  122.    
  123.     /**
  124.      * ActionListener performing the actions when clicking the open item.
  125.      */
  126.     class OpenActionListener implements ActionListener {
  127.         @Override
  128.         public void actionPerformed(ActionEvent event) {
  129.             //...
  130.         }
  131.     }
  132.    
  133.     /**
  134.      * ActionListener performing the actions when clicking the save item.
  135.      */
  136.     class SaveActionListener implements ActionListener {
  137.         @Override
  138.         public void actionPerformed(ActionEvent event) {
  139.             //...
  140.         }
  141.     }
  142.    
  143.     /**
  144.      * ActionListener performing the actions when clicking the quit item.
  145.      */
  146.     class QuitActionListener implements ActionListener {
  147.         @Override
  148.         public void actionPerformed(ActionEvent event) {
  149.             frame.dispose();
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment