Presentation is loading. Please wait.

Presentation is loading. Please wait.

CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)

Similar presentations


Presentation on theme: "CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)"— Presentation transcript:

1 CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)

2 2.0

3 Overview Constructing GUIs Components Event handling

4 AWT and Swing

5 AWT (Abstract Window Toolkit) is a thin layer of code on top of the OS, whereas Swing is much larger. AWTis a thin layer of code on top of the OS, whereas Swing is much larger. Using AWT, you have to implement a lot of things yourself, while Swing has them built in. Swing implements GUI functionality itself rather than relying on the host OS, it can offer a richer environment on all platforms Java runs on. AWT is more limited in supplying the same functionality on all platforms because not all platforms implement the same-looking controls in the same Ways.

6 Swing components are called "lightweight" because they do not require a native OS object to implement their functionality. JDialog and JFrame are heavyweight, because they do have a peer. So components like JButton, JTextArea, etc., are lightweight because they do not have an OS peer. A "peer" is a widget provided by the operating system, such as a button object or an entry field object.

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

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

9 /** * Create the Swing frame and its content. */ private void makeFrame() { frame = new JFrame("ImageViewer"); 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

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

11 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"); fileMenu.add(openItem); JMenuItem quitItem = new JMenuItem("Quit"); fileMenu.add(quitItem); }

12 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.

13 Centralized 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.

14 public class ImageViewer implements ActionListener { … public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if(command.equals("Open")) { … } else if (command.equals("Quit")) { … } … } … private void makeMenuBar(Jframe frame) { … openItem.addActionListener(this); … }

15 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.

16 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.

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

18 Dialog message types ERROR_MESSAGE INFORMATION_MESSAGE WARNING_MESSAGE QUESTION_MESSAGE

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

20 THANK YOU.


Download ppt "CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)"

Similar presentations


Ads by Google