21
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

Embed Size (px)

Citation preview

Page 1: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

1

Event Driven Programming wirh Graphical User Interfaces (GUIs)

A Crash Course

© Rick Mercer

Page 2: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

2

GUIs

Most applications have graphical user interfaces to respond to user's

Page 3: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

3

A Few Graphical Components

A Graphical User Interface (GUI) presents a graphical view of an application to users

To build a GUI application—Begin with a well-tested model

• like GameTree, MineSweeper, GameOfLife, LinkedPriorityList

—Make graphical components visible to the user

—Ensure the correct things happen for each event• user clicks button, moves mouse, presses enter key, ...

First some Java's graphical components —Windows, buttons, text fields, and text areas

Page 4: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

4

Classes in the swing package

The javax.swing package has components that show in a graphical manner JFrame: window with title, border, menu, buttons JButton: A component that can "clicked" JLabel: A display area for a small amount of text JTextField: Allows editing of a single line of text

Page 5: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

5

Get a "window" to show itself

Have a class extend javax.swing.JFrame —Your new class inherits all the methods and instance

variables for putting a window on the computer

Add a main method to call the constructorLayout Set up the GUI in the constructor

—See program on the next slide

Page 6: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

6

Can you see the "window"?

import javax.swing.*; public class FirstGUI extends JFrame {

public static void main(String[] args) { JFrame aWindow = new FirstGUI(); // Tell the JFrame object to show itself aWindow.setVisible(true); }

public FirstGUI() { layoutGUI(); // The first of three things to do }

private void layoutGUI() { setTitle("Graffiti"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Page 7: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

7

Some JFrame messages

Set the size and location of the window with setSize(200, 150); // 200 pixels wide, 150 pixels tall setLocation(75, 40); // left 75, down 40

Page 8: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

8

Building Graphical Components

Add these three graphical components to the "content pane" of the JFrame

private JLabel aLabel = new JLabel("Change with setText(String)");

private JTextArea textEditor = new JTextArea("You can edit this text ");

private JButton clickMeButton = new JButton("Nobody is listening to me");

Page 9: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

9

Using Null Layout

By default, JFrame objects have only five places where you can add components a 2nd add wipes out the 1st

There are many layout strategies, we'll use null setLayout(null);

Each component added needs it size and location set

Page 10: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

10

Add components to a window

Add the previously constructed components to one of the five areas of a JFrame

// Add components aLabel.setSize(180, 30); aLabel.setLocation(5, 0); add(aLabel); textEditor.setSize(180, 60); textEditor.setLocation(5, 35); add(textEditor); clickMeButton.setSize(180, 30); clickMeButton.setLocation(5, 95); add(clickMeButton);

Page 11: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

11

Other useful methods

String getText get the text stored in components JButton JTextArea TextField

setText(String) changes the text stored in JButton JTextArea TextField

Two methods to wrap at the word level in a JTextArea default is NO wrapping

textEditor.setLineWrap(true); textEditor.setWrapStyleWord(true);

Page 12: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

12

In Class

Complete code on handout to get this layoutFirst volunteer done, type it in

| | | |x pixel..5..........80............170.........270

Page 13: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

13

So what happens next?

You can layout a real pretty GUIYou can click on buttons, enter text into a

text field, move the mouse, press a key—And NOTHING happens

So let’s make something happen …

Page 14: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

14

Java's Event Model

Java can let the operating system notify graphical components of user interaction—Button objects are notified when clicked—JTextField objects with focus know when the user

presses the enter key—A menu item can know that a user selected it

Event driven programs respond to many things• mouse clicks, mouse movements• clicks on hyperlinks, buttons, menu items• Users pressing any key, selecting a list item

Page 15: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

15

Example: Action Events

The buttons, text fields, and menu items do not perform the actions—Instead JButton, JTextField, JMenuItem

objects send actionPerformed messages to other objects

The code that responds to events is often located in actionPerformed methods

We need to have a class that implements the ActionListener interface to guarantee the object has an actionPerformed message

Page 16: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

16

Event Driven Program with GUIs

Key elements of an event-driven GUI—Graphical components

• The screen elements that a user manipulates with the mouse and keyboard JFrame JLabel JButton JScrollbar JMenuItem JTextField JTextArea Jlist ...

—Layout managers • Govern how the components appear on the screen

• Examples FlowLayout GridLayout SpringLayout

—Events • Signals the OS that a user interacted with a GUI component

• Examples: mouse clicks, keys pressed, hyperlinks selected

Page 17: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

17

Java's Event Model

JFrame

JButton

1 Layout Graphical ComponentsJFrame JButton

JTextField JMenuItem

Listener

3 You register objects that waits for messages from graphical components addActionListener

ListenerListener

4 Users interact with these graphical components

2 You write classes that implement the correct interfaceActionListener

Page 18: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

18

No one is "Listening"

Okay, now we have a GUI—but when run, nothing happens

Wanted: An object to listen to the button that understands a specific message such as—actionPerformed

Also need to tell the button who it can send the actionPerfomed message to—Register the listener with this method

addActionListener(ActionListener al)

Page 19: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

19

Handling Events

Add a private inner class that can listen to the event that the component generates

• Your class must implement a listener interface to guarantee that it has the expected methods: First up: ActionListener

In the constructor register an instance of the listener so the component can later send messages (we do not see this) to the listener’s actionPerformed method

• events occur anytime in the future--the listener is listening (waiting for user generated events such as clicking a button or entering text into a text field)

Page 20: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

20

Inner class

Add an inner class —inner classes have access to the enclosing

classes' instance variables

—make it private since no one else needs to know about it

—otherwise you need a separate class that gets the graphical components passed as an argument

Page 21: 1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer

21

Have a class that implements ActionListener

// Inner class to listen to eventsprivate class ButtonListener implements ActionListener { // clickMeButton will call this method once registered (below) public void actionPerformed(ActionEvent anActionEvent) { JOptionPane.showMessageDialog(null, textEditor.getText()); }}

// In the constructor of the class that extends JFrame, // register the instance of the listener so the component // can later send messages to that object ButtonListener aListener = new ButtonListener(); clickMeButton.addActionListener(aListener);

Caution: This is easy to forget. It is not a compile time error