JButton Properties o text ●icon o mnemonic o horizontalAlignment ● verticalAlignment o horizontalTextposition ● erticalTextPosition O icon TextGap Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 11 JButton Properties ⚫ text ⚫ icon ⚫ mnemonic ⚫ horizontalAlignment ⚫ verticalAlignment ⚫ horizontalTextPosition ⚫ verticalTextPosition ⚫ iconTextGap
Icons o An icon is a fixed-size picture, typically is small and used to decorate components o javax. swing. Icon is an interface. To create an image, use its concrete class javax. swing. Imagelcon For example, the following statement creates an icon from an image file Icon icon=new Imagelcon(photo. gif TestButtonIcons Run Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 12 Icons ⚫An icon is a fixed-size picture; typically it is small and used to decorate components. ⚫ javax.swing.Icon is an interface. To create an image, use its concrete class javax.swing.ImageIcon. For example, the following statement creates an icon from an image file: Icon icon = new ImageIcon("photo.gif"); TestButtonIcons Run
Default icons. Pressed Icon, and Rollover icon o A regular button has a default icon, pressed icon, and rollover icon. Normally, you use the default icon o All other icons are for special effects. a pressed icon is displayed when a button is pressed and a rollover icon is displayed when the mouse is over the button but pressed Buttonicons 色助mnn-x助 ttonlcons=x Click it *Click it dick it (A)Default icon (B)Pressedicon (C)Rollovericon Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 13 Default Icons, Pressed Icon, and Rollover Icon ⚫A regular button has a default icon, pressed icon, and rollover icon. Normally, you use the default icon. ⚫All other icons are for special effects. A pressed icon is displayed when a button is pressed and a rollover icon is displayed when the mouse is over the button but pressed. (A) Default icon (B) Pressed icon (C) Rollover icon
Horizontal Alignments See figure 13.7 Horizontal alignment specifies how the icon and text are placed horizontally on a button. You can set the horizontal alignment using one of the five constants: LEADING, LEFT, CENTER, RIGHT, TRAILING. At present, LEADING and Eft are the same and trailing and right are the same. Future implementation may distinguish them. The default horizontal alignment is Swing Constants. TRAILING Vertical Alignments Vertical alignment specifies how the icon and text are placed vertically on a button. You can set the vertical alignment using one of the three constants: TOP, CENTER, BOTtOM The default vertical alignment is swing Constants. centEr. Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 14 Horizontal Alignments Horizontal alignment specifies how the icon and text are placed horizontally on a button. You can set the horizontal alignment using one of the five constants: LEADING, LEFT, CENTER, RIGHT, TRAILING. At present, LEADING and LEFT are the same and TRAILING and RIGHT are the same. Future implementation may distinguish them. The default horizontal alignment is SwingConstants.TRAILING. See figure 13.7 Vertical Alignments Vertical alignment specifies how the icon and text are placed vertically on a button. You can set the vertical alignment using one of the three constants: TOP, CENTER, BOTTOM. The default vertical alignment is SwingConstants.CENTER
Horizontal text positions Horizontal text position specifies the horizontal position of the text relative to the icon o You can set the horizontal text position using one of the five constants LEadING. LEFT center RIGHT. TRAILING The default horizontal text position is Swing Constants. RIGHT LEFT 刈 CENTER RIGHT Grapes Grab 多 Grapes Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 15 Horizontal Text Positions ⚫Horizontal text position specifies the horizontal position of the text relative to the icon. ⚫You can set the horizontal text position using one of the five constants: LEADING, LEFT, CENTER, RIGHT, TRAILING. The default horizontal text position is SwingConstants.RIGHT