slide4.4_2009_r1 - Module Module 4.4 Creating User...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Module Module 4.4 Creating User Interfaces EE3206/EE5805 Java Programming & Applications 1 Intended Intended Learning Outcomes To To create graphical user interfaces with various user-interface usercomponents. To To create listeners for various types of events. To To display multiple windows in an application. EE3206/EE5805 Java Programming & Applications 2 Frequently Frequently Used Components Introduces Introduces the frequently used GUI components JButton Component Container JComponent AbstractButton AbstractButton JToggleButton JLabel JTextArea JTextComponent JTextField JComboBox JCheckBox JRadioButton EE3206/EE5805 Java Programming & Applications 3 Buttons Buttons A button is a component that triggers an action event button when clicked. Swing Swing provides regular buttons, toggle buttons (two(twostate button), check box buttons, and radio buttons. The The common features of these buttons are generalized in javax.swing.AbstractButton. EE3206/EE5805 Java Programming & Applications 4 AbstractButton AbstractButton javax.swing.JComponent javax.swing.AbstractButton - action Command: Strin g -text: String -icon: javax.swing.Icon -pressedIcon: javax.swing.Icon -rolloverIcon: javax.swing.Icon -mnemonic: int -horizontalAlignment: int -horizontalTextPosition: in t -verticalAlignment: int -verticalTextPosition: int -borderPainted: boolean The action command of this button. The button’s text (i.e., th e text label on the button). The button’s default icon. This icon is also used as the "pressed" and "disabled" icon if there is no explicitly s et pres sed icon. The pressed icon (displayed when the button is pressed). The rollover icon (displayed when th e mouse is over th e button). The mnemonic k ey value of this button. You can select the button by p ressing the ALT key and the mnemonic key at the same time. The horizontal alignment of the icon and text (default: CENTER). The horizontal text position relative to the icon (default: RIGHT). The vertical alignment of the icon and text (default: CENTER). The vertical text position relative to the icon (default: CENTER). Indicates whether the border of the button is painted. By default, a regular button’s border is painted, but the borders for a check box and a radio button is not painted. The gap between the text and the icon on the button (JDK 1.4). The state of the button. True if the check box or radio button is selected, false if it's not. EE3206/EE5805 Java Programming & Applications 5 The get and set methods for these data fields are provided in the class, but omitted in the UML d iagram for brevity. -iconTextGap : int -selected: boolean JButton JButton Constructors and Properties JButton JButton inherits AbstractButton and provides several constructors to create buttons. buttons. Constructor Constructor JButton() JButton() JButton(String JButton(String text) JButton(String JButton(String text, Icon icon) JButton(Icon JButton(Icon icon) Properties Properties text text icon icon Mnemonic Mnemonic (shortcut key) horizontalAlignment horizontalAlignment verticalAlignment verticalAlignment horizontalTextPosition horizontalTextPosition verticalTextPosition verticalTextPosition iconTextGap iconTextGap javax.swing.AbstractButton javax.swing.JButton +JButton() +JButton(icon: javax.swing.Icon) +JButton(text: String) +JButton(text: String, icon: Icon) Creates a default button with no text and icon. Creates a button with an icon. Creates a button with text. Creates a button with text and an icon. EE3206/EE5805 Java Programming & Applications 6 Default Default Icons, Pressed Icon, and Rollover Icon A regular button has a default icon, pressed icon, and rollover icon. icon. Normally, you use the default icon. All other icons are for special effects. A pressed icon is displayed when a button is pressed and a rollover icon is displayed when the mouse is over the button but not pressed. (A) Default icon (B) Pressed icon (C) Rollover icon TestButtonIcons EE3206/EE5805 Java Programming & Applications 7 Horizontal Horizontal Alignments Horizontal alignment specifies how the icon and text are placed Horizontal horizontally on a button. You can set the horizontal alignment using one of the five constants: You LEADING, LEFT, CENTER, RIGHT, TRAILING. At At present, LEADING and LEFT are the same and TRAILING and RIGHT are the same. These These constants are inherited from the SwingConstants interface. SwingConstants The The default horizontal alignment is SwingConstants.CENTER. EE3206/EE5805 Java Programming & Applications 8 Vertical Vertical Alignments Vertical alignment specifies how the icon and text are placed vertically Vertical on a button. You can set the vertical alignment using one of the three constants: TOP, CENTER, BOTTOM. The The default vertical alignment is SwingConstants.CENTER. EE3206/EE5805 Java Programming & Applications 9 Horizontal Horizontal Text Positions Horizontal text position specifies the horizontal position of the text Horizontal relative to the icon. You can set the horizontal text position using one of the five constants: You LEADING, LEFT, CENTER, RIGHT, TRAILING. The The default horizontal text position is SwingConstants.RIGHT. EE3206/EE5805 Java Programming & Applications 10 Vertical Vertical Text Positions Vertical text position specifies the vertical position of the text relative Vertical to the icon. You can set the vertical text position using one of the three constants: You TOP, CENTER, BOTTOM. The The default vertical text position is SwingConstants.CENTER. EE3206/EE5805 Java Programming & Applications 11 Example: Example: Using Buttons Write a program that displays a message on a panel and uses two buttons, <= and =>, to move the message on the panel to the left or right. ButtonDemo MessagePanel JButton JButton EE3206/EE5805 Java Programming & Applications 12 JCheckBox JCheckBox JCheckBox inherits all the properties such as text, icon, mnemonic, verticalAlignment, horizontalAlignment, horizontalTextPosition, verticalTextPosition, and selected from AbstractButton, and provides several constructors to create check boxes. javax.swing.AbstractButton javax.swing.JToggleButton javax.swing.JCheckBox +JCheckBox() +JCheckBox(text: String) +JCheckBox(text: String, selected: boolean) +JCheckBox(icon: Icon) +JCheckBox(text: String, icon: Icon) +JCheckBox(text: String, icon: Icon, selected: boolean) Creates a default check box button with no text and icon. Creates a check box with text. Creates a check box with text and specifies whether the check box is initially selected. Creates a checkbox with an icon. Creates a checkbox with text and an icon. Creates a check box with text and an icon, and specifies whether the check box is initially selected. 13 EE3206/EE5805 Java Programming & Applications Example: Example: Using Check Boxes Add three check boxes named Centered, Bold, and Italic into the previous example to let the user specify specify whether the message is is centered, bold, or italic. JFrame ButtonDemo CheckBoxDemo CheckBoxDemo EE3206/EE5805 Java Programming & Applications 14 JRadioButton JRadioButton Radio buttons are variations of check boxes. They are often used in the group, where only one button is checked at a time. They can be grouped with the time. following code: ButtonGroup btg = new ButtonGroup(); btg.add(jrb1); btg.add(jrb2); javax.swing.AbstractButton j avax.swing.JToggleButton javax.swing.JRadioButton + JRadioButton() +JRadioButton(text: String) +JRadioButton(text: String, selected: boolean) +JRadioButton(icon: Icon) +JRadioButton(text: String, icon: Icon) +JRadioButton(text: String, icon: Icon, selected: boolean) C reates a default radio button with no text and icon. Creates a radio button with text. Creates a radio button with text and specifies whether the radio button is initially selected. Creates a radio button with an icon. Creates a radio button with text and an icon. Creates a radio button with text and an icon, and specifies whether the radio button is initially selected. 15 EE3206/EE5805 Java Programming & Applications Example: Example: Using Radio Buttons Add three radio buttons named Red, Green, and Blue into the preceding example to let the user choose the color of the message. JFrame ButtonDemo CheckBoxDemo RadioButtonDemo RadioButtonDemo EE3206/EE5805 Java Programming & Applications 16 JLabel JLabel A label is a display area for a short text, an image, or both. javax.swing.JComponent javax.swing.JLabel -text: String -icon: javax.swing.Icon -horizontalAlignment: int -horizontalTextPosition: int -verticalAlignment: int -verticalTextPosition: int -iconTextGap: int +JLabel() +JLabel(icon: javax.swing.Icon) +JLabel(icon: Icon, hAlignment: int) +JLabel(text: String) +JLabel(text: String, icon: Icon, hAlignment: int) +JLabel(text: String, hAlignment: int) The label’s text. The label’s image icon. The horizontal alignment of the text and icon on the label. The horizontal text position relative to the icon on the label. The vertical alignment of the text and icon on the label. The vertical text position relative to the icon on the label. The gap between the text and the icon on the label (JDK 1.4). Creates a default label with no text and icon. Creates a label with an icon. Creates a label with an icon and the specified horizontal alignment. Creates a label with text. Creates a label with text, an icon, and the specified horizontal alignment. Creates a label with text and the specified horizontal alignment. 17 The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity. EE3206/EE5805 Java Programming & Applications JLabel JLabel Constructors & Properties The constructors for labels are as follows: JLabel() JLabel(String text, int horizontalAlignment) JLabel(String text) JLabel(Icon icon) JLabel(Icon icon, int horizontalAlignment) JLabel(String text, Icon icon, int horizontalAlignment) JLabel inherits all the properties from JComponent and has many properties similar to the ones in JButton, such as text, icon, horizontalAlignment, verticalAlignment, horizontalTextPosition, verticalTextPosition, and iconTextGap. EE3206/EE5805 Java Programming & Applications 18 Using Using Labels // Create an image icon from image file ImageIcon icon = new ImageIcon("image/grapes.gif"); // Create a label with text, // an icon, with centered horizontal alignment JLabel jlbl = new JLabel("Grapes", icon, SwingConstants.CENTER); // Set label's text alignment and gap between text and icon jlbl.setHorizontalTextPosition(SwingConstants.CENTER); jlbl.setVerticalTextPosition(SwingConstants.BOTTOM); jlbl.setIconTextGap(5); EE3206/EE5805 Java Programming & Applications 19 JTextField JTextField A text field is an input area where the user can type in characters. Text fields are useful in that they enable the user to type in variable data (such as a name or a description). javax.swing.text.JTextComponent -text: String -editable: boolean The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity. The text contained in this text component. Indicates whether this text component is editable (default: true). javax.swing.JTextField -columns: int -horizontalAlignment: int +JTextField() +JTextField(column: int) +JTextField(text: String) +JTextField(text: String, columns: int) The number of columns in this text field. The horizontal alignment of this text field (default: LEFT). Creates a default empty text field with number of columns set to 0. Creates an empty text field with specified number of columns. Creates a text field initialized with the specified text. Creates a text field initialized with the specified text and columns. EE3206/EE5805 Java Programming & Applications 20 JTextField JTextField Constructors & Properties Constructors Constructors JTextField(int JTextField(int columns) Creates an empty text field with the specified number of columns. JTextField(String JTextField(String text) Creates a text field initialized with the specified text. JTextField(String JTextField(String text, int columns) Creates a text field initialized with the specified text and the column size. Properties Properties text text horizontalAlignment horizontalAlignment editable editable columns columns EE3206/EE5805 Java Programming & Applications 21 JTextField JTextField Methods getText() getText() Returns the string from the text field. setText(String setText(String text) Puts the given string in the text field. setEditable(boolean setEditable(boolean editable) Enables or disables the text field to be edited. By default, editable is true. editable true setColumns(int) setColumns(int) Sets the number of columns in this text field. The length of the text field is changeable. EE3206/EE5805 Java Programming & Applications 22 Example: Example: Using Text Fields Add a text field to the preceding example to let the user set a new message. ButtonDemo CheckBoxDemo RadioButtonDemo TextFieldDemo JFrame TextFieldDemo EE3206/EE5805 Java Programming & Applications 23 JTextArea JTextArea If you want to let the user enter multiple lines of text, you cannot use text fields unless you create several of them. The solution is to use JTextArea JTextArea, which enables the user to enter multiple lines of text. javax.swing.text.JTextComponent javax.swing.JTextArea -columns: int -rows: int -tabSize: int -lineWrap: boolean -wrapStyleWord: boolean +JTextArea() +JTextArea(rows: int, columns: int) +JTextArea(text: String) The number of columns in this text area. The number of rows in this text area. The number of characters used to expand tabs (default: 8). Indicates whether the line in the text area is automatically wrapped (default: false). Indicates whether the line is wrapped on words or characters (default: false). Creates a default empty text area. Creates an empty text area with the specified number of rows and columns. Creates a new text area with the specified text displayed. The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity. +JTextArea(text: String, rows: int, columns: int) Creates a new text area with the specified text and number of rows and columns. Appends the string to text in the text area. +append(s: String): void +insert(s: String, pos: int): void +replaceRange(s: String, start: int, end: int): void +getLineCount(): int Inserts string s in the specified position in the text area. Replaces partial text in the range from position start to end with string s. Returns the actual number of lines contained in the text area. EE3206/EE5805 Java Programming & Applications 24 JTextArea JTextArea Constructors & Properties Constructors Constructors JTextArea(int JTextArea(int rows, int columns) Creates a text area with the specified number of rows and columns. JTextArea(String JTextArea(String s, int rows, int columns) Creates a text area with the initial text and the number of rows and columns specified. Properties Properties text text editable editable columns columns lineWrap lineWrap wrapStyleWord wrapStyleWord rows rows lineCount lineCount tabSize tabSize EE3206/EE5805 Java Programming & Applications 25 JComboBox JComboBox A combo box is a simple list of items from which the user can choose. It performs basically the same function as a list, but can get only one value. value. javax.swing.JComponent javax.swing.JComboBox +JComboBox() +JComboBox(items: +JComboBox(items: Object) +addItem(item: Object): void +getItemAt(index: int): Object +getItemCount(): int +getSelectedIndex(): int +setSelectedIndex(index: int): void +getSelectedItem(): Object +setSelectedItem(item: Object): void Creates a default empty combo box. Creates a combo box that contains the elements in the specified array. Adds an item to the combo box. Returns the item at the specified index. Returns the number of items in the combo box. Returns the index of the selected item. Sets the selected index in the combo box. Returns the selected item. Sets the selected item in the combo box. +removeItem(anObject: Object): void Removes an item from the item list. Removes the item at the specified index in the combo box. +removeItemAt(anIndex: int): void +removeAllItems(): void Removes all items in the combo box. EE3206/EE5805 Java Programming & Applications 26 JComboBox JComboBox Methods and Event Handler To To add an item to a JComboBox jcbo, use JComboBox jcbo.addItem(Object jcbo.addItem(Object item) To To get an item from JComboBox jcbo, use JComboBox jcbo.getItemAt( jcbo.getItemAt(int index) There two types of events fired by JComboBox When an item is selected or deselected, the itemStateChanged() handler for ItemEvent will be invoked When an item is selected, the actionPerformed() handler for ActionEvent will be invoked. TestComboBoxTextArea EE3206/EE5805 Java Programming & Applications 27 ...
View Full Document

Ask a homework question - tutors are online