GUI Programming in Java Hao Jiang Boston College April, 2009.

Slides:



Advertisements
Similar presentations
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Advertisements

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Event Handling Events and Listeners Timers and Animation.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
Lecture 19 Graphics User Interfaces (GUIs)
Graphical User Interfaces (GUI) Abstract Windows Toolkit (AWT): java.awt GUI elements: Primitive Button, Label, Checkbox, Scrollbar, etc. Container Panel,
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.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
CSE 219 Computer Science III Graphical User Interface.
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
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 Programming 1 Java Programming II Events, AWT, and Swing.
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.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
CS GUI Frameworks CS 3331 Fall CS 3331 Outline MVC Model GUI components (widgets) Layout managers Handling events.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Object Oriented Programming.  Interface  Event Handling.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Review_6 AWT, Swing, ActionListener, and Graphics.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
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.
1 Lecture 8: User Interface Components with Swing.
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 Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
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 Responding to User Input.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
CSC 205 Programming II Lecture 5 AWT - I.
Welcome To java
Object-Orientated Analysis, Design and Programming
Aum Amriteshwaryai Namah
A First Look at GUI Applications
Java GUI.
A Quick Java Swing Tutorial
CSE 114 – Computer Science I Event Programming
A Quick Java Swing Tutorial
Events, Event Handlers, and Threads
Presentation transcript:

GUI Programming in Java Hao Jiang Boston College April, 2009

Java GUI Programming  GUI indicates Graphics User Interface, which contrasts with the text-only interfaces, e.g. a terminal.  GUI elements include –“window”, “menu”, “button”, “text box”, “check box”… –Key and mouse interactions  “Components” and “events handling” are two key problems in programming GUI.  For GUI programming in Java, we can use AWT (Abstract Window Toolkit) and SWING.

An Empty Window import javax.swing.*; public class SimpleFrame extends JFrame { public SimpleFrame() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { SimpleFrame frame = new SimpleFrame(); frame.setVisible(true); }

Hello World import javax.swing.*; import java.awt.*; public class HelloFrame extends JFrame { public HelloFrame() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new MyPanel()); } public static void main(String[] args) { HelloFrame frame = new HelloFrame(); frame.setVisible(true); } class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("Hello World", 50, 50); }

Drawing in Panel import java.awt.geom.*; class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; Rectangle2D rect = new Rectangle2D.Double(50, 50, 100, 100); g2.draw(rect); Rectangle2D frect = new Rectangle2D.Double(150, 50, 100, 100); g2.setPaint(new Color(255, 0, 0)); g2.fill(frect); Ellipse2D e = new Ellipse2D.Double(); e.setFrame(rect); g2.setPaint(new Color(0, 0, 255)); g2.fill(e); g2.setPaint(new Color(0, 0, 0)); g2.setFont(new Font("Helvetica", Font.BOLD, 40)); g2.drawString("Hello World", 30, 230); }

Display an Image class MyPanel extends JPanel { private Image image; public MyPanel(String imageName) { try { image = ImageIO.read(new File(imageName)); } catch (Exception e) { e.printStackTrace(); } public void paintComponent(Graphics g) { super.paintComponent(g); if (image == null) return; g.drawImage(image, 0, 0, null); } Exception handling try {…} catch (Exception e) { … }

Event Handling  Different events need to be processed in GUI based programs. –Mouse clicked, dragged, pressed, released –Window resized, moved, closed –Slider bar changed –Check box checked –Text box input changed –And many others  In Java, event sources pass event objects to event listeners; event listeners process the event objects and generate some actions.

Event Handling in Java Event Source Event Source Event Listener Event Listener Interface Listener Interface 1 … * >

An Example ActionListener listener = new MyListener(); JButton button = new JButton(“OK”); button.addActionListener(listener); … class MyListener implements ActionListener { public void actionPerformed(ActionEvent event) { // reaction to the button click }

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class ButtonFrame extends JFrame implements ActionListener { public ButtonFrame() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new MyPanel(this)); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("red")) setBackground(Color.RED); if (cmd.equals("blue")) setBackground(Color.BLUE); } public static void main(String[] args) { ButtonFrame frame = new ButtonFrame(); frame.setVisible(true); } Button Event class MyPanel extends JPanel { public MyPanel(ActionListener f) { JButton redButton = new JButton("red"); redButton.addActionListener(f); JButton blueButton = new JButton("blue"); blueButton.addActionListener(f); add(redButton); add(blueButton); }

Using Inner Class import javax.swing.*; import java.awt.*; import java.awt.event.*; public class InnerFrame extends JFrame { public InnerFrame() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new MyPanel()); } class MyListener implements ActionListener { private Color color; public MyListener(Color c) { color = c; } public void actionPerformed(ActionEvent e) { setBackground(color); } Inner class

Using Inner Class (Cont) class MyPanel extends JPanel { public MyPanel() { JButton redButton = new JButton("red"); redButton.addActionListener(new MyListener(Color.RED)); JButton blueButton = new JButton("blue"); blueButton.addActionListener(new MyListener(Color.BLUE)); add(redButton); add(blueButton); } public static void main(String[] args) { InnerFrame frame = new InnerFrame(); frame.setVisible(true); }

More Events and Listeners AWT events: ActionEvent KeyEvent AdjustmentEvent MouseEvent FocusEvent MouseWheelEvent ItemEvent WindowEvent Listeners: ActionListener MouseMotionListener AdjustmentListener MouseWheelListener FocusListener WindowListener ItemListener WindowFocusListener KeyListener WindowStateListener MouseListener Adaptors WindowAdapter

GUI Components import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ComFrame extends JFrame { public ComFrame() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new MyPanel()); } class MyPanel extends JPanel { public MyPanel() { JButton redButton = new JButton("red"); add(redButton); add(new JLabel("Label")); add(new JTextField("test", 5)); add(new JCheckBox("Bold")); } public static void main(String[] args) { ComFrame frame = new ComFrame(); frame.setVisible(true); }

Component Layout class MyPanel extends JPanel { public MyPanel() { setLayout(new BorderLayout()); add(new JButton(""), BorderLayout.NORTH); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(4, 4)); panel.add(new JButton("7")); panel.add(new JButton("8")); panel.add(new JButton("9")); panel.add(new JButton("/")); panel.add(new JButton("4")); panel.add(new JButton("5")); panel.add(new JButton("6")); panel.add(new JButton("*")); panel.add(new JButton("1")); panel.add(new JButton("2")); panel.add(new JButton("3")); panel.add(new JButton("-")); panel.add(new JButton("0")); panel.add(new JButton(".")); panel.add(new JButton("=")); panel.add(new JButton("+")); add(panel, BorderLayout.CENTER); }

Animation

GUI Using StdDraw