Graphical User Interfaces (GUIs) CS 144 John Goettsche Computer - - PowerPoint PPT Presentation

graphical user interfaces guis
SMART_READER_LITE
LIVE PREVIEW

Graphical User Interfaces (GUIs) CS 144 John Goettsche Computer - - PowerPoint PPT Presentation

Graphical User Interfaces (GUIs) CS 144 John Goettsche Computer Science and Computer Engineering 1 GUI Programming Event-driven programming GUI event loop Event listeners Components can generate events 2 CSCE Event


slide-1
SLIDE 1

Computer Science and Computer Engineering

1

Graphical User Interfaces (GUIs)

CS 144 John Goettsche

slide-2
SLIDE 2

CSCE

2

GUI Programming

  • Event-driven programming
  • GUI event loop
  • Event listeners
  • Components can generate events
slide-3
SLIDE 3

CSCE

3

Event Driven Programming

Event loop Event Listener

Event

slide-4
SLIDE 4

CSCE

4

Example: Push Counter

  • Update counter when button is pushed.
  • Components: JButton, JLabel
  • Event: button push
  • Listener: actionListener

After 6 clicks: Initial GUI:

slide-5
SLIDE 5

CSCE

5

Push Counter Components

Panel (JPanel object) Button (JButton object) Label (JLabel object) Frame (JFrame object)

slide-6
SLIDE 6

CSCE

6

Building the GUI: Setting up Components

panel.add( button ); panel.add( label ); frame.add( panel ); The panel

slide-7
SLIDE 7

CSCE

7

Building the GUI: Event Listeners

public void actionPerformed(ActionEvent e) { ... } Code to be executed when event occurs This method is contained in a class. We use

  • bjects of this class to serve as "listener" objects.
slide-8
SLIDE 8

CSCE

8

Building the GUI: Connecting Events to Event Listeners

button.addActionListener( listenerObject ); Object containing code for listener. (The actionPerformed method from the previous slide.)

slide-9
SLIDE 9

CSCE

9

public class MyGUI { private int x; public MyGUI() { ... } private class MyInnerClass { } } Class within a class.

Inner Classes

Available to both MyGUI and MyInnerClass Inner class has access to outer class's fields.

slide-10
SLIDE 10

CSCE

10

public class PushCounter extends JFrame { // The "Push Me" button private JButton pushButton; public PushCounter() { ... pushButton = new JButton( "Push Me!"); pushButton.addActionListener(new PushCounterButtonListener()); ... } private class PushCounterButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { .... } } }

Code to execute when event occurs.

A GUI Framework

Inner class implements the ActionListener interface. Extend JFrame class. Connect button click event to listener object.

slide-11
SLIDE 11

CSCE

11

Inheritance

  • Gain all methods and fields from inherited class.
  • PushCounter is-a JFrame

public class PushCounter extends JFrame { ... } All methods/fields that are in class JFrame are now available in class PushCounter.

slide-12
SLIDE 12

CSCE

12

Implementation of Interfaces

  • Require the creation of certain methods (depending on the interface)
  • ActionListener requires that the actionPerformed method exist.

private class PushCounterButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { .... } }

Must create the actionPerformed method

  • r this will not compile.

Interface

slide-13
SLIDE 13

CSCE

13

Starting Things Up

  • To start a GUI-based program we need only instantiate a JFrame object.
  • Remember, PushCounter is-a JFrame.
  • JVM will automatically enter the event loop.
  • main method may exist anywhere you like.

public static void main( String[] args ) { PushCounter frame = new PushCounter(); } Instantiate PushCounter, which extends JFrame. In GUI programs, the main method is often quite short.

slide-14
SLIDE 14

CSCE

14

Code: PushCounter.java

slide-15
SLIDE 15

CSCE

15

Example: Math Question