Graphical User Interfaces. Graphical input and output with JOptionPane.

Slides:



Advertisements
Similar presentations
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Advertisements

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.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
1 TCSS 143, Autumn 2004 Lecture Notes Graphical User Interfaces Koffman/Wolfgang Appendix C, pp
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 17 Graphical User Interfaces Swing I.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
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,
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
CS Lecture 01 Frames and Components and events Lynda Thomas
1 CSE 331 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S.
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.
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.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
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.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
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.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
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.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Software Construction LAB 08 Java Programming with SWING GUI Builder.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
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.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
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.
Lesson 28: More on the GUI button, frame and actions.
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. Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: – Events:
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:
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
GUIs and Events Rick Mercer.
CompSci 230 S Programming Techniques
A First Look at GUI Applications
Java Swing.
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Timer class and inner classes
A Quick Java Swing Tutorial
Advanced Programming in Java
GUI and Event-Handling Programming
Graphical User Interface
Presentation transcript:

Graphical User Interfaces

Graphical input and output with JOptionPane

3 An option pane is a simple dialog box for graphical input/output advantages: –simple –flexible (in some ways) –looks better than the black box of death disadvantages: –created with static methods; not very object-oriented –not very powerful (just simple dialog boxes) JOptionPane

4 Types of JOptionPane s showMessageDialog(, ) Displays a message on a dialog with an OK button. showConfirmDialog(, ) Displays a message and list of choices Yes, No, Cancel; returns user's choice as an int with one of the following values: –JOptionPane.YES_OPTION –JOptionPane.NO_OPTION –JOptionPane.CANCEL_OPTION showInputDialog(, ) Displays a message and text field for input; returns the user's value entered as a String. –can pass null for the parent to all methods

