CSE 219 Computer Science III Graphical User Interface.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
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.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
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.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Java GUI building with Swing. 2 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.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
1 Why layout managers Can we perform layout without them? –Yes. A container’s layout property can be set to null. Absolute positioning: specify size and.
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.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
JFrame and JPanel CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
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.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Computer Science 209 GUIs Model/View/Controller Layouts.
Java layout managers. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public.
Basics of GUI Programming Chapter 11 and Chapter 22.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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:
Dept. of CSIE, National University of Tainan 10/21/2012 Arranging Components on a 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.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
Chapter 7 A First Look at GUI Applications Layout Managers.
A Quick Java Swing Tutorial
Object-Orientated Analysis, Design and Programming
Graphical User Interfaces
Graphical User Interfaces -- GUIs
GUIs Model/View/Controller Layouts
Modern Programming Language Java
Java Swing.
Ellen Walker Hiram College
GUIS: Graphical User Interfaces
Creating Graphical User Interfaces
Steps to Creating a GUI Interface
Graphical User Interface
Presentation transcript:

CSE 219 Computer Science III Graphical User Interface

GUI Examples

Another GUI Example

GUI Graphical User Interface (GUI) –provides user-friendly human interaction Building Java GUIs require use of multiple frameworks: –Java’s GUI component Libraries javax.swing.* –Java’s Event Programming Libraries java.awt.event.* Javax.swing.event.* –Java’s Graphics Programming Libraries java.awt.* java.awt.geom.*

GUI Look vs. Behavior Look –physical appearance –custom component design –containment –layout management Behavior –interactivity –event programmed response

What does a GUI framework do for you? Provides ready made visible, interactive, customizable components –you wouldn’t want to have to code your own window

The JFrame Java’s top-level window –a window that is not contained inside another window Has methods for: –used to specify window to fit screen setExtendedState –specifying a response to clicking window’s ‘X’ setDefaultCloseOperation –specifying size and location (top-left corner) setSize, setLocation (inherited from Component ) Many other useful methods inherited from ancestors

javax.swing.JFrame Class Hierarchy Object Component Container Window Frame JFrame

Useful Inherited Methods for JFrame s Frame –setting window’s icon setIconImage(Image image) images can be loaded via: –Toolkit.getDefaultToolkit.getImage(String fileName) Window –for hiding window hide() –for tightly packing all components inside frame pack() Component –for displaying window setVisible(boolean b)

