CPSC150 Week 12 Graphical User Interfaces Chapter 11.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Graphical User Interfaces (Part IV)
Graphic User Interfaces Layout Managers Event Handling.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
GUI’s and eventhandlers in java Martin Jagersand.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling Objects First with Java - A Practical.
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.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
CPSC150 JavaLynn Lambert CPSC150 Week 12 InheritanceInterfaces.
Packages, Javadoc, Testing, GUIs Chapter 5 Chapter 6 Class notes.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
Java Programming Chapter 10 Graphical User Interfaces.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
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.
CS Fall 2012, Lab 09 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /20/2015 GUI - Graphical User Interface.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
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.
Review_6 AWT, Swing, ActionListener, and Graphics.
Basics of GUI Programming Chapter 11 and Chapter 22.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces 2.0.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling Objects First with Java - A Practical.
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.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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)
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling © 2017 Pearson Education, Inc. Hoboken,
A Quick Java Swing Tutorial
Graphical User Interfaces
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Building Graphical User Interfaces
Ellen Walker Hiram College
GUIS: Graphical User Interfaces
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Graphical User Interface
Steps to Creating a GUI Interface
1 Graphical User Interfaces
A Quick Java Swing Tutorial
Graphical User Interface
Presentation transcript:

CPSC150 Week 12 Graphical User Interfaces Chapter 11

CPSC150 Designing Program with Graphical User Interfaces Programs have two parts/classes: –the User Interface –the engine In Connect4 –User interface is board and drawing the board –The engine is what does the logic: calculates win/lose etc. GUI is User Interface: how program is shown to user

CPSC150 Implementating GUIs: GUI parts Components –buttons, menus, textfields, etc. Event Handling –instead of executing methods when called (by BlueJ or in code), execute methods when an event occurs (e.g., mouse click on a button) Layout –Where on the screen to put the components. use LayoutManager

CPSC150 GUIs Components –JFrames and Windows creating a Window –Menus adding a Window to the menu Event Handling –handling events that happen in the Window Layouts –arranging components in the Window

CPSC150 Swing Java has AWT (old, Abstract Window Toolkit) and Swing (based on AWT) Swing prefixes components with J –e.g., Button is from AWT, JButton is Swing –We’ll use Swing To use Swing: import javax.swing.*; // note ‘x’

CPSC150 Java GUI The Java GUI packages GUI classes are found in two packages: AWT (Abstract Windows Toolkit) Swing Swing components are based on AWT classes Java 2 released Swing as an alternative to AWT AWT uses platform-specific GUI components Swing is less reliant on platform-specific components more less heavyweight components AWT lightweight components Swing reliance on platform-specific GUI components

CPSC150 Java GUI The Java GUI inheritance hierarchy Object Color Container Component LayoutManager Panel Window Applet Frame Dialog JComponent JApplet JFrame JDialog Swing AWT Components we will use

CPSC150 Java GUI The Java GUI inheritance hierarchy JComponent JMenuBar AbstractButton JScrollPane JPanel JTextComponent JLabel JComboBox JMenuItem JButton JTextArea JTextField JPasswordField … etc. Swing … components we will use

CPSC150 Building a Window Everything is in a top level container (JFrame) JFrame has –Title bar –Menu bar (optional) –Content Pane title bar menu bar content pane Frame (whole window)

CPSC150 Code for a JFrame 3. pack frame. arrange components windowFrame.pack( ) 4. make the frame visible (can also make it invisible, as in quitWord) windowFrame.setVisible(true);

CPSC150 Code for a Frame To build a JFrame, do 4 things (From 150 lab3 GuiInterface.java) 1. Create new JFrame and put a title in private JFrame windowFrame = new JFrame(“Word Jumble 1.0”); 2. Tie parts to JFrame (component, menubar, etc) // if gameWordPanel already exists – type JPanel Container cp = WindowFrame.getContentPane( ); cp.add(gameWordPanel); OR windowFrame.getContentPane().add(gameWordPanel); OR (in Java5): windowFrame.add(gameWordPanel);

