Interactors For examples, references:

Slides:



Advertisements
Similar presentations
Chapter 10—Event-driven Programs The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Event-driven Programs C H A P T E R 1.
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
TCU CoSc Programming with Java Handling Events.
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.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Graphical User Interfaces Allow for interaction with –Buttons –Menus –Text Fields Two Java Libraries to assist in GUI Programming –AWT –Swing.
Nested for Statements The body of a control statement can contain other statements. Such statements are said to be nested. Many applications require nested.
Chapter 121 Window Interfaces Using Swing Chapter 12.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L04 (Chapter 15) Creating.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
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,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
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.
Layout Managers Arranges and lays out the GUI components on a container.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
Interactors Eric Roberts CS 106A February 19, 2010.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Creating a GUI Class An example of class design using inheritance and interfaces.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
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,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Interactors Eric Roberts CS 106A February 19, 2016.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Programming – Lecture 13 Event-driven Programs (Chapter 10) Java event model Responding to mouse events Rubber-banding, dragging objects Keyboard events.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Building GUIs Alisha Adam and Eric Roberts CS 106A March 4, 2016.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
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.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Programming – Lecture 13 Event-driven Programs (Chapter 10)
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
GUI’s.
Chapter 9: Graphical User Interfaces
A First Look at GUI Applications
Chapter 8: Writing Graphical User Interfaces
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 10—Event-driven Programs
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
CS 106A, Lecture 24 Interactors and NameSurfer
Timer class and inner classes
Five-Minute Review What are the three data structures realized in the Java Collections Framework? What is the difference between strings and text files?
Chapter 5 Processing Input with Applets
Chapter 17 Creating User Interfaces
Chapter 17 Creating User Interfaces
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Interactors For examples, references: https://sites.google.com/a/ku.edu.tr/comp130/archive/fall2016/8interactors 1

Creating a Simple GUI graphical user interface: GUI consisting of buttons and other on-screen controls. These controls are called interactors. Interactors of the Swing library will be used. Two strategies for design: control strip , layout managers.

Creating a Control Strip When you create an instance of any Program subclass, Java divides the window area into five regions as follows: NORTH WEST EAST CENTER SOUTH The CENTER region is typically where the action takes place. A ConsoleProgram adds a console to the CENTER region, and a GraphicsProgram puts a GCanvas there. The other regions are visible only if you add an interactors. They appear in the order in which they were added.

The JButton Class A JButton object looks something like Push Me The constructor for the JButton class is where label is a string telling the user what the button does. The button shown earlier on this slide is therefore created by new JButton(label) JButton pushMeButton = new JButton("Push Me"); When you click on a button, Java generates an ActionEvent object, which in turn invokes a call to actionPerformed in any listeners that are waiting for action events.

