Graphical User Interfaces (Part IV)

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Introduction to Java 2 Programming
User Interfaces II GUI – Awt, Swing, Web
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Introduction to Swing Components Chapter 14. Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program.
Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.
Graphical User Interfaces
1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces.
Chapter 11 Creating User Interfaces F What is JavaBean? F JComponent F JButton  ImageIcon  JLabel  JTextField  JTextArea  JComboBox  JList  JCheckBox.
Graphical User Interfaces Java’s AWT and Swing APIs.
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
Graphical User Interfaces, 2D Graphics & Game Programming.
Swing: the art of the GUI COMP204, Bernhard Pfahringer.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
1 lecture 14 GUI PROGRAMMING IVLecture 15 Graphical User Interfaces (cont.) Overview  GUI Programming (cont.)  Example 1: Creating Multi-layout GUIs.
1 lecture 12Lecture 15 Graphical User Interfaces (cont.) Overview  GUI Programming (cont.)  Example 1: Creating Multi-layout GUIs with Tooltips.  Example.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
1 L46 Advanced GUI Component (1). 2 OBJECTIVES  To create and manipulate sliders, and menus,
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 15 : Swing III King Fahd University of Petroleum & Minerals College of Computer.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Menus Pull-downs and popups. Tooltips –a ToolTip is a context-sensitive text string that is displayed in a popup window when the mouse rests over a particular.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Christopher Budo, Davis Nygren, spencer franks, Luke miller
·  Chapter 29 Menus.
Advanced User Interfaces
Graphical User Interface (pronounced "gooey")
Graphical User Interface
Graphical User Interface
Presentation transcript:

Graphical User Interfaces (Part IV) Overview GUI Programming (Cont’d). Example 1: Creating Multi-layout GUIs with Tooltips. Example 2: Creating Menus with Mnemonics. Example 3: Event Handling with Menus and File Dialogs. Coming Next: Introduction to Threads and Concurrency. GUI Programming (Part IV) 12

GUI Programming (Cont’d) From the preceding lectures, it is clear that to design a GUI, we have to think carefully about: What Containers & Components are required in the application. What layout to use to arrange the Components in the Container(s). What actions the Components should perform. When developing non-trivial GUIs, it is necessary to think about these issues carefully before venturing into development. In Example 1 that follows we make use of JPanels to illustrate how multi-layout windows can be built. Multi-layout windows are pervasive in all interesting applications. For example in a graphics application one requires a multi-grid panel as part of the main graphics window in order to keep and make easily accessible the graphics toolbox items like brushes, ‘erasers’ etc. GUI Programming (Part IV)

GUI Programming (Part IV) The JPanel Container Can be used as a dedicated drawing area Receives mouse events Extended to create new components Combining Swing GUI and drawing in one window can lead to errors Fix problem by separating GUI and graphics GUI Programming (Part IV) 12

Example 1: Multi-Layout GUIs import javax.swing.*;import java.awt.*; class MultiLayout extends JFrame{ private JPanel westPanel = new JPanel(); private JPanel southPanel = new JPanel(); private JButton b1, b2, b3, b4; MultiLayout(){ Container cp = getContentPane(); cp.add("North", new JButton("North")); cp.add("East", new JButton("East")); cp.add("Center", new JButton("Center")); westPanel.setLayout(new GridLayout(3,2)); westPanel.add(new JButton("Grid 1")); westPanel.add(new JButton("Grid 2")); westPanel.add(new JButton("Grid 3")); westPanel.add(new JButton("Grid 4")); westPanel.add(new JButton("Grid 5")); cp.add("West", westPanel); southPanel.setLayout(new FlowLayout()); southPanel.add(b1=new JButton("Flow 1")); southPanel.add(b2=new JButton("Flow 2")); southPanel.add(b3=new JButton("Flow 3")); southPanel.add(b4=new JButton("Flow 4")); b1.setToolTipText("I'm a button labeled "+b1.getText()); cp.add("South", southPanel); setTitle("Multi-Layout GUI-building Sample"); setSize(400,400); show(); } public static void main(String args[]){ new MultiLayout(); }} GUI Programming (Part IV)

