16.2. The Applet Class

 
[Page 492 ( continued )]

15.2. 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 , as shown in Figure 15.2.

Figure 15.2. AbstractButton defines common features of different types of buttons.
(This item is displayed on page 493 in the print version)

This section introduces the regular buttons defined in the JButton class. JButton inherits AbstractButton and provides several constructors to create buttons, as shown in Figure 15.3.

Figure 15.3. JButton defines a regular push button.
(This item is displayed on page 493 in the print version)

15.2.1. Icons, Pressed Icons, and Rollover Icons

A regular button has a default icon, pressed icon, and rollover icon. Normally, you use the default icon. The 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 not pressed. For example, Listing 15.1 displays the American flag as a regular icon, the Canadian flag as a pressed icon, and the British flag as a rollover icon, as shown in Figure 15.4.


[Page 493]
Figure 15.4. A button can have several types of icons.
(This item is displayed on page 494 in the print version)

Listing 15.1. TestButtonIcons.java
(This item is displayed on pages 493 - 494 in the print version)


[Page 494]

15.2.2. Alignments

Horizontal alignment specifies how the icon and text are placed horizontally on a button. You can set the horizontal alignment using setHorizontalAlignment(int) with one of the five constants LEADING , LEFT , CENTER , RIGHT , TRAILING , as shown in Figure 15.5. At present, LEADING and LEFT are the same, and TRAILING and RIGHT are the same. Future implementation may distinquish them. The default horizontal alignment is SwingConstants.TRAILING .

Figure 15.5. You can specify how the icon and text are placed on a button horizontally.

Vertical alignment specifies how the icon and text are placed vertically on a button. You can set the vertical alignment using setVerticalAlignment(int) with one of the three constants TOP , CENTER , BOTTOM , as shown in Figure 15.6. The default vertical alignment is SwingConstants.CENTER .

Figure 15.6. You can specify how the icon and text are placed on a button vertically.


[Page 495]

15.2.3. Text Positions

Horizontal text position specifies the horizontal position of the text relative to the icon. You can set the horizontal text position using setHorizontalTextPosition(int) with one of the five constants LEADING , LEFT , CENTER , RIGHT , TRAILING , as shown in Figure 15.7. At present, LEADING and LEFT are the same, and TRAILING and RIGHT are the same. Future implementation may distinquish them. The default horizontal text position is SwingConstants.RIGHT .

Figure 15.7. You can specify the horizontal position of the text relative to the icon.

Vertical text position specifies the vertical position of the text relative to the icon. You can set the vertical text position using setVerticalTextPosition(int) with one of the three constants TOP , CENTER , BOTTOM , as shown in Figure 15.8. The default vertical text position is SwingConstants.CENTER .

Figure 15.8. You can specify the vertical position of the text relative to the icon.

Note

The constants LEFT , CENTER , RIGHT , LEADING , TRAILING , TOP , and BOTTOM used in AbstractButton are also used in many other Swing components . These constants are centrally defined in the javax.swing.SwingConstants interface. Since all Swing GUI components implement SwingConstants , you can reference the constants through SwingConstants or a GUI component. For example, SwingConstants.CENTER is the same as JButton.CENTER .


JButton can generate many types of events, but often you need to respond to an ActionEvent . When a button is pressed, it generates an ActionEvent .

15.2.4. Example: Using Buttons

This example presents a program, shown in Listing 15.2, that displays a message on a panel and uses two buttons, <= and => , to move the message on the panel to the left or right. The layout of the UI and the output of the program are shown in Figure 15.9.

Figure 15.9. Clicking the <= and => buttons causes the message on the panel to move to the left and right, respectively.
(This item is displayed on page 496 in the print version)


Here are the major steps in the program:

1.
Create the user interface.

Create a MessagePanel object to display the message. The MessagePanel class was created in §13.11, "Case Study: The MessagePanel Class." Place it in the center of the frame. Create two buttons, <= and => , on a panel. Place the panel in the south of the frame.


[Page 496]
2.
Process the event.

Create and register listeners for processing the action event to move the message left or right according to whether the left or right button was clicked.

