
UCLA PIC 20A Chapter 10 – GUIs Java Programming • JLabel • Event Handling Model • JTextField and JPasswordField • How Event Handling Works zInstructor: Ivo Dinov, • JButton Asst. Prof. In Statistics, Neurology and • JCheckBox and JRadioButton • JComboBox Program in Computing • JList • Multiple-Selection Lists • Mouse Event Handling zTeaching Assistant: Yon Seo Kim, PIC • Adapter Classes • Keyboard Event Handling • Layout Managers University of California, Los Angeles, Summer 2002 1. FlowLayout 2. BorderLayout http://www.stat.ucla.edu/~dinov/ 3. GridLayout PIC 20A, UCLA, Ivo Dinov Slide 1 Slide 2 PIC 20A, UCLA, Ivo Dinov Introduction Introduction z Graphical User Interface ("Goo-ee") z Example GUI: Netscape Communicator Text Pictorial interface to a program Menu bar Button Label Menu field YDistinctive "look" and "feel" Different applications with consistent GUIs improve productivity http://www.stat.ucla.edu/~dinov/courses_students.html z GUIs built from components Component: object with which user interacts Examples: Labels, Text fields, Buttons, Checkboxes PIC 20A Slide 3 PIC 20A, UCLA, Ivo Dinov Slide 4 PIC 20A, UCLA, Ivo Dinov Swing Overview Swing Overview z Swing GUI components z Swing component inheritance hierarchy Defined in package javax.swing java.lang.Object Original GUI components from Abstract Windowing java.awt.Component Toolkit in java.awt java.awt.Container YHeavyweight components - rely on local platform's windowing system for look and feel javax.swing.JComponent Swing components are lightweight YWritten in Java, not weighed down by complex GUI YComponent defines methods that can be used in its subclasses (for capabilities of platform example, paint and repaint) Y More portable than heavyweight components YContainer - collection of related components Swing components allow programmer to specify look V When using JFrames, attach components to the content pane (a and feel Container) V YCan change depending on platform Method add YJComponent YCan be same across all platforms - superclass to most Swing components YMuch of a component's functionality inherited from these classes Slide 5 PIC 20A, UCLA, Ivo Dinov Slide 6 PIC 20A, UCLA, Ivo Dinov 1 JLabel Swing Overview z Labels z Some capabilities of subclasses of JComponent T Provide text instructions on a GUI Pluggable look and feel T Read-only text T Shortcut keys (mnemonics) Programs rarely change a label's contents T Class JLabel (subclass of JComponent) YDirect access to components through keyboard http://www.stat.ucla.edu/~dinov/courses_students.html Common event handling Y If several components perform same actions z Methods Tool tips 18 label1 = new JLabel( "Label with text" ); YDescription of component that appears when mouse over it YCan declare label text in constructor T myLabel.setToolTipText( "Text" ) YDisplays "Text"in a tool tip when mouse over label T myLabel.setText( "Text" ) T myLabel.getText() Slide 7 PIC 20A, UCLA, Ivo Dinov Slide 8 PIC 20A, UCLA, Ivo Dinov JLabel JLabel z Icon z Alignment Object that implements interface Icon By default, text appears to right of image JLabel methods setHorizontalTextPosition and setVerticalTextPosition One class is ImageIcon (.gif and .jpeg images) YSpecify where text appears in label YUse integer constants defined in interface SwingConstants 24 Icon bug = new ImageIcon( "bug1.gif" ); (javax.swing) YAssumed same directory as program (more Chapter 16) VSwingConstants.LEFT, RIGHT, BOTTOM, CENTER Display an icon with setIcon method (of class JLabel) z Another JLabel constructor 33 label3.setIcon( bug ); YmyLabel.setIcon( myIcon ); T JLabel( "Text", ImageIcon, YmyLabel.getIcon //returns current Icon Text_Alignment_CONSTANT) Slide 9 PIC 20A, UCLA, Ivo Dinov Slide 10 PIC 20A, UCLA, Ivo Dinov 1 // Fig. 12.4: LabelTest.java 31 label3 = new JLabel(); 2 // Demonstrating the JLabel class. z1. import 32 label3.setText( "Label with icon and text at bottom" ); 3 import javax.swing.*; z 33 label3.setIcon( bug ); 4 import java.awt.*; 1.1 Class Labeltest (extends JFrame) 34 label3.setHorizontalTextPosition( Use a no-argument constructor. Set 5 import java.awt.event.*; z 35 SwingConstants.CENTER ); text, icon, and alignment using 6 1.2 Declarations 36 label3.setVerticalTextPosition( methods. 7 public class LabelTest extends JFrame { z1.3 getContentPane 37 SwingConstants.BOTTOM ); 8 private JLabel label1, label2, label3; 38 label3.setToolTipText( "This is label3" ); 9 z2. Initialize JLabels 39 c.add( label3 ); 10 public LabelTest() 40 11 { z2.1 setToolTipText 41 setSize( 275, 170 ); 12 super( "Testing JLabel" ); 42 show(); 13 Create a Container object, to which we attach 43 } 14 Container c = getContentPane(); JLabel objects (subclass of JComponent). 44 15 c.setLayout( new FlowLayout() ); 45 public static void main( String args[] ) 16 46 { 17 // JLabel constructor with a string argument Initialize text in JLabel constructor. 47 LabelTest app = new LabelTest(); 18 label1 = new JLabel( "Label with text" ); 48 19 label1.setToolTipText( "This is label1" ); Set the tool tip text, and attach 49 app.addWindowListener( 20 c.add( label1 ); component to Container c. 50 new WindowAdapter() { 21 Create a new ImageIcon (assumed to be 51 public void windowClosing( WindowEvent e ) 22 // JLabel constructor with string, Icon and in same directory as program). More 52 { 23 // alignment arguments 53 System.exit( 0 ); 24 Icon bug = new ImageIcon( "bug1.gif" ); Chapter 16. 54 } z 25 label2 = new JLabel( "Label with text and icon", 2.2 setHorizontalText Position 55 } 26 bug, SwingConstants.LEFT ); z setVerticalText Position 56 ); 2.3 27 label2.setToolTipText( "This is label2" ); 57 } z2.3 setToolTipText 28 c.add( label2 ); ImageIcon 58 } z 29 Set and alignment 3. main 30 // JLabel constructor no arguments of text in JLabel constructor. PIC 20A, UCLA, Ivo Dinov Slide 11 PIC 20A, UCLA, Ivo Dinov Slide 12 2 Event Handling Model z GUIs are event driven T Generate events when user interacts with GUI Mouse movements, mouse clicks, typing in a text field, etc. T Event information stored in object that extends AWTEvent z To process an event zProgram Output T Register an event listener Object from a class that implements an event-listener interface (from java.awt.event or javax.swing.event) "Listens" for events T Implement event handler Method called in response to event Event handling interface has one or more methods that must be defined PIC 20A, UCLA, Ivo Dinov Slide 13 Slide 14 PIC 20A, UCLA, Ivo Dinov Event Handling Model JTextField and JPasswordField z Delegation event model z JTextFieldsandJPasswordFields T Use of event listeners in event handling T Single line areas in which text can be entered or displayed T Processing of event delegated to particular object T JPasswordFields show inputted text as an asterisk * z When an event occurs T JTextField extends JTextComponent T GUI component notifies its listeners JPasswordField extends JTextField Calls listener's event handling method z When Enter pressed z Example: T ActionEvent occurs T Enter pressed in a JTextField T Currently active field "has the focus" T Method actionPerformed called for registered listener T Details in following sections Slide 15 PIC 20A, UCLA, Ivo Dinov Slide 16 PIC 20A, UCLA, Ivo Dinov JTextField and JPasswordField JTextField and JPasswordField z Methods z Class ActionEvent T Constructors T JTextField( 10 ) Method getActionCommand VTextfield with 10 columns of text Returns text in JTextField that generated event VTakes average character width, multiplies by 10 T Method getSource JTextField( "Hi" ) getSource returns Component reference VSets text, width determined automatically JTextField( "Hi", 20 ) z Example T setEditable( boolean ) T Create JTextFields and a JPasswordField If false, user cannot edit text Can still generate events T Create and register an event handler T getPassword Use getSource to determine which component had event Class JPasswordField Display a dialog box when Enter pressed Returns password as an array of type char Slide 17 PIC 20A, UCLA, Ivo Dinov Slide 18 PIC 20A, UCLA, Ivo Dinov 3 1 // Fig. 12.7: TextFieldTest.java 31 2 // Demonstrating the JTextField class. z 32 // construct textfield with default text 3 import java.awt.*; 1. import 33 password = new JPasswordField( "Hidden text" ); 4 import java.awt.event.*; z1.1 Declarations 34 c.add( password ); 5 import javax.swing.*; 35 JPasswordField initialized with 6 z1.2 Constructor 36 TextFieldHandler handler = new TextFieldHandler(); 7 public class TextFieldTest extends JFrame { z 37 text1.addActionListener( handler ); text, which appears as asterisks. 8 private JTextField text1, text2, text3; 1.3 GUI components 38 text2.addActionListener( handler ); 9 private JPasswordField password; z 39 text3.addActionListener( handler ); 10 2. Initialize text fields 40 password.addActionListener( handler ); 11 public TextFieldTest() z2.1 setEditable 41 12 { 42 setSize( 325, 100 ); Register event handlers. Good 13 super( "Testing JTextField and JPasswordField" ); 43 show(); practice to use an inner class as an 14 44 } event handler. 15 Container c = getContentPane(); 45 16 c.setLayout( new FlowLayout() ); Create new JTextField 46 public static void main( String args[] ) 17 objects using the various 47 { 18 // construct textfield with default sizing 48 TextFieldTest app = new TextFieldTest(); 19 text1 = new JTextField( 10 ); constructors. 49 20 c.add( text1 ); 50 app.addWindowListener( 21 51 new WindowAdapter() { 22 // construct textfield with default text 52 public void windowClosing( WindowEvent e )
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages17 Page
-
File Size-