第13章 创建用户图形界面
JCheckBox JRadioButton
2
ponent
+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.
JComponent Properties • toolTipText • border
TestSwingCommonFeatures
Run
5
Button
按钮(button)是一种点击时触发行为事件的 组件。Swing 提供常规按钮、开关按钮、复选 框和单选按钮。这些按钮的公共特性在 javax.swing.AbstractButton中进行描述。
3
Borders
可以在 JComponent 类的任何对象上设置边框。 Swing 类有几种边框类型. 创建一个标题边框, 使用
new TitledBorder(String title).
创建一个线型边框,使用
new LineBorder(Color color, int width),
其中width 是指线的宽度. 例如, 如下程序清单 在信息面板上显示一个标题边框。
+setPressedIcon(pressedIcon: Icon): void Sets a pressed icon for the button. +getRolloverIcon(): javax.swing.Icon Returns the rollover icon (displayed when the mouse is over the button). +setRolloverIcon(pressedIcon: Icon): void +getMnemonic(): int +setMnemonic(mnemonic: int): void +getHorizontalAlignment(): int 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.
+getIconTextGap(): int
Returns the gap between the text and the icon on the button. (JDK 1.4) +setIconTextGap(iconTextGap: int): void Sets a gap between the text and the icon on the button. (JDK 1.4) +isSelected(): boolean Returns the state of the button. True if the check box or radio button is selected, false if it's not. +setSelected(b: boolean): void Sets the state for the check box or radio button.
Returns the horizontal alignment of the icon and text on the button. +setHorizontalAlignment(alignment: int): Sets the horizontal alignment of the icon and text. (default: CENTER) void +getHorizontalTextPosition(): int Returns the horizontal text position relative to the icon on the button. +setHorizontalTextPosition(position: int): Sets the horizontal text position of the text relative to the icon. (default: void RIGHT) +getVerticalAlignment(): int Returns the vertical alignment of the icon and text on the button. +setVerticalAlignment(vAlignment: int): void +getVerticalTextPosition(): int +setVerticalTextPosition(position: int) : void +isBorderPainted(): Boolean +setBorderPainted(b: boolean): void 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.
java.awt.Container
+add(comp: Component): Component Appends a component to the container. +add(comp: Component, index: int): Component Adds a component to the container with the specified index. Removes the component from the container. +remove(comp: Component): void +getLayout(): LayoutManager +setLayout(l: LayoutManager): void +paintComponents(g: Graphics): void Returns the layout manager for this container. Sets the layout manager for this container. Paints each of the components in this container.