GUI Programming (Part IV) Menus Java provides several classes—JMenuBar, JMenu, JMenuItem, JCheckBoxMenuItem, and JRadioButtonMenuItem —to implement menus in a frame. A JFrame or JApplet can hold a menu bar to which the pull-down menus are attached. Menus consist of menu items that the user can select (or toggle on or off). Menu bars can be viewed as a structure to support menus. GUI Programming (Part IV) 12

GUI Programming (Part IV) Menus with Frames Menus Important part of GUIs Perform actions without cluttering GUI Attached to objects of classes that have method setJMenuBar JFrame and Japplet ActionEvents Classes used to define menus JMenuBar - container for menus, manages menu bar JMenuItem - manages menu items Menu items - GUI components inside a menu Can initiate an action or be a submenu Method isSelected GUI Programming (Part IV)

GUI Programming (Part IV) Menu Classes JMenu - manages menus Menus contain menu items, and are added to menu bars Can be added to other menus as submenus When clicked, expands to show list of menu items JCheckBoxMenuItem (extends JMenuItem) Manages menu items that can be toggled When selected, check appears to left of item JRadioButtonMenuItem (extends JMenuItem) When multiple JRadioButtonMenuItems are part of a group (ButtonGroup), only one can be selected at a time When selected, filled circle appears to left of item GUI Programming (Part IV)

GUI Programming (Part IV) Menu Classes (Cont’d) Mnemonics Quick access to menu items (File) Can be used with classes that have subclass javax.swing.AbstractButton Method setMnemonic() JMenu fileMenu = new JMenu("File") fileMenu.setMnemonic('F'); Press Alt + F to access menu Method setSelected(true) Of class AbstractButton Sets button/item to selected state addSeparator() Of class Jmenu Inserts separator line into menu GUI Programming (Part IV)

GUI Programming (Part IV) The JMenuBar Class A menu bar holds menus; the menu bar can only be added to a frame. Following is the code to create and add a JMenuBar to a frame: JFrame f = new JFrame(); f.setSize(300, 200); f.setVisible(true); JMenuBar mb = new JMenuBar(); f.setJMenuBar(mb); GUI Programming (Part IV) 12

GUI Programming (Part IV) The Menu Class You attach menus onto a JMenuBar. The following code creates two menus, File and Help, and adds them to the JMenuBar mb: JMenu fileMenu = new JMenu("File", false); JMenu helpMenu = new JMenu("Help", true); mb.add(fileMenu); mb.add(helpMenu); GUI Programming (Part IV) 12

GUI Programming (Part IV) The JMenuItem Class You add menu items on a menu. The following code adds menu items and item separators in menu fileMenu: fileMenu.add(new JMenuItem("new")); fileMenu.add(new JMenuItem("open")); fileMenu.add(new JMenuItem("-")); fileMenu.add(new JMenuItem("print")); fileMenu.add(new JMenuItem("exit")); GUI Programming (Part IV) 12

GUI Programming (Part IV) Submenus You can add submenus into menu items. The following code adds the submenus “Unix,” “NT,” and “Win95” into the menu item “Software.” JMenu softwareHelpSubMenu = new JMenu("Software"); JMenu hardwareHelpSubMenu = new JMenu("Hardware"); helpMenu.add(softwareHelpSubMenu); helpMenu.add(hardwareHelpSubMenu); softwareHelpSubMenu.add(new JMenuItem("Unix")); softwareHelpSubMenu.add(new JMenuItem("NT")); softwareHelpSubMenu.add(new JMenuItem("Win95")); GUI Programming (Part IV) 12

