JButton class:
The
JButton class is used to create a button that have plateform-independent
implementation.
|
Commonly
used Constructors:
- JButton(): creates a button with no text and
icon.
- JButton(String
s): creates a
button with the specified text.
- JButton(Icon
i): creates a
button with the specified icon object.
Commonly
used Methods of AbstractButton class:
1) public void
setText(String s): is
used to set specified text on button.
|
2) public String
getText(): is
used to return the text of the button.
|
3) public void
setEnabled(boolean b): is
used to enable or disable the button.
|
4) public void
setIcon(Icon b): is
used to set the specified Icon on the button.
|
5) public Icon
getIcon(): is
used to get the Icon of the button.
|
6) public void
setMnemonic(int a): is
used to set the mnemonic on the button.
|
7) public void
addActionListener(ActionListener a): is used to add the action listener to
this object.
|
Note: The JButton class extends AbstractButton class.
Example
of displaying image on the button:
1. import java.awt.event.*;
2. import javax.swing.*;
3.
4. public class ImageButton{
5. ImageButton(){
6. JFrame f=new JFrame();
7.
8.
9. JButton b=new JButton(new ImageIcon("b.jpg"));
10. b.setBounds(130,100,100, 40);
11.
12. f.add(b);
13.
14. f.setSize(300,400);
15. f.setLayout(null);
16. f.setVisible(true);
17.
18. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19.
20. }
21.
22. public static void main(String[] args) {
23. new ImageButton();
24. }
25. }
No comments:
Post a Comment