Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week 18 2.0.

Similar presentations


Presentation on theme: "Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week 18 2.0."— Presentation transcript:

1 Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week 18 2.0

2 2 Overview Constructing GUIs Components Event handling

3 3 AWT and Swing

4 4 Creating a frame import java.awt.*; import java.awt.event.*; import javax.swing.*; // comment omitted. public class ImageViewer implements ActionListener { private JFrame frame; /** * Create an ImageViewer and show it on screen. */ public ImageViewer() { makeFrame(); } // rest of class omitted. }

5 5 Elements of a frame Title Menu bar Content pane Window controls

6 6 /** * 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); frame.pack(); frame.setVisible(true); } The content pane

7 7 Adding menus JMenuBar –Displayed below the title. –Contains the menus. JMenu –e.g. File. Contains the menu items. JMenuItem –e.g. Open. Individual items.

8 8 private void makeMenuBar(JFrame frame) { JMenuBar menubar = new JMenuBar(); frame.setJMenuBar(menubar); JMenu fileMenu = new JMenu("File"); menubar.add(fileMenu); JMenuItem openItem = new JMenuItem("Open"); openItem.addActionListener(this); fileMenu.add(openItem); JMenuItem quitItem = new JMenuItem("Quit"); quitItem.addActionListener(this); fileMenu.add(quitItem); }

9 9 Event handling Events correspond to user interactions with components. Components are associated with different event types. –Frames are associated with WindowEvent. –Menus are associated with ActionEvent. Objects can be notified when an event occurs. –Such objects are called listeners.

10 10 Centralised event receipt A single object handles all events. –Implements the ActionListener interface. –Defines an actionPerformed method. It registers as a listener with each component. –item.addActionListener(this) It has to work out which component has dispatched the event.

11 11 public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if(command.equals("Open")) { open(); } if (command.equals("Quit")) { quit(); } public void open() { System.out.println("Unable to open a file."); } public void quit() { System.out.println("Thank you for using ImageViewer."); System.exit(0); }

12 12 Dialogs Modal dialogs block all other interaction. –Forces a response from the user. Non-modal dialogs allow other interaction. –This is sometimes desirable. –May be difficult to avoid inconsistencies.

13 13 JOptionPane standard dialogs Message dialog –Message text plus an OK button. Confirm dialog –Yes, No, Cancel options. Input dialog –Message text and an input field. Variations are possible.

14 14 A message dialog private void showAbout() { JOptionPane.showMessageDialog(frame, "ImageViewer\n" + "Version 0.2", "About ImageViewer", JOptionPane.INFORMATION_MESSAGE); }

15 15 Dialog message types ERROR_MESSAGE INFORMATION_MESSAGE WARNING_MESSAGE QUESTION_MESSAGE PLAIN_MESSAGE

16 16 Review Pre-defined components simplify creation of sophisticated GUIs. Many components recognise user interactions with them. Reactive components deliver events to listeners.


Download ppt "Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week 18 2.0."

Similar presentations


Ads by Google