CS 112 Introduction to Programming GUI Programming: Swing/Android Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone: 432-6400.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces (Part IV)
Advertisements

CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
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 Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Event Handling Events and Listeners Timers and Animation.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
1 TCSS 143, Autumn 2004 Lecture Notes Graphical User Interfaces Koffman/Wolfgang Appendix C, pp
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
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.
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 GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
CS 112 Introduction to Programming Swing/Android GUI Programming Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
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.
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.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
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.
University of Limerick1 Software Architecture Java Layout Managers.
© 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.
Graphical User Interfaces. Graphical input and output with JOptionPane.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Visual Index of Swing GUI Components
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.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
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.
Graphical User Interface (GUI)
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
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.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Graphical User Interfaces
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Advanced User Interfaces
A Quick Java Swing Tutorial
Chap 7. Building Java Graphical User Interfaces
Chapter 7-3 (Book Chapter 14)
A Quick Java Swing Tutorial
Graphical User Interface
Presentation transcript:

CS 112 Introduction to Programming GUI Programming: Swing/Android Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:

2 Admin  Project m Checkpoint due this coming Thursday m Final report due May 5  Optional assignment 2a/2b posted today m 2a is GUI programming, which we will cover today m 2b depends on using Google Cloud App Engine  Last class tournament day planning?

Recap: Polymorphism  polymorphism: Ability for the same code to be used with different types of objects and behave differently with each m Same reference points to different types of objects m The method used exists in all the objects m The method may behave differently

Recap: Polymorphism through Inheritance  Same reference that can point to different types of objects  A variable of type T can hold an object of class T or descendent of T, e.g., Employee emp = new Employee(“Ed”); emp = new Lawyer(“Larry”); emp = new LegalSecretary(“Lisa”);  The method used exists in pointed to object m If the method is defined in the base class  The method may behave differently m The child class can override the method

Recap: Polymorphic Arrays  A common usage of polymorphism is to define an array of a base type, but different entries refer to different types of objects m To handle a heterogeneous population of objects with uniformity, achieving generic programming

Polymorphic Array index0123 Critter[] critters = { new Ant(), new Cougar(), new Snake(), new Bulldog() }; while (true) for (i=0; i<critters.length; i++) pos = critters[i].getMove(); disp = critters[i].toString(); draw disp at pos Not dependent on any specific critters but only the generic Critter concept