Listing 15.2. ButtonDemo.java
(This item is displayed on pages 496 - 497 in the print version)
 1   import   java.awt.*; 2   import   java.awt.event.ActionListener; 3   import   java.awt.event.ActionEvent; 4   import   javax.swing.*; 5 6   public class   ButtonDemo   extends   JFrame { 7  // Create a panel for displaying message  8    protected   MessagePanel messagePanel  9  =   new   MessagePanel(   "Welcome to Java"   );  10 11  // Declare two buttons to move the message left and right  12    private   JButton jbtLeft =   new   JButton(   "<="   );  13    private   JButton jbtRight =   new   JButton(   "=>"   );  14 15   public static void   main(String[] args) { 16 ButtonDemo frame =   new   ButtonDemo(); 17 frame.setTitle(   "ButtonDemo"   ); 18 frame.setLocationRelativeTo(   null   );  // Center the frame  19 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 20 frame.setSize(   250   ,   100   ); 21 frame.setVisible(   true   ); 22 } 23 24   public   ButtonDemo() { 25  // Set the background color of messagePanel  26 messagePanel.setBackground(Color.white); 27 28  // Create Panel jpButtons to hold two Buttons "<=" and "right =>"  29 JPanel jpButtons =   new   JPanel(); 30 jpButtons.setLayout(   new   FlowLayout()); 31 jpButtons.add(jbtLeft); 32 jpButtons.add(jbtRight); 33 34  // Set keyboard mnemonics  35  jbtLeft.setMnemonic(   'L'   );  36 jbtRight.setMnemonic(   'R'   ); 37 38  // Set icons and remove text  39  // jbtLeft.setIcon(new ImageIcon("image/left.gif"));  40  // jbtRight.setIcon(new ImageIcon("image/right.gif"));  

[Page 497]
 41  // jbtLeft.setText(null);  42  // jbtRight.setText(null);  43 44  // Set tool tip text on the buttons  45  jbtLeft.setToolTipText(   "Move message to left"   );  46 jbtRight.setToolTipText(   "Move message to right"   ); 47 48  // Place panels in the frame  49 setLayout(   new   BorderLayout()); 50 add(messagePanel, BorderLayout.CENTER); 51 add(jpButtons, BorderLayout.SOUTH); 52 53  // Register listeners with the buttons  54  jbtLeft.addActionListener(   new   ActionListener() {  55   public void   actionPerformed(ActionEvent e) { 56 messagePanel.moveLeft(); 57 } 58 }); 59  jbtRight.addActionListener(   new   ActionListener() {  60   public void   actionPerformed(ActionEvent e) { 61 messagePanel.moveRight(); 62 } 63 }); 64 } 65 } 

messagePanel (line 8) is deliberately declared protected so that it can be referenced by a subclass in future examples.

You can set an icon image on the button by using the setIcon method. If you uncomment the following code in lines 39 “42:

 // jbtLeft.setIcon(new ImageIcon("image/left.gif")); // jbtRight.setIcon(new ImageIcon("image/right.gif")); // jbtLeft.setText(null); // jbtRight.setText(null); 

the texts are replaced by the icons, as shown in Figure 15.10(a). "image/left.gif" is located in "c:\book\image\left.gif" . Note that the back slash is the Windows file path notation. In Java, the forward slash should be used.

Figure 15.10. You can set an icon on a JButton and access a button using mnemonic keys.

You can set text and an icon on a button at the same time, if you wish, as shown in Figure 15.10(b). By default, the text and icon are centered horizontally and vertically.

The button can also be accessed by using the keyboard mnemonics. Pressing ALT+L is equivalent to clicking the <= button, since you set the mnemonic property to 'L' in the left button (line 35). If you change the left button text to "Left" and the right button to "Right," the L and R in the captions of these buttons will be underlined , as shown in Figure 15.10(b).


[Page 498]

Each button has a tool-tip text (lines 45 “46), which appears when the mouse is set on the button without clicking, as shown in Figure 15.10(c).

Note

Since MessagePanel is not in the Java API, you should place MessagePanel.java in the same directory with ButtonDemo.java.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net