Buttons. Appearance of buttons A button has one of three appearances: Disabled by your program Enabled by your program Enabled by your program and pressed.

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 Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
GUI Tutorial Day 3. Custom Dialog Create, display, hide, retrieve information.
1 Simple Phone Applet Lab Mobile Phone Display Area Send, Menu and End Numbers 0-9 * and #
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Chapter 121 Window Interfaces Using Swing Chapter 12.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
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.
CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser.
MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 21 INTRODUCTORY PROGRAMMING Week 4 Lecture 2 Fonts –Horstmann 4.6 ActionEvents –different events.
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.
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.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
CS Lecture 01 Frames and Components and events Lynda Thomas
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Layout Managers Arranges and lays out the GUI components on a container.
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.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
A cannon game ?. Simple version angle from command line, one shot only Coordinate system is “upside-down”: Use dy(int) method to transform y coordinate:
Creating a GUI Class An example of class design using inheritance and interfaces.
GUI Tutorial 1. A Bit of Philosophy on what to Teach  There are numerous libraries, frameworks, options  Modern GUIs are often developed using XML (e.g.,
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.
Lesson 28: More on the GUI button, frame and actions.
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.
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.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Modular Event Handling
Inner Classes.
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.
GUI building with the AWT
Inner Classes 27-Dec-17.
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Introduction to Event Handling
GUI building with the AWT
A Quick Java Swing Tutorial
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
GUI building with the AWT
Graphical User Interface
Inner Classes 25-Oct-19.
Presentation transcript:

Buttons

Appearance of buttons A button has one of three appearances: Disabled by your program Enabled by your program Enabled by your program and pressed by the user

A style rule for buttons Users expect buttons to do things When the user clicks a button, there should be a visible change in the display –If there isn’t, the user wonders “Did the program recognize my button click?” This is an application of the Principle of Least Surprise: A program should surprise the user as little as possible. –In other words: a program should behave the way the user expects it to behave

Calculator example In the Calculator program, every button click changes the display –Almost: Clicking the Clear button repeatedly leaves the display at 0 (duh!) Clicking a base button ( Oct, Dec, Hex ) changes the number in the display –Unless it’s a small number (less or equal to base) –Even then, some buttons are enabled/disabled –This is a subtle change that might not be noticed –It would be better to display Oct / Dec / Hex somewhere

Button constructors import java.awt.*; new Button() –Constructs a button with no label new Button(String label ) –Constructs a button with the given label

Placing buttons with FlowLayout setLayout(new FlowLayout( )); Button button1 = new Button("Button 1"); add(button1);

Placing buttons with GridLayout setLayout(new GridLayout(2, 3)); Button button1 = new Button("Button 1"); add(button1);

Placing buttons with BorderLayout setLayout(new BorderLayout( )); Button button1 = new Button("Button 1"); add(button1, BorderLayout.NORTH);

Using a layout manager Create a container (usually a Panel ) Send it the message setLayout( layout_manager ) to tell it what kind of layout manager to use Send add messages to the container; the kind of add message depends on the layout manager –If BorderLayout, an extra parameter should be used Example: Panel p = new Panel(); p.setLayout(new GridLayout(3, 4)); p.add(button1);

Adding a listener import java.awt.event.*; button1.addActionListener(new MyButtonListener()); class MyButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { code to execute when button1 is pressed } } MyButtonListener is best implemented as a member class, so that it has full access to the fields of the enclosing class

Adding the same listener to several buttons MyListener listener = new MyButtonListener(); button1.addActionListener(listener); button2.addActionListener(listener); button3.addActionListener(listener); class MyButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { code to execute when button1 is pressed } }

Adding an anonymous listener import java.awt.event.*; button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { code to execute when button1 is pressed }}); An anonymous listener is more convenient if: –The actionPerformed method is short, and –The listener is only used for this one button

Enabling the button A Button is a Component A Component can be enabled or disabled Button inherits this method from Component : public void setEnabled(boolean b) To enable a button: button1.setEnabled(true); To disable a button: button1.setEnabled(false); You will never get an Event from a disabled component

Changing the button’s appearance button.setLabel(String label); –Changes the label on a button button.setBackground(Color color); button.setForeground(Color color); –Changes color of background or text of button –These methods are inherited from Component –May not work on all platforms

Changing the button’s font new Font(String name, int style, int size ) –name: The name of a font. It may be: A font on your system (maybe not on my system), or one of the font types "Serif", "Sans-serif", "Monospaced", "Dialog", and "DialogInput" –style: one of Font.PLAIN, Font.BOLD, Font.ITALIC, or Font.BOLD+Font.ITALIC –size: The point size, such as 10, 12, or 18 button.setFont(Font font ); or panel.setFont(Font font ); // default for contents

Writing the listener To listen for a button click: –Write a class that implements ActionListener –Create an instance of that class –Attach the instance to one or more buttons button1.addActionListener(new MyButtonListener()); To implement ActionListener, you must provide this method: public void actionPerformed(ActionEvent e)

Writing actionPerformed actionPerformed must be public void actionPerformed takes an ActionEvent parameter If the listener is attached to only a single button, you can ignore the ActionEvent If the listener is attached to several buttons, you can use the ActionEvent parameter to discover which button was pressed

Examining an ActionEvent public void actionPerformed(ActionEvent e) An ActionEvent is an EventObject –it inherits a method public Object getSource() –getSource() returns the Object that caused the event –So: if (e.getSource() == button1) {...} Alternatively, an ActionEvent has a method public String getActionCommand() that (for a Button ) returns the label on the button –So: if (e.getActionCommand().equals("Button 1")) {...}

Doing something You define what the button does in your button listener If the listener is an instance of a member class or an anonymous class, you have full access to the enclosing class If the listener is an external class, it may be harder to access the information you need Good luck!

The End