• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
JavaYDL17
 

JavaYDL17

on

  • 390 views

 

Statistics

Views

Total Views
390
Views on SlideShare
390
Embed Views
0

Actions

Likes
0
Downloads
18
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    JavaYDL17 JavaYDL17 Presentation Transcript

    • Chapter 17Creating User InterfacesLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1
    • MotivationsA graphical user interface (GUI) makes a systemuser-friendly and easy to use. Creating a GUIrequires creativity and knowledge of how GUIcomponents work. Since the GUI components inJava are very flexible and versatile, you can createa wide assortment of useful user interfaces.Previous chapters briefly introduced several GUIcomponents. This chapter introduces thefrequently used GUI components in detail. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 2
    • Objectives To create graphical user interfaces with various user- interface components (§§17.2–17.8). To create listeners for JCheckBox, JRadioButton, and JTextField (§17.2). To enter multiple-line texts using JTextArea (§17.3). To select a single item using JComboBox (§17.4). To select a single or multiple items using JList (§17.5). To select a range of values using JScrollBar (§17.6). To select a range of values using JSlider and explore differences between JScrollBar and JSlider (§17.7). To display multiple windows in an application (§17.8). Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3
    • Events for JCheckBox,JRadioButton, and JTextField GUIEventDemo Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 4
    • JTextAreaIf you want to let the user enter multiple lines of text, you cannot usetext fields unless you create several of them. The solution is to useJTextArea, which enables the user to enter multiple lines of text. javax.swing.text.JTextComponent The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity. javax.swing.JTextArea -columns: int The number of columns in this text area. -rows: int The number of rows in this text area. -tabSize: int The number of characters used to expand tabs (default: 8). -lineWrap: boolean Indicates whether the line in the text area is automatically wrapped (default: false). -wrapStyleWord: boolean Indicates whether the line is wrapped on words or characters (default: false). +JTextArea() Creates a default empty text area. +JTextArea(rows: int, columns: int) Creates an empty text area with the specified number of rows and columns. +JTextArea(text: String) Creates a new text area with the specified text displayed. +JTextArea(text: String, rows: int, columns: int) Creates a new text area with the specified text and number of rows and columns. +append(s: String): void Appends the string to text in the text area. +insert(s: String, pos: int): void Inserts string s in the specified position in the text area. +replaceRange(s: String, start: int, end: int): Replaces partial text in the range from position start to end with string s. void +getLineCount(): int Returns the actual number of lines contained in the text area. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 5
    • JTextArea Constructors JTextArea(int rows, int columns) Creates a text area with the specified number of rows and columns. JTextArea(String s, int rows, int columns) Creates a text area with the initial text and the number of rows and columns specified. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 6
    • JTextArea Properties text editable columns lineWrap wrapStyleWord rows lineCount tabSize Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 7
    • Example: Using Text Areas This example gives a program that displays an image in a label, a title in a label, and a text in a text area. javax.swing.JPanel javax.swing.JFrame -char token -char token +getToken 1 1 +getToken DescriptionPanel TextAreaDemo +setToken +setToken +paintComponet +paintComponet -jlblImageTitle: JLabel +mouseClicked +mouseClicked -jtaTextDescription: JTextArea +setImageIcon(icon: ImageIcon): void +setTitle(title: String): void +setTextDescription(text: String): void +getMinimumSize(): Dimension Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 8
    • Example, cont.DiscriptionPanel TextAreaDemo RunLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 9
    • JComboBoxA combo box is a simple list of items from which the user canchoose. It performs basically the same function as a list, butcan get only one value. javax.swing.JComponent javax.swing.JComboBox +JComboBox() Creates a default empty combo box. +JComboBox(items: Object[]) Creates a combo box that contains the elements in the specified array. +addItem(item: Object): void Adds an item to the combo box. +getItemAt(index: int): Object Returns the item at the specified index. +getItemCount(): int Returns the number of items in the combo box. +getSelectedIndex(): int Returns the index of the selected item. +setSelectedIndex(index: int): void Sets the selected index in the combo box. +getSelectedItem(): Object Returns the selected item. +setSelectedItem(item: Object): void Sets the selected item in the combo box. +removeItem(anObject: Object): void Removes an item from the item list. +removeItemAt(anIndex: int): void Removes the item at the specified index in the combo box. +removeAllItems(): void Removes all items in the combo box. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 10
    • JComboBox MethodsTo add an item to a JComboBox jcbo, usejcbo.addItem(Object item)To get an item from JComboBox jcbo, usejcbo.getItem() Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 11
    • Using the itemStateChanged Handler When a choice is checked or unchecked, itemStateChanged() for ItemEvent is invoked as well as the actionPerformed() handler for ActionEvent.public void itemStateChanged(ItemEvent e) { // Make sure the source is a combo box if (e.getSource() instanceof JComboBox) String s = (String)e.getItem();} Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 12
    • Example: Using Combo BoxesThis example letsusers view animage and adescription of acountrys flag byselecting thecountry from acombo box. ComboBoxDemo Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 13
    • JListA list is a component that performs basically the same function as a combobox, but it enables the user to choose a single value or multiple values. javax.swing.JComponent javax.swing.JList +JList() Creates a default empty list. +JList(items: Object[]) Creates a list that contains the elements in the specified array. +getSelectedIndex(): int Returns the index of the first selected item. +setSelectedIndex(index: int): void Selects the cell at the specified index. +getSelectedIndices(): int[] Returns an array of all of the selected indices in increasing order. +setSelectedIndices(indices: int[]): void Selects the cells at the specified indices. +getSelectedValue(): Object Returns the first selected item in the list. +getSelectedValues(): Object[] Returns an array of the values for the selected cells in increasing index order. +getVisibleRowCount(): int Returns the number of visible rows displayed without a scrollbar. (default: 8) +setVisibleRowCount(count: int): void Sets the preferred number of visible rows displayed without a scrollbar. +getSelectionBackground(): Color Returns the background color of the selected cells. +setSelectionBackground(c: Color): void Sets the background color of the selected cells. +getSelectionForeground(): Color Returns the foreground color of the selected cells. +setSelectionForeground(c: Color): void Sets the foreground color of the selected cells. +getSelectionMode(): int Returns the selection mode for the list. +setSelectionMode(selectionMode: int): Sets the selection mode for the list. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 14
    • JList Constructors JList() Creates an empty list. JList(Object[] stringItems) Creates a new list initialized with items. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 15
    • JList Properties selectedIndexd selectedIndices selectedValue selectedValues selectionMode visibleRowCount Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 16
    • Example: Using ListsThis example givesa program that letsusers selectcountries in a listand display the flagsof the selectedcountries in thelabels. ListDemo Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 17
    • JScrollBarA scroll bar is a control that enables the user to select from a range of values. Thescrollbar appears in two styles: horizontal and vertical. javax.swing.JComponent The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity. javax.swing.JScrollBar -orientation: int Specifies horizontal or vertical style, default is horizontal. -maximum: int Specifies the maximum value the scroll bar represents when the bubble reaches the right end of the scroll bar for horizontal style or the bottom of the scroll bar for vertical style. -minimum: int Specifies the minimum value the scroll bar represents when the bubble reaches the left end of the scroll bar for horizontal style or the top of the scroll bar for vertical style. -visibleAmount: int Specifies the relative width of the scroll bars bubble. The actual width appearing on the screen is determined by the maximum value and the value of visibleAmount. -value: int Represents the current value of the scroll bar. -blockIncrement: int Specifies value added (subtracted) when the user activates the block- increment (decrement) area of the scroll bar, as shown in Figure 13.30. -unitIncrement: int Specifies the value added (subtracted) when the user activates the unit- increment (decrement) area of the scroll bar, as shown in Figure 13.30. +JScrollBar() Creates a default vertical scroll bar. +JScrollBar(orientation: int) Creates a scroll bar with the specified orientation. +JScrollBar(orientation: int, value: Creates a scrollbar with the specified orientation, value, extent, int, extent: int, min: int, max: int) minimum, and maximum. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 18
    • Scroll Bar PropertiesMinimal value Maximal value Block decrement Block increment Bubble Unit decrement Unit increment Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 19
    • Example: Using ScrollbarsThis example useshorizontal and verticalscrollbars to control amessage displayed on apanel. The horizontalscrollbar is used to movethe message to the left orthe right, and the verticalscrollbar to move it up anddown. ScrollBarDemo Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 20
    • JSliderJSlider is similar to JScrollBar, but JSlider has moreproperties and can appear in many forms. javax.swing.JComponent The get and set methods for these data fields are provided in the class, but omitted in the UML diagram for brevity. javax.swing.JSlider -maximum: int The maximum value represented by the slider (default: 100). -minimum: int The minimum value represented by the slider (default: 0). -value: int The current value represented by the slider. -orientation: int The orientation of the slider (default: JSlider.HORIZONTAL). -paintLabels: boolean True if the labels are painted at tick marks (default: false). -paintTicks: boolean True if the ticks are painted on the slider (default: false). -paintTrack: boolean True if the track is painted on the slider (default: true). -majorTickSpacing: int The number of units between major ticks (default: 0). -minorTickSpacing: int The number of units between minor ticks (default: 0). -inverted: boolean True to reverse the value-range, and false to put the value range in the normal order (default: false). +JSlider() Creates a default horizontal slider. +JSlider(min: int, max: int) Creates a horizontal slider using the specified min and max. +JSlider(min: int, max: int, value: int) Creates a horizontal slider using the specified min, max, and value. +JSlider(orientation: int) Creates a slider with the specified orientation. +JSlider(orientation: int, min: int, max: Creates a slider with the specified orientation, min, max, and value. int, value: int) Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 21
    • Example: Using SlidersRewrite the precedingprogram using the slidersto control a messagedisplayed on a panelinstead of using scrollbars. SliderDemo Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 22
    • Creating Multiple WindowsThe following slides show step-by-step how tocreate an additional window from an applicationor applet. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 23
    • Creating Additional Windows, Step 1Step 1: Create a subclass of JFrame (called aSubFrame) that tells the new window whatto do. For example, all the GUI applicationprograms extend JFrame and are subclassesof JFrame. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 24
    • Creating Additional Windows, Step 2Step 2: Create an instance of SubFrame in theapplication or applet.Example:SubFrame subFrame = new SubFrame("SubFrame Title"); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 25
    • Creating Additional Windows, Step 3Step 3: Create a JButton for activating thesubFrame.add(new JButton("Activate SubFrame")); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 26
    • Creating Additional Windows, Step 4Step 4: Override the actionPerformed()method as follows:public actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if (e.target instanceof Button) { if ("Activate SubFrame".equals(actionCommand)) { subFrame.setVisible(true); } }} Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 27
    • Example: Creating Multiple Windows This example creates a main window with a text area in the scroll pane, and a button named "Show Histogram." When the user clicks the button, a new window appears that displays a histogram to show the occurrence of the letters in the text area. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 28
    • Example, cont.MultipleWindowsDemo Run Histogram Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 29