Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.

Slides:



Advertisements
Similar presentations
Graphic User Interfaces Layout Managers Event Handling.
Advertisements

TCU CoSc Programming with Java Handling Events.
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Graphical User Interfaces Allow for interaction with –Buttons –Menus –Text Fields Two Java Libraries to assist in GUI Programming –AWT –Swing.
Chapter 121 Window Interfaces Using Swing Chapter 12.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
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.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
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.
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.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
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.
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,
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
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 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.
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.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Layout Managers Arranges and lays out the GUI components on a container.
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.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Computer Science 209 GUIs Model/View/Controller Layouts.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
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.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
Event Handling CS 21a: Introduction to Computing I First Semester,
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.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
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.
GUIs and Events Rick Mercer.
Graphical User Interfaces
Graphical User Interfaces -- GUIs
Swing JComponents.
A First Look at GUI Applications
“Form Ever Follows Function” Louis Henri Sullivan
GUIs Model/View/Controller Layouts
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
Chapter 13: Advanced GUIs and Graphics
Event-driven programming for GUI
GUI building with the AWT
Chapter 7-3 (Book Chapter 14)
GUI building with the AWT
Graphical User Interface
Presentation transcript:

Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction with the user. GUI’s accept input from: the keyboard a mouse. A window in a GUI consists of components that: present data to the user allow interaction with the application.

Introduction Some common GUI components are: buttons, labels, text fields, check boxes, radio buttons, combo boxes, and sliders.

Text Fields provide a line of user-updatable text need to - import javax.swing.*; - declare a variable private JTextField input; - construct input = new JTextField( "Enter text here " ); - place in a window - use. For example, String userInput = input.getText();

import java. awt. ; import javax. swing import java.awt.*; import javax.swing.*; public class GUIDemo extends JApplet { private JTextField input; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); Container contentPane = getContentPane(); contentPane.add( input, BorderLayout.NORTH ); contentPane.validate(); }

Labels JLabel class provided by Swing Single line of read-only text A passive component - no event listener required! Useful for providing labels for sliders, text fields, etc.

Using JLabels Two ways to construct Modifiable with setText method Providing text only JLabel speedLabel = new JLabel("Speed is "); Providing text and justification JLabel speedLabel = new JLabel("Speed is ", JLabel.RIGHT); Modifiable with setText method speedLabel.setText("Speed is " + speed);

import java. awt. ; import javax. swing import java.awt.*; import javax.swing.*; public class GUIDemo2 extends JApplet { private JTextField input; private JLabel label; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); label = new JLabel("label"); Container contentPane = getContentPane(); contentPane.add( input, BorderLayout.NORTH ); contentPane.add( label, BorderLayout.SOUTH ); contentPane.validate(); }

Buttons Let’s add a button to our previous program. Want clicks on the button to trigger a program action Requires slightly more programming effort than text fields

Checklist for using JButtons Construct the JButton Add it to the content pane of the JApplet and validate So that the JApplet responds to events generated by the JButton Add this as a listener Make sure your JApplet implements ActionListener requires to add an actionPerformed method

Action Events When a JButton is clicked an ActionEvent is generated actionPerformed is executed Just as onMouseClick is executed whenever a user clicks on the canvas actionPerformed is provided with a parameter Contains information about the object that triggered the event

import java. awt. ; import java. awt. event. ; import javax. swing import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUIDemo3 extends JApplet implements ActionListener { private JTextField input; private JLabel label; private JButton button; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); label = new JLabel("label"); button = new JButton("click"); button.addActionListener(this); Container contentPane = getContentPane(); contentPane.add( input, BorderLayout.NORTH ); contentPane.add( label, BorderLayout.SOUTH ); contentPane.add( button, BorderLayout.CENTER); contentPane.validate(); }

public void actionPerformed(ActionEvent e){ label. setText(input public void actionPerformed(ActionEvent e){ label.setText(input.getText()); }

Layout of Components in a Container Many layout options One example: BorderLayout (default JApplet contentPane) BorderLayout.NORTH BorderLayout.EAST BorderLayout.SOUTH BorderLayout.WEST BorderLayout.CENTER Sizes everything to fill space

FlowLayout Alternate layout that sequences components left to right, allows to keep natural size

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUIDemo4 extends JApplet implements ActionListener { private JTextField input; private JLabel label; private JButton button; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); label = new JLabel("label"); button = new JButton("click"); button.addActionListener(this); Container contentPane = getContentPane(); contentPane.setLayout( new FlowLayout()); contentPane.add( input); // removed NORTH, SOUTH, etc contentPane.add( label ); contentPane.add( button); contentPane.validate(); } public void actionPerformed(ActionEvent e){ label.setText(input.getText());

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUIDemo5 extends JApplet implements ActionListener { private JTextField input; private JLabel label; private JButton button, button2; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); label = new JLabel("label"); button = new JButton("update"); button.addActionListener(this); button2 = new JButton("font"); button2.addActionListener(this); Container contentPane = getContentPane(); contentPane.setLayout( new FlowLayout()); contentPane.add( input); contentPane.add( label ); contentPane.add( button); contentPane.add( button2); contentPane.validate(); }

Processing multiple buttons Use e.getSource() to tell which was clicked public void actionPerformed(ActionEvent e){ if (e.getSource()==button){ label.setText(input.getText()); } if (e.getSource()==button2){ label.setFont( new Font("Arial",Font.PLAIN,20));

Your Assignment Add more buttons, labels, and text fields buttons: copy box1 to box2 copy box2 to box1 exchange boxes (use a temp string to hold one) google swap two string variables make all the fonts grow by 20 percent (*1.2)

Review Construct the GUI component Add the component to a container (i.e., a panel or the content pane of a window) If a listener is needed Add this as a listener for the GUI component Add the event-handling methods promised by the listener interface