第七章(3) 创建图形用户界面
第七章(3) 创建图形用户界面
学习目标 使用不同的用户界面组件: JButton, J CheckBox JRadioButton jlabel jTextfield JTextArea JComboBox jlist jscrollbar and slider 为不同的事件类型创建监听器 使用边框在外观上分类组织用户界面组件 使用 Imagelcon类创建图像图标 在一个应用程序中显示多个窗口
学习目标 使用不同的用户界面组件: JButton, JCheckBox, JRadioButton, JLabel, JTextField, JTextArea, JComboBox, JList, JScrollBar, and JSlider. 为不同的事件类型创建监听器 使用边框在外观上分类组织用户界面组件 使用ImageIcon类创建图像图标 在一个应用程序中显示多个窗口
本章介绍的组件 Butto ComponentI Container nonel Abstract Button J Checkbox JLabel Radiobutton JTextField 丿 Texto ITextAre J ComboBox JList SCrollbAr
本章介绍的组件 AbstractButton JToggleButton JCheckBox JRadioButton JComboBox JList JSlider JTextComponent JLabel JButton Component Container JComponent JTextField JTextArea JScrollBar
java.awt.component 返回该组件的字体 getFontO: java. awt. Fc setFont(f: java. awt. Font): void 设置该组件的字体 getBackgroundo: java. awt. Color 返回该组件的背景色 setBackground(c: Color ) void 设置该组件的背景色 getForegroundO: java. awt. Color 返回该组件的前景色 setForeground(c: Color ) void 设置该组件的前景色 ge 返回该组件的宽度 get Height(: int 返回该组件的高度 getPre(: Dimension 返回该组件的恰当尺寸 setPreferredSize(d: Dimension): void 设置该组件的恰当尺寸 is Visible(: boolean 指明该组件是否是可见的 et Visible(b: boolean): void 显示或隐藏该组件 java.awtContainer dd(comp: Component ) Component 向该容器中添加一个组件 ldd(comp: Component, index: int): Component用指定序号向该容器添加一个组件 remove(comp: Component ) void 从该容器中删除一个组件 返回该容器的布局管理器 setLayout(l: Layout Manager): void 设置该容器的布局管理器 paint Components(g: Graphics): void 在该容器上绘制每个组件 Javax.swing Component FgetTool'TipTextO: String 返回该组件的工具提示文本 设置该组件新的工具提示文本 HsetToolTip Text(test: String):void 返回该组件的边框 getBorderO: javax. swing. border Border HsetBorder(border: Border): void 设置该组件的新边框
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 向该容器中添加一个组件 用指定序号向该容器添加一个组件 从该容器中删除一个组件 返回该容器的布局管理器 设置该容器的布局管理器 在该容器上绘制每个组件 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 返回该组件的字体 设置该组件的字体 返回该组件的背景色 设置该组件的背景色 返回该组件的前景色 设置该组件的前景色 返回该组件的宽度 返回该组件的高度 返回该组件的恰当尺寸 设置该组件的恰当尺寸 指明该组件是否是可见的 显示或隐藏该组件 javax.swing.JComponent +getToolTipText(): String +setToolTipText(test: String): void +getBorder(): javax.swing.border.Border +setBorder(border: Border): void 返回该组件的工具提示文本 设置该组件新的工具提示文本 返回该组件的边框 设置该组件的新边框
边框 Borders 可以在 JComponent类的任何对象上设置边框 Swing有几种边框类型,如: new Titledborder( String title.创建一个标题边框 new Line border( Color color. int width,创建一个线 型边框,其中wdth是指线的宽度。 以下代码设置面板边框为标题边框 JPanel panel new JPanelo panel set Border(new Title border(" My Panel)
边框Borders 可以在JComponent类的任何对象上设置边框。 swing有几种边框类型,如: new TitledBorder(String title). 创建一个标题边框 new LineBorder(Color color, int width), 创建一个线 型边框,其中width是指线的宽度。 以下代码设置面板边框为标题边框 JPanel panel = new JPanel(); panel.setBorder(new TitleBorder(“My Panel”));
按钮 Buttons 按钮是一种点击时触发 Action event的组件。 Swing提供常规 buttons, toggle(开关) buttons, check box buttons和 radio buttons.这些按钮的公共 特性在 javax. swing. AbstractButton中描述。 鸟 Buttons 巴区 Regular Button CheckBox O RadioButton
按钮Buttons 按钮是一种点击时触发ActionEvent的组件。 Swing提供常规 buttons, toggle(开关) buttons, check box buttons和 radio buttons. 这些按钮的公共 特性在 javax.swing.AbstractButton中描述
javax. swingJComponent iava.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 button's default icon. This icon is also used as the"pressed"and disabled"icon if there is no explicitly set pressed icon t Pressedlcon(: javax. swing. Icon Returns 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) uonnq un Jo] uoo! 1AOIOIB sIs: (uooI uooIpassad Juu/.pIoA stroll 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 Returns 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 Retuns 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 order is painted, but the border for a check box and a radio button is etlconText Gapo: int Retuns the gap between the text and the icon on the button. DK 1. 4) nTextGap(icon Text Gap: 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
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继承 AbstractButton并提供了几个创建按 钮的构造方法 I javax. swing. AbstractButton avax SWing.JButton JButtono 创建默认按钮 JButton(icon: javax. swing. Icon 用图标创建一个按钮 JButton(text: String) 用文本创建一个按钮 JButton(text: String, icon: Icon) 用文本和图标创建一个按钮
JButton JButton 继承 AbstractButton 并提供了几个创建按 钮的构造方法. javax.swing.JButton +JButton() +JButton(icon: javax.swing.Icon) +JButton(text: String) +JButton(text: String, icon: Icon) 创建默认按钮 用图标创建一个按钮 用文本创建一个按钮 用文本和图标创建一个按钮 javax.swing.AbstractButton
JButton属性 七ext mnemonIc horizontalAlignment verticalalignment horizontaltextposition verticaltextposition iC○ nTextGap
JButton 属性 text icon mnemonic horizontalAlignment verticalAlignment horizontalTextPosition verticalTextPosition iconTextGap
Icons 个icon是固定尺寸的图片;它一般比较 用于装饰组件. lavan. .swing. Icon是 接口.为了创建一个icon,使用它的实现 类 avax swing. Imagelcon.比如,下面代 码从一个 image创建一个icon Icon icon=new Imagelcon(photo. gif: TestButtonicons
Icons 一个icon是固定尺寸的图片; 它一般比较 小,用于装饰组件. javax.swing.Icon 是一 个接口. 为了创建一个icon, 使用它的实现 类 javax.swing.ImageIcon. 比如,下面代 码从一个image创建一个icon: Icon icon = new ImageIcon("photo.gif"); TestButtonIcons