Polymorphic Array public class Staff { private Employee[] staffList; public Staff() { staffList = new Employee[4]; staffList[0] = new Lawyer("Lisa"); staffList[1] = new Secretary("Sally"); staffList[2] = new Marketer("Mike"); staffList[3] = new LegalSecretary("Lynne"); } public void payday() { for (int count = 0; count < staffList.length; count++) { System.out.printf("%-10s:", staffList[count].name()); System.out.printf("$%.2f\n", staffList[count].pay()); System.out.println(" "); } Works on any mix of Employee objects

Recap: Polymorphism through Interface  Same reference points to different types of objects  A variable of interface type T can point to any object implementing T, e.g., Movable mobj = new Bicycle(); mobj.move(); mobj = new Animal(); mobj.move();  The method used exists in pointed to object m If the method is defined in the interface and the object implements the interface  The method may behave differently m The class implements the interface provides its own definition of the method

Summary: General Programming  When defining a class or method (e.g., sorting), think about the essential (most general) properties/behaviors of the objects you require  Implement the class/method for the most general properties so that your design is the most general ! 9

10 Outline  Admin and recap  GUI design overview

11 GUI Overview  Standard Java provides a GUI framework (called Swing) to support the development of GUI applications  Android adapts the standard GUI framework to support the development of Android applications  GUI systems can be diverse, complex  The ease of developing GUI applications using a GUI framework is the key to its success  We will see both Swing and Android, to illustrate the common and evolving design decisions.

 Discussion: Common issues in designing a GUI framework? 12 GUI Programming Framework Goals

13 GUI Programming Framework Goals  Display: how to create and layout GUI components  User interaction: how to specify user action on a GUI component

14 Outline  Admin and recap  Interface  Examples: GUI design o Overview  Display

15 Design for Display  Problem: Re-usability  Basic idea o Design a large number of standard GUI display/view components

Swing: 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.

Swing: 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.

Swing: JFileChooser A special dialog box that allows the user to select one or more files/folders  public JFileChooser()  public JFileChooser(String currentDir)  public int showOpenDialog(Component parent)  public int showSaveDialog(Component parent)  public File getSelectedFile()  public static int APPROVE_OPTION, CANCEL_OPTION Possible result values from showXxxDialog(..). JFileChooser chooser = new JFileChooser(); int result = chooser.showSaveDialog(this); if (result == JFileChooser.APPROVE_OPTION) this.saveData(chooser.getSelectedFile().getName());

Swing: JColorChooser Another special dialog that lets the user pick from a palette of colors  public JColorChooser()  public JColorChooser(Color initial)  public Color showDialog( Component parent, String title, Color initialColor) m returns null if user chose Cancel option

Android 20

21 Design for Display  Problem: o A GUI app often uses multiple components and a major work is to layout the components  Basic idea o Allow a GUI component to be a container:  One can add components to a container o A container has a layout manager to manage how components in it are arranged.

Containers  container: An object that holds components; it also governs their positions, sizes, and resizing behavior.  Containers have the following public methods:  public void add(Component comp) public void add(Component comp, Object info) Adds a component to the container, possibly giving extra information about where to place it.  public void remove(Component comp) Removes the given component from the container.  public void setLayout(LayoutManager mgr) Uses the given layout manager to position the components in the container.  public void validate() You should call this if you change the contents of a container that is already on the screen, to make it re-do its layout.

23 Standard Swing GUI Class Hierarchy (A Very Small Subset) Component Container JComponent JPanelJAbstractButton JButtonJToggleButton JCheckBoxJRadioBox Window Frame JFrame Typically used as top-level standalone container in Swing GUI app. Android is called Activity

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  NOTE: Call setVisible(true) to make a frame appear on screen after creating it. JFrame

JFrame Example  A simple program that creates and shows a JFrame : import java.awt.*; import javax.swing.*; public class SimpleFrame { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setForeground(Color.WHITE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(300, 120)); frame.setTitle("A frame"); frame.setVisible(true); }  Graphical output:

26 Design for Display  Problem: o How does a programmer specify where each component sits in the container, how big each component should be, and what the component should do if the window is resized/moved/maximized/etc?  Basic idea: o Layout managers, which define templates

 Here are several common Java layout managers: Layout managers

Preferred size  Swing component objects each have a certain size they would "like" to be--just large enough to fit their contents (text, icons, etc.)  This is called the preferred size of the component  Some types of layout managers (e.g. FlowLayout ) choose to size the components inside them to the preferred size; others (e.g. BorderLayout, GridLayout ) disregard the preferred size and use some other scheme Buttons at preferred size: Not preferred size:

FlowLayout public FlowLayout()  treats container as a left-to-right, top-to-bottom "page" or "paragraph"  components are given their preferred size both horizontally and vertically  components are positioned in order added  if too long, components wrap around to next line Container panel = new JPanel(new FlowLayout()); panel.add(new JButton("Button 1") );

FlowLayout example import java.awt.*; import javax.swing.*; public class FlowLayoutExample { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(320, 75)); frame.setTitle("Flow layout"); frame.setLayout(new FlowLayout()); frame.add(new JLabel("Type your ZIP Code: ")); frame.add(new JTextField(5)); frame.add(new JButton("Submit")); frame.setVisible(true); }

GridLayout public GridLayout(int rows, int columns)  treats container as a grid of equally-sized rows and columns  components are given equal horizontal / vertical size, disregarding preferred size

GridLayout example import java.awt.*; import javax.swing.*; public class GridLayoutExample { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(300, 120)); frame.setTitle("The grid"); // 2 rows, 3 columns frame.setLayout(new GridLayout(2, 3)); for (int i = 1; i <= 6; i++) { JButton button = new JButton(); button.setText("Button " + i); frame.add(button); } frame.setVisible(true); }

BorderLayout public BorderLayout()  divides container into five regions: NORTH, SOUTH, WEST, EAST, CENTER  NORTH and SOUTH regions expand to fill region horizontally, and use preferred size vertically  WEST and EAST regions expand to fill region vertically, and use preferred size horizontally  CENTER uses all space not occupied by others Container panel = new JPanel(new BorderLayout()); panel.add(new JButton("Button 1 (NORTH)", BorderLayout.NORTH );

BorderLayout example import java.awt.*; import javax.swing.*; public class BorderLayoutExample { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(210, 200)); frame.setTitle("Run for the border"); frame.setLayout(new BorderLayout()); frame.add(new JButton("north"), BorderLayout.NORTH); frame.add(new JButton("south"), BorderLayout.SOUTH); frame.add(new JButton("west"), BorderLayout.WEST); frame.add(new JButton("east"), BorderLayout.EAST); frame.add(new JButton("center"), BorderLayout.CENTER); frame.setVisible(true); }

BoxLayout Box.createHorizontalBox() Box.createVerticalBox()  aligns components in container in a single row or column  components use preferred sizes and align based on their preferred alignment  preferred way to construct a container with box layout: Box.createHorizontalBox(); or Box.createVerticalBox ();

Complex layouts  Can you create a layout like this, using one preceding layout manager?

 Create container within container  Each container has a different layout, and by combining the layouts, more complex / powerful layout can be achieved  In Swing, we typically use JPanel as container to form container hierarchy:  public JPanel() Constructs a panel with a default flow layout.  public JPanel(LayoutManager mgr) Constructs a panel that uses the given layout manager. Solution: composite layout

Composite layout example import java.awt.*; import javax.swing.*; public class TelephonePad { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(250, 200)); frame.setTitle("Telephone"); frame.setLayout(new BorderLayout()); JPanel centerPanel = new JPanel(new GridLayout(4, 3)); for (int i = 1; i <= 9; i++) { centerPanel.add(new JButton("" + i)); } centerPanel.add(new JButton("*")); centerPanel.add(new JButton("0")); centerPanel.add(new JButton("#")); frame.add(centerPanel, BorderLayout.CENTER); JPanel southPanel = new JPanel(new FlowLayout()); southPanel.add(new JLabel("Number to dial: ")); southPanel.add(new JTextField(10)); frame.add(southPanel, BorderLayout.SOUTH); frame.setVisible(true); } }