Example 2: Creating Menus with Mnemonics import javax.swing.*; class DemonstratingMenus extends JFrame { private JMenuBar menuBar = new JMenuBar(); private JMenu fileMenu = new JMenu("File"); private JMenuItem neW, opeN, closE, savE, prinT; private JMenuItem saveCurrent, saveAs, saveAll; DemonstratingMenus(String title){ super(title); setJMenuBar(menuBar); menuBar.add(fileMenu); fileMenu.setMnemonic('F'); fileMenu.add(neW = new JMenuItem ("New")); fileMenu.add(opeN = new JMenuItem ("Open")); opeN.setMnemonic('o'); fileMenu.add(savE = new JMenu ("Save")); savE.add(saveCurrent = new JMenuItem ("Save Current")); savE.add(saveAs = new JMenuItem ("Save As")); savE.add(saveAll = new JMenuItem ("Save All")); GUI Programming (Part IV)

Example 2: Creating Menus with Mnemonics (Cont’d) fileMenu.add(savE); fileMenu.add(prinT = new JCheckBoxMenuItem ("Print")); fileMenu.addSeparator(); fileMenu.add("Quit"); setSize(400,400); show(); } public static void main(String args[]){ new DemonstratingMenus("Demonstrating Menus using Swing Components."); GUI Programming (Part IV)

Example 3: Event Handling with Menus and FileDialogs import javax.swing.*; import java.awt.*; import java.awt.event.*; class HandlingMenuEvents extends JFrame { private JMenuBar menuBar = new JMenuBar(); private JMenu fileMenu = new JMenu("File"); private JMenuItem neW, opeN, closE, savE, prinT, quiT; private JMenuItem saveCurrent, saveAs, saveAll; private JTextField jtxf; GUI Programming (Part IV)

Example 3: Event Handling with Menus and FileDialogs (Cont’d) HandlingMenuEvents(String title) { super(title); setJMenuBar(menuBar); menuBar.add(fileMenu); fileMenu.setMnemonic('F'); fileMenu.add(neW = new JMenuItem ("New")); fileMenu.add(opeN = new JMenuItem ("Open")); opeN.setMnemonic('o'); fileMenu.add(savE = new JMenu ("Save")); savE.add(saveCurrent = new JMenuItem ("Save Current")); savE.add(saveAs = new JMenuItem ("Save As")); savE.add(saveAll = new JMenuItem ("Save All")); fileMenu.add(savE); fileMenu.add(prinT = new JCheckBoxMenuItem ("Print")); fileMenu.addSeparator(); fileMenu.add(quiT = new JMenuItem("Quit")); quiT.setMnemonic('q'); GUI Programming (Part IV)

Example 3: Event Handling with Menus and FileDialogs prinT.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e ) { JFrame fp = new JFrame("Printing Assistant"); JPanel f = new JPanel(); Container cp = fp.getContentPane(); cp.setLayout(new FlowLayout()); f.add(jtxf = new JTextField(23)); jtxf.setText("Sorry, no printer."); cp.add(f); fp.setSize(400,400); fp.show(); } }); opeN.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { JFrame f = new JFrame(); FileDialog fd = new FileDialog(f, "Select File To Open."); fd.show(); quiT.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ System.exit(0); }}); setSize(400,400); show(); public static void main(String args[]){ new HandlingMenuEvents("Demonstrating Menus using Swing Components."); /* Java generates events when the user selects a menu item, presses a key, pushes a command buttom, etc. */ GUI Programming (Part IV)

Example 4: A Useful Exercise. Q. Write a Java application MultiplicationTutor which repeatedly generates simple multiplication questions that can be used by children for self-assisted learning. The GUI of this application consists of three text field components where the questions are displayed, where the user types his answer and where the application responds to the user’s answer (telling him whether he is right or wrong. If the user types a wrong answer, the response box should tell the user to try again and the question should not change. If the user types the correct answer, the response filed should display this and another random question should be generated immediately. Write the program so that the user can stop the exercise appropriately. The figure below gives a sample GUI for this application. GUI Programming (Part IV)