1 GUIs (Graphical User Interfaces) Purpose of these two lectures: Provide you with basics of writing GUI programs in Java, not an in-depth study. Readings.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
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.
Graphical User Interfaces CS 2110 Spring Ivan Sutherland: “Sketchpad”,
Corresponds with Chapter 12
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
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.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Java Swing Chris North cs3724: HCI. AWT to Swing AWT: Abstract Windowing Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* Extends.
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,
CSE 219 Computer Science III Graphical User Interface.
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.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
CS Lecture 01 Frames and Components and events Lynda Thomas
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
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.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
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.
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 Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
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.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
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 12 Apr 2011 GUIS: Graphical User Interfaces Their mouse had a mean time between failure of … a week … it would jam up irreparably, or... jam up on the.
1 Lecture 25 Listening to buttons and mice Quotes by Tony Hoare There are two ways of constructing a software design: (1) make it so simple that there.
Computer Science 209 GUIs Model/View/Controller Layouts.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Java Swing One of the most important features of Java is its ability to draw graphics.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
1 CS211 Lecture 24 GUIs (Graphical User Interfaces) Readings in Weiss: Appendix B does a good job of laying out the basics. Study it! ProgramLive has a.
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:
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.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
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.
Graphical User Interfaces
8 April 2008 GUIS —Graphical User Interfaces
A Quick Java Swing Tutorial
Ellen Walker Hiram College
GUIS: Graphical User Interfaces
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
13 April 2010 GUIS: Graphical User Interfaces
Graphical User Interface
Presentation transcript:

1 GUIs (Graphical User Interfaces) Purpose of these two lectures: Provide you with basics of writing GUI programs in Java, not an in-depth study. Readings in Weiss: Appendix B does a good job of laying out the basics. Study it! Java 1.3 API specs: A reference, to be used whenever you are writing a Java GUI program. Refer to it often!! Java 1.0: GUI package java.awt. Event handling awkward, cumbersome Java 1.1: New event handling model Java 1.2: Added package javax.swing, an extension of awt. Improved GUI components, more flexibility. Programs shown in class are on course website for you to download and try out. Awt: Abstract window toolkit API: Application Programmer Interface. Event: Something that happens on the GUI --a button press, keyboard action, mouse movement, etc.

2 awt versus Swing package java.awt.*; package javax.swing.*; ButtonJButton (extends Button) FrameJFrame (extends Frame) TextFieldJTextField (extends TextField) noJToggleButton Color no Swing really is an extension of awt, providing more flexibility and more features. Many of awt’s classes are actually written in the native windowing system on your computer --they are “heavyweight”. Most of Swing’s classes are written entirely in Java itself --they are “lightweight”. A few, e.g. JFrame, are heavyweight. Many of the Swing API specs point you directly to tutorials that show you how to use them. Very useful.

3 A window with component editable JTextfield uneditable JTextfield JLabel JPanel, green background, nothing painted on it JComboBox JList JRadioButtons These are in a Button- Group JCheckBox JButton each row is a JPanel with components in it

4 Basic Hierarachy of GUI classes Class Component is abstract, cannot be instantiated. But its subclasses can. A Component generally has a position and size, can be painted, can receive input events. Component Container Window Frame JFrame JComponent JPanel JButton JCheckBox JComboBox JLabel JRadioBox JTextArea JTextField JFileChooser Canvas Dialog JDialog

5 Component A Component has a position and a size and can be painted. Methods in a Component --there are many more! c.setSize(width, height); c.setBackGroundColor(color); c.setFont(f); c.show(); Component Container WindowJComponent JPanel Canvas Frame JFrame Dialog JDialog

6 Container: superclass of all Components that can hold other components. Components are generally added to a Container c: c.add(new JButton(“yes”)); c.add(new JButton(“no”), “north”); Basic top-level containers: JWindow: top-level window with no border JFrame: top-level window with border, menubar JDialog: top-level window used for a dialog JPanel, primary use: as a container of other components. Allows one to organize objects into a unit, often to simplify layouts. See this on the next slides. JPanel, secondary use: paint on it with graphics commands --lines, circles, text, etc. (instead of using class Canvas). Container WindowJComponent JPanelJFrameJDialog

