Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * GE.1 i) The Quit menu item has an action listener, that calls ImageViewer's
- * actionPerformed method (addActionListener(this)). In actionPerformed, a line
- * is printed containing the string "Item: " followed by the name of the command
- * (event) triggering the method, in this case Quit. On clicking the button, the
- * console will print the text "Item: Quit".
- */
- package ge;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- /**
- * ImageViewer is the main class of the image viewer application. It builds
- * and displays the application GUI.
- *
- * To start the application, create an object of this class.
- *
- * @author Michael Kolling and David J Barnes
- * @author hypesystem <[email protected]>
- * @version 0.2 modified by hypesystem
- */
- public class ImageViewer
- implements ActionListener
- {
- private JFrame frame;
- /**
- * Create an ImageViewer show it on screen.
- */
- public ImageViewer()
- {
- makeFrame();
- }
- /**
- * Receive notification of an action. This is deprecated, but would work, if
- * makeFrame set the actionlisteners of items to this
- * (addActionListener(this)).
- * @param event Details of the action.
- */
- @Override
- public void actionPerformed(ActionEvent event)
- {
- System.out.println("Item: " + event.getActionCommand());
- if(event.getActionCommand().equals("Open")) openFile();
- else if(event.getActionCommand().equals("Save")) saveFile();
- else if(event.getActionCommand().equals("Quit")) quit();
- else System.out.println("Item called not known");
- }
- /**
- * part of deprecated actionPerformed logic
- */
- private void openFile() {
- //...
- }
- /**
- * part of deprecated actionPerformed logic
- */
- private void saveFile() {
- //...
- }
- /**
- * part of deprecated actionPerformed logic
- */
- private void quit() {
- frame.dispose();
- }
- // ---- swing stuff to build the frame and all its components ----
- /**
- * Create the Swing frame and its content.
- */
- private void makeFrame()
- {
- frame = new JFrame("ImageViewer");
- makeMenuBar(frame);
- Container contentPane = frame.getContentPane();
- JLabel label = new JLabel("I am a label. I can display some text.");
- contentPane.add(label);
- // building is done - arrange the components and show
- frame.pack();
- frame.setVisible(true);
- }
- /**
- * Create the main frame's menu bar.
- * @param frame The frame that the menu bar should be added to.
- */
- private void makeMenuBar(JFrame frame)
- {
- JMenuBar menubar = new JMenuBar();
- frame.setJMenuBar(menubar);
- // create the File menu
- JMenu fileMenu = new JMenu("File");
- menubar.add(fileMenu);
- JMenuItem openItem = new JMenuItem("Open");
- openItem.addActionListener(new OpenActionListener());
- fileMenu.add(openItem);
- JMenuItem saveItem = new JMenuItem("Save");
- saveItem.addActionListener(new SaveActionListener());
- fileMenu.add(saveItem);
- // added seperator to seperate the categories of menu items
- fileMenu.add(new JPopupMenu.Separator());
- JMenuItem quitItem = new JMenuItem("Quit");
- quitItem.addActionListener(new QuitActionListener());
- fileMenu.add(quitItem);
- }
- /**
- * ActionListener performing the actions when clicking the open item.
- */
- class OpenActionListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent event) {
- //...
- }
- }
- /**
- * ActionListener performing the actions when clicking the save item.
- */
- class SaveActionListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent event) {
- //...
- }
- }
- /**
- * ActionListener performing the actions when clicking the quit item.
- */
- class QuitActionListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent event) {
- frame.dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment