Chapter12 Creates a Graphic User Interface Common features of Swing GUI components Button check box radio button Label Text Domain/Text Area combo box list box Scrollbar/slider Create multiple windows java高级程序设计(下) 1 GUI component Common GUI components (yellow display): JButton Component Container JComponent AbstractButton JCheckBox JToggleButton JLabel JRadioButton JTextField JTextComponent JTextArea JComboBox JList JScrollBar JSlider java高级程序设计(下) 2 Common features of Swing GUI component s java.awt.Component +setFont(f: java.awt.Font): void 返回该组件的字体 +getBackground(): java.awt.Color +setBackground(c: Color): void 返回该组件的背景色 +getFont(): java.awt.Font +getForeground(): java.awt.Color +setForeground(c: Color): void +getWidth(): int 设置该组件的字体 设置该组件的背景色 返回该组件的前景色 返回该组件的宽度 设置该组件的宽度 +getHeight(): int +getPreferredSize(): Dimension +setPreferredSize(d: Dimension) : void +isVisible(): boolean +setVisible(b: boolean): 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 javax.swing.JComponent 向该容器中添加一个组件 用指定序号向该容器中添加一个组件 从该容器中删除一个组件 返回容器的布局管理器 设置该容器的布局管理器 在该容器上绘制每个组件 +setToolTipText(test: String): void 返回该组件的工具提示文本。所谓工具提示文本是指当鼠标指向 组件而不点击时显示的提示信息 +getBorder(): javax.swing.border.Border +setBorder(border: Border): void 返回该组件的边框 +getToolTipText(): String 设置该组件新的工具提示文本 设置该组件的新边框 java高级程序设计(下) 3 Common features of Swing GUI component s The types of borders in Swing components are as follows: 1. New TitleBorder (Swing title) // Setting Title borders 2. New LineBorder (Color color, int width) // Setting Linear Borders Example: TestSwingCommonFeatures. J ava Button: A component that triggers an act ion event when clicked. Swing provides: Conventional buttons, switch buttons, c heck boxes and radio buttons Common features: java. swing. Abstracti onButton java高级程序设计(下) javax.swing.JComponent javax.swing.AbstractionButton javax.swing.JButton 4 Icon - Image Icon It can be used to decorate components. Javax. swing. Icon Interface - Javax. swing. ImageIcon Java currently supports three image formats: GIF, JPEG, PNG Example: Display three icons on a button (default icon, push icon, and top icon) Common attributes of JButton:  Text        Icon Mnemonic Horizontal Alignment Vertical Alignment HorizontalTextPosition Vertical TextPosition IconTextGap java高级程序设计(下) 5 Alignment, text location Alignment mode Horizontal alignment: set Horizontal Alignment (int) Vertical alignment: set Vertical Alignment (int) The int value is selected as a constant under the SwingConstants inte rface. Five values can be selected: LEADING, LEFT, CENTER, RIGHT, TRAILING. Text location Refers to the horizontal and vertical position of text relative to icons. Horizontal Text Position (int) Vertical Text Position (int) The int value is selected as a constant under the SwingConstants inte rface. Three values can be selected: TOP, CENTER and BOTTOM. java高级程序设计(下) 6 Use the button (JButton) Example: Write a program to display a message on the panel and use two icons <= and=> to move the information left or right on the panel. Code: mypanel. Java Public void action Performed (Action Event) { If (e. getActionCommand (). equals ("<=") X-=10; If (e. getActionCommand (). equals ("=>") X+=10; Repaint (); } java高级程序设计(下) 7 Check box (JCheckBox) javax.swing.AbstractButton javax.swing.JToggleButton javax.swing.JCheckBox 创建一个没有文本和图标的默认复选框 +JCheckBox() +JCheckBox(text: String) +JCheckBox(text: String, selected: boolean) +JCheckBox(icon: Icon) +JCheckBox(text: String, icon: Icon) +JCheckBox(text: String, icon: Icon, selected: boolean) 创建一个带文本的复选框 创建一个带文本的复选框,并指定初始状态是否选中 创建一个带图标的复选框 创建一个带文本和图标的复选框 创建一个带文本和图标的复选框,并指定初始状态是否选中 Use the check box: Example: Checkmypanel. Java java高级程序设计(下) 8 Radio Button That is to say, select the button and choose the only option from multiple option s. Organize multiple radio buttons: java. swing. ButtonGroup ButtonGroup BTG = new ButtonGroup (); Btg. add (jrb1); Btg. add (jrb2); Note: 1. Only one radio button can be selected from the radio button group at a time. 2. ButtonGroup is not a component, so it cannot be added to the container. Click a radio button to trigger the ItemEvent event, and then trigger the Act ionEvent event. Example: Add three radio butt

pptDoc Creating graphical user interface in Java

Professional Docs > Common > Other > Preview
28 Pages 0 Downloads 208 Views 3.0 Score
Tips: Current document can only be previewed at most page8,If the total number of pages in the document exceeds page 8,please download the document。
Uploaded by admin on 2019-10-15 15:10:03
You can enter 255 characters
What is my domain?( )
  • No comments yet