Upload
curt-ulanday
View
216
Download
0
Embed Size (px)
Citation preview
7/28/2019 WebDeitelCh11_part2_BasicGUI
1/17
BasicGUICo
mponents
Basic GUI Components
Java: How to Program
Chapter 14 (continued)
1
7/28/2019 WebDeitelCh11_part2_BasicGUI
2/17
BasicGUICo
mponents Basic Java GUI
Focus: Event Handling in Java's Graphical User
Interface
GUIs are event driven
2
7/28/2019 WebDeitelCh11_part2_BasicGUI
3/17
BasicGUICo
mponents
3
Swing vs. AWT (14.4)
Original (AWT) GUI components - heavyweight:
rely on local platform's windowing system to determinefunctionality 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
7/28/2019 WebDeitelCh11_part2_BasicGUI
4/17
BasicGUICo
mponents 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:
4
7/28/2019 WebDeitelCh11_part2_BasicGUI
5/17
BasicGUIComponents Setting up Event Handling for GUI Component
1. Must create a class that represents the event handler
and implements an appropriate interfaceknown asan 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
7/28/2019 WebDeitelCh11_part2_BasicGUI
6/17
BasicGUIComponents
6
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.
7/28/2019 WebDeitelCh11_part2_BasicGUI
7/17
BasicGUIComponents
7/28/2019 WebDeitelCh11_part2_BasicGUI
8/17
BasicGUIComponents
7/28/2019 WebDeitelCh11_part2_BasicGUI
9/17
BasicGUIComponents
7/28/2019 WebDeitelCh11_part2_BasicGUI
10/17
BasicGUIComponents 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 anActionEvent 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
7/28/2019 WebDeitelCh11_part2_BasicGUI
11/17
BasicGUIComponents 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
7/28/2019 WebDeitelCh11_part2_BasicGUI
12/17
BasicGUIComponents 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
7/28/2019 WebDeitelCh11_part2_BasicGUI
13/17
BasicGUIComponents
13
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
7/28/2019 WebDeitelCh11_part2_BasicGUI
14/17
BasicGUIComponents Event Handling (14.7-14.8)
Javas event handling model is called the delegation event
model: event processing is delegatedto 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
7/28/2019 WebDeitelCh11_part2_BasicGUI
15/17
BasicGUIComponents
15
Java uses Several Types of Event Listeners
actionListeners:
JButtons (command buttons), JTextFields, JPasswordFields
itemListeners:
JCheckBoxs (true/false values),
JToggleButtons (used with toolbars), and
JRadioButtons (on/off values)
JComboBox (drop-down lists)
Each event listener type has a handler:
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
7/28/2019 WebDeitelCh11_part2_BasicGUI
16/17
BasicGUIComponents
16
Delegation Event Model and Fancy Buttons (14.9)
14 Container c = getContentPane(); // fig 12.10
15 c.setLayout( new FlowLayout() );
// create buttons
18 plainButton = new JButton( "Plain Button" );
19 c.add( plainButton );
21 Icon bug1 = new ImageIcon( "bug1.gif" );
22 Icon bug2 = new ImageIcon( "bug2.gif" );23 fancyButton = new JButton("Fancy Button",bug1);
24 fancyButton.setRolloverIcon( bug2 );
25 c.add( fancyButton );
// innerclass ButtonHandler for button event handling
29 ButtonHandler handler = new ButtonHandler();
30 fancyButton.addActionListener( handler );
31 plainButton.addActionListener( handler );
7/28/2019 WebDeitelCh11_part2_BasicGUI
17/17
BasicGUIComponents
17
An inner class used for button event handling (14.9)
52 private class ButtonHandler implements
ActionListener {
53 public void actionPerformed( ActionEvent e ){
55 JOptionPane.showMessageDialog( null,
56 "You pressed: " + e.getActionCommand() );
if (e.getActionCommand() == "Plain Button")
doSomethingPlain();
if (e.getActionCommand() == "Fancy Button")
doSomethingFancy();
57 }
}
}