7 Producing the window on slide 3 public class GUI extends JPanel implements ActionListener { // Constructor: a demo window for CS211 public GUI() {makeTheObjects(); doTheLayout(); theDrawButton.addActionListener(this); } // Handle the draw button push public void actionPerformed(ActionEvent evt) {...} private GUICanvas theCanvas; private JComboBox theShape; private JList theColor; private JTextField theXCoor; private JTextField theYCoor; private JRadioButton smallPic; private JRadioButton mediumPic; private JRadioButton largePic; private JCheckBox theFillBox; private JButton theDrawButton; private JTextField theMessage; next slides variables that will contain the objects in the window

8 private void makeTheObjects() { theCanvas= new GUICanvas(); theCanvas.setBackground(Color.green); theCanvas.setPreferredSize(new Dimension(99,99)); theShape= new JComboBox( new String[]{"Circle", "Square"}); theColor= new JList(new String[]{"red","blue"}); theColor.setSelectionMode( ListSelectionModel.SINGLE_SELECTION); theColor.setSelectedIndex(0); theXCoor= new JTextField(3); theYCoor= new JTextField(3); ButtonGroup theSize= new ButtonGroup(); smallPic= new JRadioButton("Small", false); mediumPic= new JRadioButton("Medium", true); largePic= new JRadioButton("Large", false); theSize.add(smallPic); theSize.add(mediumPic); theSize.add(largePic); theFillBox= new JCheckBox("Fill"); theFillBox.setSelected(false); theDrawButton= new JButton("Draw"); theMessage= new JTextField(25); theMessage.setEditable(false); }

9 private void doTheLayout(){ JPanel topHalf= new JPanel(); JPanel bottomHalf= new JPanel(); // Lay out the top half topHalf.setLayout(new FlowLayout()); topHalf.add(theCanvas); topHalf.add(new JLabel("Shape")); topHalf.add(theShape); topHalf.add(theColor); topHalf.add(new JLabel("X coor")); topHalf.add(theXCoor); topHalf.add(new JLabel("Y coor")); topHalf.add(theYCoor); // Lay out the bottom half bottomHalf.setLayout(new FlowLayout()); bottomHalf.add(smallPic); bottomHalf.add(mediumPic); bottomHalf.add(largePic); bottomHalf.add(theFillBox); bottomHalf.add(theDrawButton); bottomHalf.add(theMessage); // Lay out the GUI setLayout(new BorderLayout()); add(topHalf, "North"); add(bottomHalf, "South"); }

10 The main program import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TrivialApplication extends JFrame{ public static void main(String args[]) { JFrame jf= new TrivialApplication(); jf.setTitle("GUI Demo"); jf.getContentPane.add(new GUI()); jf.pack(); jf.show(); } Create instance of this frame, give it a title Add a new GUI to frame jf Add a component to a JFrame’s content pane, not to JFrame itself. Don’t ask why; just do it! Tell jf to lay out its components Make frame jf visible on the monitor

11 Methods in components Weiss explains the basic commands for dealing with components like JLabel, JButton,JList; we don’t go into detail here. Also look at API specs JLabel jl: You can change the text whenever you want. jl.setText(“whatever you want”); JCheckBox jc: A checkbox is either checked or unchecked: jc.isSelected(); jc.setSelected(true); or jc.setSelected(false); jc.setLabel(“whatever you want”); ButtonGroup bg: Only one button (e.g. a JCheckBox) in a ButtonGroup can be selected at any time. If the user checks one, the others become unchecked. Simply add buttons to a ButtonGroup bg.add(jc);

12 Layout managers A container is associated with a layout manager, which does the layout of the components in the container. Different layout managers, for different designs. Defaults JPanel: FlowLayout Frame (and JFrame): BorderLayout Setting container c’s layout manager c.setLayout(new FlowLayout()); FlowLayout. Suppose components c1, c2, c3, …, cn are added to a JPanel. The components are placed in that order in a row, from left to right; whenever there is no room, a new row is started. A scrollbar appears for the JPanel if there is no room for all the rows. Make the window width bigger (or smaller), and the number of components in each row change accordingly. BorderLayout: See next slide. GridBagLayout: Gives most flexibility, but is most difficult to use. We won’t cover it.

13 BorderLayout manager Allows placement of 5 components, in 5 places: north, east, south, west, and center. Any of the five components can be a Jpanel, which can contain its own subcomponents. So there is really no limit on how many components can be there. // add component c at position p on panel (or // frame) p. place is one of “north”, “east”, “south”, // “west”, “north” p.add(c, p); Program that produced this window is on next slide.

14 BorderLayout managers import java.awt.*; import javax.swing.*; public class BorderEx extends JFrame { public static void main(String[] pars) { JFrame f= new BorderEx(); // create instance // of this class JPanel p= new JPanel();// Create a JPanel p.setLayout(new BorderLayout()); p.add(new JButton("North"), "North"); p.add(new JButton("East"), "East"); p.add(new JButton("West"), "West"); p.add(new JButton("South"), "South"); p.add(new JButton("Center"), "Center"); f.getContentPane().add(p); f.pack(); f.show(); } class is a JFrame use a BorderLayout add buttons to Jpanel p add panel to f tell f to lay out it’s components make frame visible