CPSC150 JFrame Lots of parts to a JFrame, mostly ContentPane –content panes can hold any number of components –(4 components in windowFrame in lab3) –contentPane (only one) vs JPanel (can have lots) Pre-Java5 –add content (components) to the contentPane –Now, add to the JFrame. adds to contentPane

CPSC150 Some Components JButton JPanel JLabel Lots, look at: /swing/package-summary.html

CPSC150 title bar minimize maximize close come for free component (JPanel) { Also in lab3, what to do on close (click on red X): windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

CPSC150 GUIs JFrames and Windows –creating a Window Menus –adding a Window to the menu Event Handling –handling events that happen in the Window Layouts –arranging components in the Window

CPSC150 Building a Window Everything is in a JFrame JFrame has –Title bar –Menu bar (optional) –Content Pane title bar menu bar content pane Frame (whole window)

CPSC150 Adding menus: 3 parts JMenuBar –Displayed below the title. –Contains the menus. JMenu –e.g. File. Contains the menu items. JMenuItem –e.g. Open. Individual items.

CPSC150 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); } To add JMenuBar to JFrame } To add JMenuItem to JMenu { To add JMenu to JMenuBar {

CPSC150 Struts and Glue Invisible components used as spacing. Available from the Box class. Strut: fixed size. –Component createHorizontalStrut(int width) –Component createVerticalStrut(int height) Glue: fills available space. –Component createHorizontalGlue() –Component createVerticalGlue()

CPSC150 Pushing a menu to the right menu = new JMenu("File"); menubar.add(menu); menu = new JMenu("Filter"); menubar.add(menu); menubar.add(Box.createHorizontalGlue()); menu = new JMenu("Help"); menubar.add(menu); Glue (invisible)

CPSC150 GUIs Components –JFrames and Windows creating a Window –Menus adding a Window to the menu Event Handling –handling events that happen in the Window Layouts –arranging components in the Window

CPSC150 Events We can now put items on a JFrame and arrange them. We need to be able to do something with them. Action Events –mouse click, mouse over, window action, menu choice, etc. General, ActionListener –Something happened –in java.awt.event –( vent/package-summary.html) vent/package-summary.html

CPSC150 “Check” button in Lab 3: add ActionListener event actionPerformed in the outer class class someclass { // inside some method: b = new JButton("Check"); b.addActionListener(this); // "this" is the current class public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); checkWord(); }

CPSC150 Action Listeners: Inner Classes class someclass { // inside some method: b = new JButton("Check"); b.addActionListener(new checkAction( )); private class checkAction implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); checkWord(); } } // end of checkAction } // end of someclass

CPSC150 A third way: anonymous inner classes // Associate actionlistener with button and write actionPerformed b = new JButton("Check"); b.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); checkWord(); } );

CPSC150 Anonymous class elements b.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); checkWord(); } ); Anonymous object creation Actual parameter Class definition

CPSC150 GUIs JFrames and Windows –creating a Window Menus –adding a Window to the menu Event Handling –handling events that happen in the Window Layouts –arranging components in the Window

CPSC150 Layout managers Manage limited space for components. –FlowLayout, BorderLayout, GridLayout, BoxLayout, GridBagLayout (most flexible, hardest) Manage Container objects, e.g. a content pane. Each imposes its own style.

CPSC150 FlowLayout

CPSC150 BorderLayout default for top level containers (JFrame)

CPSC150 GridLayout

CPSC150 BoxLayout Note: no component resizing.

CPSC150 Layout Manager Lab 3 4 JPanels gameWordPanel, gameGuessPanel, gameButtonPanel, gameStatusPanel

CPSC150 Layout Managers – Lab 3 Container contentPane = windowFrame.getContentPane ( ); contentPane.setLayout(new GridLayout(4, 1)); OR windowFrame.getContentPane().setLayout(new GridLayout(4,1)); OR windowFrame.setLayout(new GridLayout(4, 1)); // add panels in order windowFrame.getContentPane().add(gameWordPanel); windowFrame.getContentPane().add(gameGuessPanel); windowFrame.getContentPane().add(gameButtonPanel); windowFrame.getContentPane().add(gameStatusPanel);

CPSC150 Buttons and nested layouts Use JPanels and embedded layouts A GridLayout inside a FlowLayout inside a BorderLayout.