GUI App Key Entities Display Composite (Layout M.) Display Composite (Layout M.) Display Display Composite (Layout M.) Display Event Handler

40 Design for Display  Problem: extensibility o Allow flexible extension (i.e., creation of new types of display components)  Basic idea o Allow app to override draw method to generate new visual display

41 Class Hierarchy (A Very Small Subset) JComponent JPanelJAbstractButtonJLabelJTextComponent JButtonJToggleButton JCheckBoxJRadioBox JTextField paintComponent() To create a new visual component, extends JComponent and overrides paintComponent() JPasswordField JTextArea

42 Display Extension Example  FaceViewer.java, FaceComponent.java m Extends JButton to customize its look

43 Android Simplification  Restructure layout managers, such as:  You may want to check out more dynamic layout such as ListView

Android: Allow the layout specification to be declarative 44

 Layout m LetterGame/app/src/main/ res/layout/activity_main.xml 45 Example: LetterGame

46 Outline  Admin and recap  Interface  Examples: GUI design o Overview o Display o User interaction

47 GUI Manager  Maintains a collection (think an array) of GUI components (i.e., Critters)  Manages display complexities such as which part is visible, which part is not, …  Manages user interaction complexities such as user clicks, move over components

48 User Interaction  GUI framework (manager) identifies the component (view) over which a user-interaction event happens component MouseMouse Motion Key Touch

Display Component 49 GUI Event Handling Framework and Polymorphism  To allow an app to customize behavior, a display component needs to have a mechanism for the app to provide the method to be invoked. Display Component listener reference App 1App 2 listener reference

50 Events and Listeners Event source Listener This listener object waits for and responds to an event Event

51 Design Decision: Partition Event Types  Problem: many types of events can happen, and a component may not be interested in all of them, but for completeness, the listener needs to implement all methods (callbacks)  Solution: classify events into different categories so that a component can select the types (categories) of events to which it wants to respond. component MouseMouse Motion Key Touch

52 Example : MouseEvent  Some methods in the MouseEvent class Point getPoint (); returns the location of this mouse event int getX (); returns the x coordinate of the location of this mouse event. int getY (); returns the y coordinate of the location of this mouse event. int getClickCount (); returns the number of quick, consecutive clicks represented by this mouse event.

53 MouseListener Interface  Methods in the MouseListener interface: void mousePressed (MouseEvent event); called when the mouse button is pressed down. void mouseReleased (MouseEvent event); called when the mouse button is released. void mouseClicked (MouseEvent event); called if the mouse button is pressed & released at the same location. void mouseEntered (MouseEvent event); called when the mouse pointer passes into a component void mouseExited (MouseEvent event); called when the mouse pointer passes out of a component

54 MouseMotionListener  Methods in the MouseMotionListener interface: void mouseDragged (MouseEvent event); called when the mouse button is pressed on a component and then dragged void mouseMoved (MouseEvent event); called when the mouse button has been moved on a component (with no buttons down)

55 Java GUI Event Handling Steps  If a component wants to respond to a given category events (e.g., events defined in MouseListener), the component should have a delegate (listener object) for that category of events  When an event of that category happened, the GUI manager will invoke the method of the listener object o the listener provides callback  To make sure that an installed listener object can handle a category of events, it should implement the listener interface for that category of event

