The following tables list the commonly used button-related API.
Method or Constructor | Purpose |
---|---|
JButton(String, Icon)
|
Create a JButton instance,
initializing it to have the specified text/image.
|
void setText(String)
|
Set or get the text displayed by the button. |
void setIcon(Icon)
|
Set or get the image displayed by the button when the button isn't selected or pressed. |
void setDisabledIcon(Icon)
|
Set or get the image displayed by the button when it's disabled. If you don't specify a disabled image, then the look and feel creates one by manipulating the default image. |
void setPressedIcon(Icon)
|
Set or get the image displayed by the button when it's being pressed. |
setRolloverEnabled(boolean)
|
Use setRolloverEnabled(true) and
setRolloverIcon(someIcon)
to make the button display the specified icon
when the cursor passes over it.
|
Method or Constructor | Purpose |
---|---|
void setHorizontalAlignment(int)
|
Set or get where in the button its contents should be placed.
The AbstractButton class allows
any one of the following values
for horizontal alignment:
LEFT ,
CENTER (the default), and
RIGHT .
For vertical alignment:
TOP ,
CENTER (the default), and
BOTTOM .
|
void setHorizontalTextPosition(int)
|
Set or get where the button's text should be placed,
relative to the button's image.
The AbstractButton class allows
any one of the following values
for horizontal position:
LEFT ,
CENTER , and
RIGHT (the default).
For vertical position:
TOP ,
CENTER (the default), and
BOTTOM .
|
Create a class DogButton
extends JButton
with the following icons
![]() icon filename : doggie1.gif |
![]() icon when you roll over(move mouse onto) the button. filename : doggie2.gif |
![]() icon when you press the button. filename : doggie3.gif |
Here is an example on how to use the DogButton
class.
You can get the program here.
![]() normal look |
![]() move mouse on the button. |
![]() press the button. |
1:import java.awt.*; 2:import javax.swing.*; 3: 4:public class DogButtonTest{ 5: public static void main(String[] args) { 6: JFrame f = new JFrame(); 7: f.getContentPane().add(new DogButton()); 8: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 9: f.setTitle("Dog"); 10: f.setSize(100,80); 11: f.setVisible(true); 12: } 13:}
See how we use the constructor at line 7.
You can get the program here.
Remember to save the pictures by right clicking on the pictures and select "save
image".
1:import java.awt.*; 2:import javax.swing.*; 3: 4:public class DogButton extends JButton { 5: private Icon icon = new ImageIcon("doggie1.gif"); 6: private Icon rolloverIcon = new ImageIcon("doggie2.gif"); 7: private Icon pressedIcon = new ImageIcon("doggie3.gif"); 8: public DogButton() { 9: setIcon(icon); 10: setRolloverIcon(rolloverIcon); 11: setPressedIcon(pressedIcon); 12: } 13:}
What is wrong if I replace the constructor by
public DogButton() { super(icon); setRolloverIcon(rolloverIcon); setPressedIcon(pressedIcon); }
Answer:
super(icon)
is the first line to be called. But at that point of
the program, icon
hasn't been declared yet.
We are going to write a program react to user's clicks.
The class extends JFrame
, when you click on the button, the
counter increase by 1.
You can get the program here.
Don't worry if you don't really understand the program. We will
discuss the concept in more detail later.
1:import java.awt.*; 2:import javax.swing.*; 3:import java.awt.event.*; 4: 5:public class ButtonTest extends JFrame implements ActionListener { 6: private JLabel countLabel; 7: private JButton button; 8: private int count = 0; 9: private Container contentPane; 10: public ButtonTest() { 11: contentPane = getContentPane(); 12: contentPane.setLayout(new GridLayout(2,1)); 13: // change the count to string. alignment is center 14: countLabel = new JLabel(Integer.toString(count), JLabel.CENTER); 15: button = new JButton("Click me"); 16: contentPane.add(countLabel); 17: contentPane.add(button); 18: // the class is an action listener 19: button.addActionListener(this); 20: } 21: 22: // what happens after you click the button 23: public void actionPerformed(ActionEvent e) { 24: count++; 25: countLabel.setText(Integer.toString(count)); 26: } 27: 28: public static void main(String[] args) { 29: ButtonTest f = new ButtonTest(); 30: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 31: f.pack(); 32: f.setVisible(true); 33: } 34:}
Line 5 : implements
has almost the exact meaning as
extends
. We have to override actionPerformed
method inherited from the class ActionListener
.
Line 19 : addActionListener
to the button. ButtonTest
"extends" ActionListener
, so it itself is an
ActionListener
.
Line 23 - Line 26 : actionPerformed
method. Describe the
reaction of the class when the user clicks on it.