Download presentation
Presentation is loading. Please wait.
Published byAnne Sparks Modified over 9 years ago
1
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 Chapter 15 Creating User Interfaces
2
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 2 Objectives F To create graphical user interfaces with various user-interface components: JButton, JCheckBox, JRadioButton, JLabel, JTextField, JTextArea, JComboBox, JList, JScrollBar, and JSlider (§15.2 – 15.12). F To create listeners for various types of events (§15.2 – 15.12). F To use borders to visually group user-interface components (§15.2). F To create image icons using the ImageIcon class (§15.3). F To display multiple windows in an application (§15.14).
3
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 3 Components Covered in the Chapter F Introduces the frequently used GUI components F Uses borders and icons
4
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 4 Buttons A button is a component that triggers an action event when clicked. Swing provides 1- regular buttons, 2- toggle buttons, 3-check box buttons, 4-radio buttons.
5
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 5 JButton Constructors The following are JButton constructors: JButton() JButton(String text) JButton(String text, Icon icon) JButton(Icon icon)
6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 6 JButton Properties text F icon F mnemonic F horizontalAlignment F verticalAlignment F horizontalTextPosition F verticalTextPosition F iconTextGap
7
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Set mnemonic keys Set keyboard mnemonics: F jbtLeft.setMnemonic('L'); F jbtRight.setMnemonic('R'); 7
8
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 8 Horizontal Alignments Horizontal alignment specifies how the icon and text are placed horizontally on a button. You can set the horizontal alignment using one of the five constants: LEADING, LEFT, CENTER, RIGHT, TRAILING. At present, LEADING and LEFT are the same and TRAILING and RIGHT are the same. Future implementation may distinguish them. The default horizontal alignment is SwingConstants.TRAILING.
9
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 9 Vertical Alignments Vertical alignment specifies how the icon and text are placed vertically on a button. You can set the vertical alignment using one of the three constants: TOP, CENTER, BOTTOM. The default vertical alignment is SwingConstants.CENTER.
10
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 10 Horizontal Text Positions Horizontal text position specifies the horizontal position of the text relative to the icon. You can set the horizontal text position using one of the five constants: LEADING, LEFT, CENTER, RIGHT, TRAILING. The default horizontal text position is SwingConstants.RIGHT.
11
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 11 Vertical Text Positions Vertical text position specifies the vertical position of the text relative to the icon. You can set the vertical text position using one of the three constants: TOP, CENTER. The default vertical text position is SwingConstants.CENTER.
12
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 12 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.
13
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 13 JRadioButton Radio buttons are variations of check boxes. They are often used in the group, where only one button is checked at a time.
14
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 14 Grouping Radio Buttons ButtonGroup btg = new ButtonGroup(); btg.add(jrb1); btg.add(jrb2);
15
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 GUI Classes (Components) for Handling Text 15
16
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 16 GUI Classes for Handling Text F The Swing GUI classes JLabel, JTextField, and JTextArea deal with text. F A JLabel object displays uneditable text (or image). F A JTextField object allows the user to enter a single line of text. F A JTextArea object allows the user to enter multiple lines of text. It can also be used for displaying multiple lines of uneditable text.
17
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 17 JLabel A label is a display area for a short text, an image, or both.
18
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 18 JLabel Constructors 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)
19
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 19 JLabel Properties 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.
20
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 20 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);
21
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 21 JLabel with an Image JLabel (with an image) JLabel (with a text) JTextField JLabel imgLabel = new JLabel(new ImageIcon("cat.gif")); Frame.add(imgLabel);
22
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 22 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 enter in variable data (such as a name or a description).
23
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 23 JTextField Constructors F JTextField(int columns) Creates an empty text field with the specified number of columns. F JTextField(String text) Creates a text field initialized with the specified text. JTextField(String text, int columns) Creates a text field initialized with the specified text and the column size.
24
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 24 JTextField Properties text F horizontalAlignment F editable F columns
25
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 25 JTextField Methods F getText() Returns the string from the text field. F setText(String text) Puts the given string in the text field. F setEditable(boolean editable) Enables or disables the text field to be edited. By default, editable is true. F setColumns(int) Sets the number of columns in this text field. The length of the text field is changeable.
26
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 26 JTextArea F We use a JTextArea object to display or allow the user to enter multiple lines of text. F The setText method assigns the text to a JTextArea, replacing the current content. F The append method appends the text to the current text. JTextArea textArea = new JTextArea( );... textArea.setText("Hello\n"); textArea.append("the lost "); textArea.append("world"); Hello the lost world JTextArea
27
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 27 We want to create a frame with a JTextField and a JTextArea When the user enters a text in the JTextField, and then presses Enter or clicks the ADD button,the entered text will be appended to the JTextArea When the user clicks CLEAR the text in the JTextArea will be cleared JTextArea JTextField JTextArea
28
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 28 JTextArea Example_TextArea.java
29
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 29 JTextArea Example_TextArea.java
30
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 30 JTextArea Example_TextArea.java
31
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 31 F The state of a text Area after six words are entered.
32
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 32 Adding Scroll Bars to JTextArea F By default a JTextArea does not have any scroll bars. To add scroll bars, we place a JTextArea in a JScrollPane object. JTextArea textArea = new JTextArea();... JScrollPane scrollText = new JScrollPane(textArea);... contentPane.add(scrollText);
33
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 33 With Scroll Bars F A sample Ch14TextFrame3 window when a JScrollPane is used.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.