The HitchhikerButton Program import acm.program.*; import java.awt.event.*; import javax.swing.*; /* * This program puts up a button on the screen, which triggers a * message inspired by Douglas Adams's novel. */ public class HitchhikerButton extends ConsoleProgram { /* Initializes the user-interface buttons */ public void init() { add(new JButton("Red"), SOUTH); addActionListeners(); } /* Responds to a button action */ public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Red")) { println("Please do not press this button again."); We don't have a run method, the program is asynchronous

Let's get our hands dirty Creating a ConsoleProgram including action buttons: - add buttons to the 4 sides and print which one is clicked on the console. Keywords: JButton, addActionListeners, actionPerformed(ActionEvent e), e.getActionCommand()

Let's get our hands dirty Creating a GraphicsProgram including action buttons: - add buttons to the 4 sides and draw a shape in the middle based on the clicked button. Keywords: JButton, addActionListeners, actionPerformed(ActionEvent e), e.getActionCommand()

Using check boxes and radio buttons Small Medium Large

Remember the Mouse Events 1. Call addMouseListeners. 2. Write new definitions of any listener methods you need. The most common mouse events are shown in the following table, along with the name of the appropriate listener method: mouseClicked( e) mousePressed( e) mouseReleased(e) mouseMoved(e) mouseDragged(e) Called when the user clicks the mouse Called when the mouse button is pressed Called when the mouse button is released Called when the user moves the mouse Called when the mouse is dragged with the button down The parameter e is a MouseEvent object, which gives more data about the event, such as the location of the mouse. 9

Let's implement the following task which uses radio buttons and mouse events

new JSlider(min, max, value) The JSlider Class The user can adjust a JSlider by dragging the slider knob. The simplest form of the JSlider constructor looks like this: new JSlider(min, max, value) where min and max are integers giving the minimum and maximum values of the slider and value is the initial value. You can retrieve the current value by calling getValue.

Let's replace our size buttons with a slider

The JLabel Class add(new JLabel("Small"), SOUTH); DrawStarMap Small Large add(new JLabel("Small"), SOUTH); add(sizeSlider, SOUTH); add(new JLabel("Large"), SOUTH);

The JComboBox Class In some applications, you may need to allow the user to chose among a set of options that would take up too much space on the screen if you listed them all. In such situations, you can use the JComboBox class, which lists the available options in a popup menu that goes away once the selection is made. A JComboBox used to select T-shirt sizes might look like this on the screen: X-Large From the user’s point of view, a JComboBox works like this: Small X-Large Medium Large From the user’s point of view, a JComboBox works like this: X-Large Depressing the mouse brings up a popup menu. Dragging the mouse selects from the different options. Releasing the mouse sets the state to the current option. Given that its purpose is to offer the user a choice of options, the JComboBox interactor is sometimes called a chooser.

Using the JComboBox Interactor The standard constructor for a JComboBox creates an empty interactor that contains no options; you then add the desired options by calling the addItem method for each one. JComboBox sizeChooser = new JComboBox(); sizeChooser.addItem("Small"); sizeChooser.addItem("Medium"); sizeChooser.addItem("Large"); sizeChooser.addItem("X-Large"); sizeChooser.setEditable(false); The code to create the T-shirt size chooser looks like this: The last line prevents the user from typing in some other size. The items in a JComboBox need not be strings but can instead be any object. The label that appears in the popup menu is determined by applying the object’s toString method. The getSelectedItem and setSelectedItem methods allow you to determine and set which item is selected.

Let's use JComboBox as alternative to selecting radio buttons

The JTextField Class You can accept keyboard input in a user interface by using the JTextField class, which provides the user with an area in which it is possible to enter a single line of text. HelloGUI Name Hello, world. Hello, Eric. Eric world

Notes on the JTextField Class The constructor for the JTextField class has the form new JTextField(columns) where columns is the number of text columns assigned to the field (i.e length). The space often appears larger than one might expect, because Java reserves space for the widest characters. You can get and set the string entered in a JTextField by calling the getText and setText methods. A JTextField generates an action event if the user presses the ENTER key in the field. If you want your program to respond to that action event, you need to register the program as an action listener for the field. In the HelloGUI example, the action listener is enable by the statement nameField.addActionListener(this);

The HelloGUI Program We can add one more button for printing import acm.program.*; import java.awt.event.*; import javax.swing.*; /** This class displays a greeting whenever a name is entered */ public class HelloGUI extends ConsoleProgram { public void init() { nameField = new JTextField(10); add(new JLabel("Name"), SOUTH); add(nameField, SOUTH); nameField.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == nameField) { println("Hello, " + nameField.getText()); /* Private instance variables */ private JTextField nameField; We can add one more button for printing

Numeric Fields JTextField subclasses IntField class interprets its text string as an int; DoubleField class interprets the text string as a double. IntField and DoubleField classes export getValue and setValue methods that get and set the numeric value of the field.

Managing GUI Layout So far, the interactors live in control strips on each side of the window. Arranging interactors to form an elegant, easy-to-use interface is a difficult design challenge. One of the factors that complicates the design is the fact that the size of the program window can change over time. A layout that makes sense for a large window may not be appropriate for a small one. Java seeks to solve the problem of changing window size by using layout managers, which are responsible for arranging interactors and other components when the windows that contain them change size.

Various Layout Managers exist in Swing BorderLayout BoxLayout CardLayout FlowLayout GridBagLayout GridLayout GroupLayout SpringLayout

Using the TableLayout Class from ACM The TableLayout manager arranges components into a two- dimensional grid. The TableLayout constructor takes the number of rows and columns in the grid: new TableLayout(rows, columns) Example: TableLayoutExample Button 1 Button 2 Button 3 Button 4 Button 5 Button 6

TemperatureConverter A Temperature Conversion Program The TemperatureConverter program on the next slide uses the TableLayout manager to create a simple user interface for a program that converts temperatures back and forth from Celsius to Fahrenheit. The steps involved in using the program are: Enter an integer into either of the numeric fields. 1 . Hit ENTER or click the conversion button. 2 . Read the result from the other numeric field. 3 . TemperatureConverter Degrees Fahrenheit 212 32 F -> C Degrees Celsius 1 100 10 C -> F C=(F - 32) x (5/9) F=C x 1.8 + 32 Let's implement it, Lec26

Code for the Temperature Converter /** * This program allows users to convert temperatures back and forth * from Fahrenheit to Celsius. */ public class TemperatureConverter extends Program { /* Initializes the graphical user interface */ public void init() { setLayout(new TableLayout(2, 3)); fahrenheitField = new DoubleField(32); celsiusField = new DoubleField(0); add(new JLabel("Degrees Fahrenheit")); add(fahrenheitField); add(new JButton("F -> C")); add(new JLabel("Degrees Celsius")); add(celsiusField); add(new JButton("C -> F")); addActionListeners(); } page 1 of 2 skip code

Code for the Temperature Converter /* Listens for a button action */ public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("F -> C")) { int f = fahrenheitField.getValue(); int c = GMath.round((5.0 / 9.0) * (f - 32)); celsiusField.setValue(c); } else if (cmd.equals("C -> F")) { int c = celsiusField.getValue(); int f = GMath.round((9.0 / 5.0) * c + 32); fahrenheitField.setValue(f); } /* Private instance variables */ private IntField fahrenheitField; private IntField celsiusField; /** * This program allows users to convert temperatures back and forth * from Fahrenheit to Celsius. */ public class TemperatureConverter extends Program { /* Initializes the graphical user interface */ public void init() { setLayout(new TableLayout(2, 3)); fahrenheitField = new IntField(32); fahrenheitField.setActionCommand("F -> C"); fahrenheitField.addActionListener(this); celsiusField = new IntField(0); celsiusField.setActionCommand("C -> F"); celsiusField.addActionListener(this); add(new JLabel("Degrees Fahrenheit")); add(fahrenheitField); add(new JButton("F -> C")); add(new JLabel("Degrees Celsius")); add(celsiusField); add(new JButton("C -> F")); addActionListeners(); } page 2 of 2 skip code

Ensure there will be no divide by zero error Calculator Before clicking = After clicking = DoubleField JComboBox JButton JLabel Ensure there will be no divide by zero error