Defining your own JFrame class import java.awt.Image; import java.awt.Toolkit; import javax.swing.JFrame; public class MyEmptyFrame extends JFrame { public MyEmptyFrame() { super("MyEmptyFrame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Toolkit tk = Toolkit.getDefaultToolkit(); Image frameIcon = tk.getImage("monopoly_guy.jpg"); setIconImage(frameIcon); setExtendedState(MAXIMIZED_BOTH); } Sets frame title Sets frame icon Terminates program when frame closed Maximizes frame

Displaying our MyEmptyFrame 1.Construct a MyEmptyFrame object 2.Call setVisible(true) to make the frame visible, this spawns a thread which: –opens the window –starts an event handling loop for the window the program is then event-driven What’s a thread? –allows different code to run in parallel –more on this in coming lectures

An Executable MyEmptyFrame import java.awt.*; import javax.swing.JFrame; public class MyEmptyFrame extends JFrame { public MyEmptyFrame() { super("MyEmptyFrame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Toolkit tk = Toolkit.getDefaultToolkit(); Image frameIcon = tk.getImage("monopoly_guy.jpg "); setIconImage(frameIcon); setExtendedState(MAXIMIZED_BOTH); } public static void main(String[] args) { MyEmptyFrame frame = new MyEmptyFrame(); frame.setVisible(true); }

Containment A Container object is used to hold GUI components. Like what? –buttons, text fields, text areas, etc. All Swing components (ex: JPanel ) are descendants of the Container class Component s are added to a Container using the add method –when a Component is added to a Container it will appear inside that Container

JPanel s JPanel s are blank components They are useful for two purposes: 1.Drawing on them 2.Laying out other GUI components –JPanel s are used to neatly organize your GUI components (ex: buttons) into separate groupings Either draw on a panel or place components inside it, never both simultaneously

javax.swing.JPanel Class Hierarchy Object Component Container Window Frame JFrame JComponent JPanel

Simple GUI Programming TIP To start creating a GUI using Swing: 1.Define your own class that extends JFrame 2.Make all of your necessary GUI components instance variables buttons, text areas, etc. 3.When your class is constructed, setup the GUI construct all necessary components layout all components inside your frame specify all event handlers (next lecture)

Using a JPanel to organize buttons public class MyYesNoFrame extends JFrame { private JPanel panel = new JPanel(); private JButton yesButton = new JButton("Yes"); private JButton noButton = new JButton("No"); … public MyYesNoFrame() { … panel.add(yesButton); panel.add(noButton); this.add(panel); }

How many GUI components do you see? JButton JPanel JTextArea (usually contained inside a JScrollPane)

Containment hierarchy MyYNOCFrame frame –JPanel northPanel JButton yesButton JButton noButton –JScrollPane jsp JTextArea textArea –JPanel southPanel JButton okButton JButton cancelButton

MyYNOCFrame ’s components import java.awt.*; import javax.swing.*; public class MyYNOCFrame extends JFrame { private JPanel northPanel = new JPanel(); private JPanel southPanel = new JPanel(); private JButton yesButton = new JButton("Yes"); private JButton noButton = new JButton("No"); private JButton okButton = new JButton("Ok"); private JButton cancelButton = new JButton("Cancel"); private JTextArea myTextArea = new JTextArea(); private JScrollPane jsp = new JScrollPane(myTextArea); public MyYNOCFrame() { super("MYYNOCFrame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Toolkit tk = Toolkit.getDefaultToolkit(); Image frameIcon = tk.getImage("NYYankees.jpg"); setIconImage(frameIcon); setExtendedState(MAXIMIZED_BOTH); layoutGUI(); }

Laying out MyYNOCFrame public void layoutGUI() { northPanel.add(yesButton); northPanel.add(noButton); southPanel.add(okButton); southPanel.add(cancelButton); this.add(northPanel, BorderLayout.NORTH); this.add(jsp, BorderLayout.CENTER); this.add(southPanel, BorderLayout.SOUTH); } … Uses a layout manager to position components inside a container

Layout Managers Java layout managers use algorithms to position and size GUI components inside a container –every container has its own layout manager –the layout manager decides how to arrange contents when: a container is made visible a container is resized or moved

LayoutManager An interface in java.awt package –classes that implement LayoutManager define algorithms for placing displaying components inside containers. Some classes that implement LayoutManager : –FlowLayout, BorderLayout, BoxLayout, GridLayout, GridBagLayout Note: for these, setPreferredSize does nothing –What about null ? setPreferredSize specifies exact component dimensions

Layout Management Appearance is determined by: –type of layout manager –order components are added to container –location parameters used for adding components To specify a layout for a Container –use setLayout method –or, use the default layout manager Default Layout Mangers: –JPanel : FlowLayout –JFrame : BorderLayout

FlowLayout Simplest, placed components: –left to right –in order of calls to add –components aligned LEFT, RIGHT, or CENTER –starts a new row if needed JFrame frame = new JFrame("FlowTest"); JPanel panel = new JPanel(); panel.add(new JButton("A")); panel.add(new JButton("B")); panel.add(new JButton("C")); panel.add(new JButton("D")); panel.add(new JButton("E")); frame.add(panel);

JFrame frame = new JFrame("BorderTest"); frame.add(new JButton("North"), BorderLayout.NORTH); frame.add(new JButton("South"), BorderLayout.SOUTH); frame.add(new JButton("East"), BorderLayout.EAST); frame.add(new JButton("West"), BorderLayout.WEST); frame.add(new JButton("Center"), BorderLayout.CENTER); BorderLayout 5 regions ( CENTER, NORTH, SOUTH, EAST, WEST) –1 component allowed in each that component may also contain other components North/south stretched horizontally East/west stretched vertically

Which LayoutManager to use? My advice: keep it simple Use: –BorderLayout to organize panels inside of a JFrame or another JPanel –FlowLayout to organize components inside panels ex: JButton s –sometimes it is useful to place other JPanel s containing components inside JPanel s Using these simple layouts, you can design the layout of many applications (including our project)

JPanel s inside other JPanel s JPanel

MyNestedPanelsFrame ’s components import java.awt.*; import javax.swing.*; public class MyNestedPanelsFrame extends JFrame { … private JPanel southPanel = new JPanel(); private JPanel topPanelInSouthPanel = new JPanel(); private JPanel bottomPanelInSouthPanel = new JPanel(); private JButton okButton = new JButton("Ok"); private JButton cancelButton = new JButton("Cancel"); private JLabel statusLabel = new JLabel("LAYOUT EXAMPLE"); public MyNestedPanelsFrame() { … layoutGUI(); }

Laying out MyNestedPanelsFrame public void layoutGUI() { northPanel.add(yesButton); northPanel.add(noButton); southPanel.setLayout(new BorderLayout()); topPanelInSouthPanel.add(okButton); topPanelInSouthPanel.add(cancelButton); topPanelInSouthPanel.setBackground(Color.RED); bottomPanelInSouthPanel.add(statusLabel); bottomPanelInSouthPanel.setBackground(Color.YELLOW); southPanel.add(topPanelInSouthPanel, "North"); southPanel.add(bottomPanelInSouthPanel, "South"); add(northPanel, BorderLayout.NORTH); add(jsp, BorderLayout.CENTER); add(southPanel, BorderLayout.SOUTH); }

Draw image on Jpanel