Chapter 13 Creating User Interfaces Prerequisi七es王 or part工工工 Chapter 8 Inheritance and Polymorphism Chapter 9 Abstract Classes and Interfaces Starry starry night Chapter 11 Getting Started with GUI Programming flaming flowers that brightly blaze swirling clouds in violet haze reflect in Vincent's eyes of China blue Colors changing hue Chapter 12 Event-Driven Programmin morning fields of amber grain weathered faces lined in pain are smoothed beneath the artists loving hand. hapter 13 Creating User Interfaces And now I understand what you tried to say to me and how you suffered for your sanity Chapter 14 Applets, Images, Audio and how you tried to set them free. Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 13 Creating User Interfaces Prerequisites for Part III Chapter 11 Getting Started with GUI Programming Chapter 8 Inheritance and Polymorphism Chapter 13 Creating User Interfaces Chapter 14 Applets, Images, Audio Chapter 9 Abstract Classes and Interfaces Chapter 12 Event-Driven Programming …Starry starry night flaming flowers that brightly blaze swirling clouds in violet haze reflect in Vincent's eyes of China blue. Colors changing hue morning fields of amber grain weathered faces lined in pain are smoothed beneath the artist's loving hand. And now I understand what you tried to say to me and how you suffered for your sanity and how you tried to set them free…
Objectives o To create graphical user interfaces with various user-interface components: JButton, JCheck Box, RAdiobUtton, JLabel JTextField JTextArea, JComboBox JList, JScrollBar, and jSlider (§132-13.12) o To create listeners for various types of events(8.2-13. 12) To use borders to visually group user-interface components (§132) To create image icons using the ImageIcon class(s 13. 3) To display multiple windows in an application(8 13. 13) To know how to create menu Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives ⚫ To create graphical user interfaces with various user-interface components: JButton, JCheckBox, JRadioButton, JLabel, JTextField, JTextArea, JComboBox, JList, JScrollBar, and JSlider (§13.2 – 13.12). ⚫ To create listeners for various types of events (§13.2 – 13.12). ⚫ To use borders to visually group user-interface components (§13.2). ⚫ To create image icons using the ImageIcon class (§13.3). ⚫ To display multiple windows in an application (§13.13). ⚫ To know how to create menu
Components Covered in the Chapter o Introduces the frequently used GUI components oUses borders and icons Butto Component Container 匹 Component bstract Button I Checkbox JLabel RAdio Button Text Field HETex Componen TExtaRea J Combo JList SCrollbAr SLider Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 3 Components Covered in the Chapter ⚫Introduces the frequently used GUI components ⚫Uses borders and icons AbstractButton JToggleButton JCheckBox JRadioButton JComboBox JList JSlider JTextComponent JLabel JButton Component Container JComponent JTextField JTextArea JScrollBar
iava.awt. Component get Fonto: java. awt. Font Returns the font of this component Sets the font of this compor get Background: java. awt. Color Returns the background color of this component set Background(c: Color): void Sets the background color of this component get Foreground: java. awt. Color Returns the foreground color of this component. foReground(c: Color ): void Sets the foreground color of this component Returns the width of this component getHeightO: int Returns the height of this component tPreferredSize( Dimension Returns the preferred size of this compor setPreferredSize(d Dimension): void Sets the preferred size of this component is Visible: boolean Indicates whether this component is visible set Visible(b: boolean); void Shows or hides this component java. awt Container add(comp: Component): Component ppends a component to the container add(comp: Component, index: int): Component Adds a component to the container with the specified index remove(comp: Component ): void Removes the component from the container getLayout(: LayoutManager Returns the layout manager for this container set Layout(l: Layout Manager): void Sets the layout manager for this container paintComponents(g: Graphics ): void Paints each of the components in this container javax. swing. JComponent get TextO: String Returns the tool tip text for this component. Tool tip text is displayed when the mouse points on the component without clicking set ToolTipText(test: String):ve Sets a new tool tip text for this component getBorder(: javax. swing. border Border Returns the border for this component setBorder(border: Border ) void Sets a new border for this component
Liang,Introduction to Java Programming,revised by Dai-kaiyu 4 java.awt.Container +add(comp: Component): Component +add(comp: Component, index: int): Component +remove(comp: Component): void +getLayout(): LayoutManager +setLayout(l: LayoutManager): void +paintComponents(g: Graphics): void Appends a component to the container. Adds a component to the container with the specified index. Removes the component from the container. Returns the layout manager for this container. Sets the layout manager for this container. Paints each of the components in this container. java.awt.Component +getFont(): java.awt.Font +setFont(f: java.awt.Font): void +getBackground(): java.awt.Color +setBackground(c: Color): void +getForeground(): java.awt.Color +setForeground(c: Color): void +getWidth(): int +getHeight(): int +getPreferredSize(): Dimension +setPreferredSize(d: Dimension) : void +isVisible(): boolean +setVisible(b: boolean): void Returns the font of this component. Sets the font of this component. Returns the background color of this component. Sets the background color of this component. Returns the foreground color of this component. Sets the foreground color of this component. Returns the width of this component. Returns the height of this component. Returns the preferred size of this component. Sets the preferred size of this component. Indicates whether this component is visible. Shows or hides this component. javax.swing.JComponent +getToolTipText(): String +setToolTipText(test: String): void +getBorder(): javax.swing.border.Border +setBorder(border: Border): void Returns the tool tip text for this component. Tool tip text is displayed when the mouse points on the component without clicking. Sets a new tool tip text for this component. Returns the border for this component. Sets a new border for this component
Borders o You can set a border on any object of the JComponent class. Swing has several types of borders. To create a titled border use new Titled Border string title) To create a line border. use new Line border color color, int width), where width specifies the thickness of the line For example, the following code displays a titled border on a pane JPanel panel =new JPanelO panel set Border(new Title border( My Panel) Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 5 Borders ⚫You can set a border on any object of the JComponent class. Swing has several types of borders. To create a titled border, use new TitledBorder(String title). To create a line border, use new LineBorder(Color color, int width), where width specifies the thickness of the line. For example, the following code displays a titled border on a panel: JPanel panel = new JPanel(); panel.setBorder(new TitleBorder(“My Panel”));
Test Swing Common Features Component Properties J Component Properties ●font t○O1T1 pText background border ● foreground preferredSize o minimum Size o maximum Size TestSwing Common Features R un Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 Test Swing Common Features Component Properties ⚫ font ⚫ background ⚫ foreground ⚫ preferredSize ⚫ minimumSize ⚫ maximumSize JComponent Properties toolTipText border TestSwingCommonFeatures Run
Buttons a button is a component that triggers an action event when clicked o Swing provides regular buttons, toggle buttons check box buttons. and radio buttons o The common features of these buttons are represented in javax. swing. AbstractButton Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 Buttons ⚫A button is a component that triggers an action event when clicked. ⚫Swing provides regular buttons, toggle buttons, check box buttons, and radio buttons. ⚫The common features of these buttons are represented in javax.swing.AbstractButton
I javax. swing JComponent javax.swing. AbstractButton etAction Commando: String Retums the action command of this button etActionCommand(s: String): void Sets a new action command for this button tTextO: String Retums the buttons text (i.e the text label on the button) Text( text: String): void Sets the button's text getlcon(: javax. swing. Icon Retums the button's default icon Iconicon: Icon): void Sets the buttons default icon. This icon is also used as the"pressed"and sabled"icon if there is no explicitly set pressed icon. t Pressedlcon(: javax. swing. Icon eturms the pressed icon( displayed when the button is pressed) et Pressedlcon( pressedlcon: Icon): void Sets a pressed icon for the button. onO): javax. swing. Icon Returns the rollover icon(displayed when the mouse is over the button) etRolloverlcon(pressedlcon: Icon): Sets a rollover icon for the button cO: Int Retums the mnemonic key value of this button. You can select the button by pressing the alt key and the mnemonic key at the same time etMnemonic(mnemonic: int ) void Sets a mnemonic key value of this button. t horizontalAlignmento: int Retums the horizontal alignment of the icon and text on the button. et HorizontalAlignment(alignment: in ): Sets the horizontal alignment of the icon and text.(default: CENTER) ethorizontal Text Position(: int Returns the horizontal text position relative to the icon on the button etHorizontalText Position(position: int) Sets the horizontal text position of the text relative to the icon(default RIGHT get VerticalAlignment(: int Retums the vertical alignment of the icon and text on the button tVerticalAlignment(vAlignment: int): Sets the vertical alignment of the icon and text. (default: CENTER) Retums the vertical text position relative to the icon on the button et VerticalTextPosition(position: int): Sets the vertical text position of the text relative to the icon(default: isBorderPaintedo: Boolean Indicates whether the border of the button is painted et Border Painted(b: boolean ) void Draws or hides the border of the button. By default, a regular buttons border is painted, but the border for a check box and a radio button is etlconText Gapo: int eturms the gap between the text and the icon on the button. (DK 1.4) etlconText Gap(icon TextGap: int): void Sets a gap between the text and the icon on the button. (JDK 1.4) is Selected: boolean Retums the state of the button True if the check box or radio button is Selected (b: boolean ) void Sets the state for the check box or radio button
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 javax.swing.AbstractButton +getActionCommand(): String +setActionCommand(s: String): void +getText(): String +setText(text: String): void +getIcon(): javax.swing.Icon +setIcon(icon: Icon): void +getPressedIcon(): javax.swing.Icon +setPressedIcon(pressedIcon: Icon): void +getRolloverIcon(): javax.swing.Icon +setRolloverIcon(pressedIcon: Icon): void +getMnemonic(): int +setMnemonic(mnemonic: int): void +getHorizontalAlignment(): int +setHorizontalAlignment(alignment: int): void +getHorizontalTextPosition(): int +setHorizontalTextPosition(position: int): void +getVerticalAlignment(): int +setVerticalAlignment(vAlignment: int): void +getVerticalTextPosition(): int +setVerticalTextPosition(position: int) : void +isBorderPainted(): Boolean +setBorderPainted(b: boolean): void +getIconTextGap(): int +setIconTextGap(iconTextGap: int): void +isSelected(): boolean +setSelected(b: boolean): void Returns the action command of this button. Sets a new action command for this button. Returns the button’s text (i.e., the text label on the button). Sets the button’s text. Returns the button’s default icon. Sets the button's default icon. This icon is also used as the "pressed" and "disabled" icon if there is no explicitly set pressed icon. Returns the pressed icon (displayed when the button is pressed). Sets a pressed icon for the button. Returns the rollover icon (displayed when the mouse is over the button). Sets a rollover icon for the button. Returns the mnemonic key value of this button. You can select the button by pressing the ALT key and the mnemonic key at the same time. Sets a mnemonic key value of this button. Returns the horizontal alignment of the icon and text on the button. Sets the horizontal alignment of the icon and text. (default: CENTER) Returns the horizontal text position relative to the icon on the button. Sets the horizontal text position of the text relative to the icon. (default: RIGHT) Returns the vertical alignment of the icon and text on the button. Sets the vertical alignment of the icon and text. (default: CENTER). Returns the vertical text position relative to the icon on the button. Sets the vertical text position of the text relative to the icon. (default: CENTER) Indicates whether the border of the button is painted. Draws or hides the border of the button. By default, a regular button’s border is painted, but the border for a check box and a radio button is not painted. Returns the gap between the text and the icon on the button. (JDK 1.4) Sets a gap between the text and the icon on the button. (JDK 1.4) Returns the state of the button. True if the check box or radio button is selected, false if it's not. Sets the state for the check box or radio button. javax.swing.JComponent
JButton JButton inherits Abstract Button and provides several constructors to create buttons javax.swing. AbstractButton javax.swing.JButton BUttony Creates a default button with no text and icon JButton(icon: javax. swing. Icon) Creates a button with an icon JButton(text: String) Creates a button with text JButton(text: String, icon: Icon) Creates a button with text and an icon Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 JButton JButton inherits AbstractButton and provides several constructors to create buttons. 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. javax.swing.AbstractButton
JButton constructors The following are JButton constructors BUttony JButton String text JButton(String text, Icon icon) JButton(Iconicon) Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 JButton Constructors The following are JButton constructors: JButton() JButton(String text) JButton(String text, Icon icon) JButton(Icon icon)