5 JOptionPane examples 1 showMessageDialog analogous to System.out.println to display a message import javax.swing.*; public class MessageDialogExample { public static void main(String[] args) { JOptionPane.showMessageDialog(null,"How's the weather?"); JOptionPane.showMessageDialog(null, "Second message"); }

6 JOptionPane examples 2 showConfirmDialog analogous to a System.out.print that prints a question, then reading an input value from the user (can only be one of the provided choices) import javax.swing.*; public class ConfirmDialogExample { public static void main(String[] args) { int choice = JOptionPane.showConfirmDialog(null, "Erase your hard disk?"); if (choice == JOptionPane.YES_OPTION) { JOptionPane.showMessageDialog(null, "Disk erased!"); } else { JOptionPane.showMessageDialog(null, "Cancelled."); }

7 JOptionPane examples 3 showInputDialog analogous to a System.out.print that prints a question, then reading an input value from the user (can be any value) import javax.swing.*; public class InputDialogExample { public static void main(String[] args) { String name = JOptionPane.showInputDialog(null, "What's yer name, pardner?"); JOptionPane.showMessageDialog(null, "Yeehaw, " + name); }

8 Onscreen GUI elements Most GUIs are not composed of option panes; they are too limited. Instead, complex GUIs contain the following elements: –frame: A graphical window on the screen. –components: GUI widgets such as buttons or text fields. –containers: Logical groups of components.

9 JFrame example 1 A simple program that creates and shows a JFrame : import javax.swing.*; public class SimpleFrame { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setVisible(true); } Graphical output:

10 JFrame example 2 import java.awt.*; import javax.swing.*; public class SimpleFrame2 { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setForeground(Color.WHITE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(new Point(10, 50)); frame.setSize(new Dimension(300, 120)); frame.setTitle("A frame"); frame.setVisible(true); } Graphical output:

11 JFrame properties JFrame s have the following properties that you can get/set: nametypedescriptionmethods default close operation int what should happen when frame is closed getDefaultCloseOperation, setDefaultCloseOperation icon image Image icon in the window's title bar getIconImage, setIconImage layout LayoutManager how the frame should position its components getLayout, setLayout resizable boolean whether the window can be resized isResizable, setResizable title String window's title bar text getTitle, setTitle

12 Component properties nametypedescriptionmethods background Color background color getBackground, setBackground enabled boolean whether the component can be interacted with isEnabled, setEnabled font Font font used to display any text on the component getFont, setFont foreground Color foreground color getForeground, setForeground location Point (x, y) position of component on screen getLocation, setLocation size Dimension width, height of component getSize, setSize preferred size Dimension width, height that the component wants to be getPreferredSize, setPreferredSize visible boolean whether the component can be seen on screen isVisible, setVisible

13 Swing component hierarchy Graphical components in Java form an inheritance hierarchy: java.lang.Object +--java.awt.Component +--java.awt.Container | +--javax.swing.JComponent | +--javax.swing.JButton | +--javax.swing.JLabel | +--javax.swing.JMenuBar | +--javax.swing.JOptionPane | +--javax.swing.JPanel | +--javax.swing.JTextArea | +--javax.swing.JTextField | +--java.awt.Window +--java.awt.Frame +--javax.swing.JFrame When doing GUI programming, always import these packages: import java.awt.*; import javax.swing.*;

14 Java GUI: AWT and Swing Sun's initial idea: create a set of classes/methods that can be used to write a multi-platform GUI (Abstract Windowing Toolkit, or AWT) –problem: not powerful enough; limited; a bit clunky to use Second edition (JDK v1.2): Swing –a newer library written from the ground up that allows much more powerful graphics and GUI construction Drawback: Both exist in Java now; easy to get them mixed up; still have to use both sometimes!

15 A frame is a graphical window that can be used to hold other components public JFrame() public JFrame(String title) Creates a frame with an optional title. public void setTitle(String text) Puts the given text in the frame’s title bar. public void setDefaultCloseOperation(int op) Makes the frame perform the given action when it closes. Common value: JFrame.EXIT_ON_CLOSE public void add(Component comp) Places the given component or container inside the frame. –How would we add more than one component to the frame? NOTE: Call setVisible(true) to make a frame appear on screen after creating it. JFrame

16 JButton, JLabel The most common component— a button is a clickable onscreen region that the user interacts with to perform a single command A text label is simply a string of text displayed on screen in a graphical program. Labels often give infor- mation or describe other components public JButton(String text) public JLabel(String text) Creates a new button / label with the given string as its text. public String getText() Returns the text showing on the button / label. public void setText(String text) Sets button / label's text to be the given string.

17 JTextField, JTextArea A text field is like a label, except that the text in it can be edited and modified by the user. Text fields are commonly used for user input, where the user types information in the field and the program reads it A text area is a multi-line text field public JTextField(int columns) public JTextArea(int lines, int columns) Creates a new text field the given number of columns (letters) wide. public String getText() Returns the text currently in the field. public void setText(String text) Sets field's text to be the given string.

18 Components example This program attempts to show 2 buttons: import java.awt.*; import javax.swing.*; public class ComponentsExample1 { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(300, 100)); frame.setTitle("A frame"); JButton button1 = new JButton(); button1.setText("I'm a button."); button1.setBackground(Color.BLUE); frame.add(button1); JButton button2 = new JButton(); button2.setText("Click me!"); button2.setBackground(Color.RED); frame.add(button2); frame.setVisible(true); }

19 Layout problems The preceding program added two buttons to the frame, but only one appeared on the screen. layout manager: An object contained inside frames and other graphical containers that decides the position and size of the components inside the container. The default layout manager sizes each component added to occupy the full window space.

20 Changing layouts We can correct the program's appearance by changing the frame's layout manager. Change the layout by calling the setLayout method on the frame and passing a layout manager object. –We will see several layout managers later. –We'll use one called a FlowLayout, which sizes each component to its preferred size and positions them in left-to-right rows. –If the following line is added to the preceding program just before calling setVisible(true), its appearance will be: frame.setLayout(new FlowLayout());

Action events with ActionListener

22 Event-driven programming program's execution is indeterminate on-screen components cause events to occur when they are clicked / interacted with events can be handled, causing the program to respond, driving the execution thru events (an "event-driven" program)

23 Java Event Hierarchy java.lang.Object +--java.util.EventObject +--java.awt.AWTEvent +--java.awt.event.ActionEvent +--java.awt.event.TextEvent +--java.awt.event.ComponentEvent +--java.awt.event.FocusEvent +--java.awt.event.WindowEvent +--java.awt.event.InputEvent +--java.awt.event.KeyEvent +--java.awt.event.MouseEvent import java.awt.event.*;

24 Action events: ActionEvent most common / simple event type in Swing represent an action occurring on a GUI component created by: –button clicks –check box checking / unchecking –menu clicks –pressing Enter in a text field –etc.

25 Listening for events attach a listener to the component listener’s appropriate method will be called when event occurs (e.g. when the button is clicked) for Action events, use ActionListener

26 Writing an ActionListener // part of Java; you don't write this public interface ActionListener { public void actionPerformed(ActionEvent event); } // Prints a message when the button is clicked. public class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent event){ JOptionPane.showMessageDialog(null, "An event occurred!"); }

27 Attaching an ActionListener JButton button = new JButton("button 1"); ActionListener listener = new MyActionListener(); button.addActionListener(listener); now button will print "Event occurred!" when clicked –addActionListener method exists in many Swing components