Installing a Listener  If an object implements a listener interface for a given category of events, it can be installed as the listener object for the category of events 56 public class RubberLinesPanel implements MouseListener, MouseMotionListener { public RuberLinesPanel() { addMouseListener( this ); addMouseMotionListener( this ); … } public void mousePressed(MouseEvent e) {…} // all methods defined in interfaces }

Summary: Framework w/ Event Handler Display Composite (Layout M.) Display Composite (Layout M.) Display Display Composite (Layout M.) Display Event Handler

Example Swing GUI App: RubberBand 58

59 Extension: Event Preprocessing o A component can define logic (think of a method) to map some raw events to more component-meaningful (semantic) events o E.g., allows a button component to map a mouse click, key press or touch to a "meaningful” event such as "button pressed“ o Such events are called ActionEvent in Swing component ActionMouseMouse Motion Key

60 ActionListener  Methods in the ActionListener interface: // part of Java; you don't write this public interface ActionListener { public void actionPerformed(ActionEvent event); }

Installing an Action Listener 61 JButton button = new JButton(“click”); ActionListener listener = new MyListener(); button.addActionListener(listener); public class MyListener implements ActionListener { public void actionPerformed(ActionEvent ae) {…} }

62 Example: ActionEvent  Some methods in the ActionEvent class String getActionCommand (); returns the command strings int getModifiers (); returns the modifier keys held down during this action event. long getWhen (); returns the timestamp of when this event occurred. component ActionMouse Mouse Motion Key

63 Putting it Together: Example  For example, after a user clicks on top of a button component,  if the button component has a Mouse listener object, its mousePressed method will be automatically called  the mouse event is also translated to an ActionEvent object  if the button has a listener object for ActionEvent, the method actionPerformed of the listener object is automatically invoked

Example: Swing InvestmentViewer  Add a button so that each time the user clicks on the button, $10 is added to the bank account 64 See BankAccount.java; InvestmentFrame.java; InvestmentMain.java

65 Android’s Event Listener Interfaces  Android defines a set of event listener interfaces, e.g., m View.OnClickerListener m View.OnLongClickListener m View.OnFocusChangeListener m View.OnKeyListener m View.OnTouchListener  Method in Android’s OnClickListener interface: void onClick(View v); called when the view v has been clicked.

Example: Android Button Set listener: 66 public class HangmanActivity extends Activity implements OnClickListener { private Button ba; private int count; private TextView public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hangman); ba = (Button)findViewById(R.id.ba); ba.setOnClickListener(this); count = 0; displayWord = (TextView)findViewById(R.id.displayWord); } … }

Example: Android Button Handler: public void onClick(View arg0) { if (arg0 == ba) { count ++; displayWord.setText("a count: " + count); }

Android Simplification You can simplify by setting the onClick() property in the UI design, and Android will then generate an anonymous class with the onClick() method for you automatically 68

Example: LetterGame  Listener m LetterGame/app/src/main/ java/com/example/yry/lettergame/MainActivit y.java 69

Summary  Java Swing and Android introduce many programming features to make GUI programming easy/extensible  GUI programming may look daunting initially, but the basic concepts, as we have studied, are quite simple, and clean 70

Backup Slides 71

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

Additional components

A check box is a toggleable button with two states: checked and unchecked A radio button is a button that can be selected; usually part of a group of mutually-exclusive radio buttons (1 selectable at a time)  public JCheckBox / JRadioButton(String text) public JCheckBox(String text, boolean isChecked) Creates checked/unchecked check box with given text.  public boolean isSelected() Returns true if check box is checked.  public void setSelected(boolean selected) Sets box to be checked/unchecked. JCheckBox, JRadioButton

ButtonGroup A logical group of radio buttons that ensures that only one is selected at a time  public ButtonGroup()  public void add(JRadioButton button)  The ButtonGroup is not a graphical component, just a logical group; the RadioButton s themselves are added to the container, not the ButtonGroup

ImageIcon Allows you to put a picture on a button, label or other component  public class ImageIcon implements Icon m public ImageIcon(String filename) m public ImageIcon(URL address)  in JButton, JRadioButton, JCheckBox, JLabel, etc...  constructor that takes an Icon m public void setIcon(Icon) m public void setSelectedIcon(Icon) m public void setRolloverIcon(Icon)

JScrollPane A special container that holds a component, using scrollbars to allow that component to be seen  public JScrollPane(Component comp) Wraps the given component with scrollbars.  After constructing the scroll pane, add the scroll pane to the container, not the original component. frame.add(new JScrollPane(area), BorderLayout.CENTER) ;

The top-level container that holds menus; can be attached to a frame  public JMenuBar()  public void add(JMenu menu) Usage: in JFrame, the following method exists: m public void setJMenuBar(JMenuBar bar) JMenuBar

A menu to hold menu items; menus can contain other menus  public JMenu(String text)  public void add(JMenuItem item)  public void addSeparator()  public void setMnemonic(int mnemonic) JMenu

An entry in a frame's Menu bar, which can be clicked to perform commands  public JMenuItem(String text)  public JMenuItem(String text, Icon icon)  public JMenuItem(String text, int mnemonic)  public void addActionListener( ActionListener al)  public void setAccelerator( KeyStroke ks )  public void setEnabled(boolean b)  public void setMnemonic(int mnemonic) JMenuItem