Sei sulla pagina 1di 17

Basic GUI Components

Basic GUI Components


Java: How to Program Chapter 14 (continued)

Basic GUI Components

Basic Java GUI

Focus: Event Handling in Java's Graphical User Interface GUIs are event driven

Basic GUI Components

Swing vs. AWT (14.4)

Original (AWT) GUI components - heavyweight:


rely on local platform's windowing system to determine functionality and look and feel Look and feel: a components appearance and the way in which the user interacts with it. result: platform-dependent appearance

Swing GUI Components are lightweight:


Written, manipulated and displayed completely in Java not "weighed down" by platform's GUI capabilities

Basic GUI Components

Swing vs. AWT (14.4)


You can include Swing and AWT components on the same GUI. The picture below shows the difference between AWT and Swing:

Basic GUI Components

Setting up Event Handling for GUI Component


1.

Must create a class that represents the event handler and implements an appropriate interfaceknown as an event-listener interface.

In Craps.java, the Craps class implemented Actionlistener and declared the only method for that interface -> actionPerformed, satisfying this step.

2.

Must indicate that an object of the class from Step 1 should be notified when the event occursknown as registering the event handler.

In Craps.java, this step was accomplished (for the roll Jbutton) by the following:
Roll.addActionListener(this); The addActionListener argument is an ActionListener object, which in this case is the current instance of the Craps object
5

Basic GUI Components

Event Registration (14.6)


Consider the following code from Figure 14.9 on the following slides. Note that class TextFieldFrame extends JFrame but does not implement ActionListener (contrary to our Craps.java example).

Instead, class TextFieldHandler implements the ActionListener interface and therefore defines actionPerformed.

Basic GUI Components

Basic GUI Components

Basic GUI Components

Basic GUI Components

Event Registration (14.6)

When you click on a component, it receives focus (cursor appears in textfield)

When the user presses ENTER while one of the JTextFields or JPasswordFields has focus, causes the system to generate an ActionEvent object

ActionEvent objects contain information about the event that just occurred, such as: the event source the text in the text field.

getSource() method returns a reference to the event source

Line 58 in Fig. 14.9 asks, Is the event source textField1?10

Basic GUI Components

Event Registration (14.6)

The ActionEvent is processed by an object that implements the ActionListener interface (in our example: TextFieldHandler) The TextFieldHandler class satisfies #1 in slide 3 But before this can occur, the program must register the TextFieldHandler object as the event handler for the JTextFields and the JPasswordField. TextField1.addActionListener(handler); registers handler (which is of type TextFieldHandler) as the event handler for the TextField1 component Satisfies #2 in slide 3

11

Basic GUI Components

Key Event Handling

Implementing KeyListener will allow you to create a dynamic response to users pressing ANY key
In our last example, only pressing enter would generate an event Must declare the methods: keyPressed (called when any key is pressed) keyTyped (called in response to pressing any key that is not an action key

Action keys:arrows, home, end, page up, page down, any function keyall non-characters

keyReleased (called when any key is released) Must register with addKeyListener(KeyEvent) method
12

Basic GUI Components

Event Handling (14.7-14.8)

Event information is stored in an object that extends AWTEvent:

ActionEvent is the object weve seen in our examples so far

13

Basic GUI Components

Event Handling (14.7-14.8)

Javas event handling model is called the delegation event model:

event processing is delegated to a particular object (the event listener) within a program use of delegation allows more than one event of the same type to be handled by the program (ie., multiple JButton's)

14

Basic GUI Components

Java uses Several Types of Event Listeners

actionListeners:

JButtons (command buttons), JTextFields, JPasswordFields JCheckBoxs (true/false values), JToggleButtons (used with toolbars), and JRadioButtons (on/off values) JComboBox (drop-down lists) more than one component of same listener type can be used, if so, "delegation event model" object (usually an inner class) is used to parse out program's course of action

itemListeners:

Each event listener type has a handler:


15

Basic GUI Components

Delegation Event Model and Fancy Buttons (14.9)


14 15 Container c = getContentPane(); // fig 12.10 c.setLayout( new FlowLayout() ); // create buttons plainButton = new JButton( "Plain Button" ); c.add( plainButton ); Icon bug1 = new ImageIcon( "bug1.gif" ); Icon bug2 = new ImageIcon( "bug2.gif" ); fancyButton = new JButton("Fancy Button",bug1); fancyButton.setRolloverIcon( bug2 ); c.add( fancyButton );

18 19 21 22 23 24 25

// innerclass ButtonHandler for button event handling


29 30 31 ButtonHandler handler = new ButtonHandler(); fancyButton.addActionListener( handler ); plainButton.addActionListener( handler );
16

Basic GUI Components

An inner class used for button event handling (14.9)


52 53 55 56 private class ButtonHandler implements ActionListener { public void actionPerformed( ActionEvent e ){ JOptionPane.showMessageDialog( null, "You pressed: " + e.getActionCommand() ); if (e.getActionCommand() == "Plain Button") doSomethingPlain(); if (e.getActionCommand() == "Fancy Button") doSomethingFancy(); } }

57 }

17

Potrebbero piacerti anche