0% found this document useful (0 votes)
9 views9 pages

Java JButton Class Overview and Examples

The document provides an overview of the Java JButton, JLabel, and JTextField classes, including their declarations, commonly used constructors, and methods. It includes example code for creating buttons and labels with action listeners, as well as displaying images on buttons. The JButton class allows for creating interactive buttons, while JLabel is used for displaying text, and JTextField is for editing single-line text input.

Uploaded by

Sherin Dorothy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Java JButton Class Overview and Examples

The document provides an overview of the Java JButton, JLabel, and JTextField classes, including their declarations, commonly used constructors, and methods. It includes example code for creating buttons and labels with action listeners, as well as displaying images on buttons. The JButton class allows for creating interactive buttons, while JLabel is used for displaying text, and JTextField is for editing single-line text input.

Uploaded by

Sherin Dorothy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java JButton

The JButton class is used to create a labeled button that has platform
independent implementation. The application result in some action when
the button is pushed. It inherits AbstractButton class.

JButton class declaration


Let's see the declaration for [Link] class.

1. public class JButton extends AbstractButton implements Accessible

Commonly used Constructors:

Constructor Description

JButton() It creates a button with no text and icon.

JButton(String s) It creates a button with the specified text.

JButton(Icon i) It creates a button with the specified icon


object.

Commonly used Methods of AbstractButton class:

Methods Description

void setText(String s) It is used to set specified text


on button

String getText() It is used to return the text of


the button.

void setEnabled(boolean b) It is used to enable or disable


the button.

void setIcon(Icon b) It is used to set the specified


Icon on the button.

Icon getIcon() It is used to get the Icon of the


button.

void setMnemonic(int a) It is used to set the mnemonic


on the button.
void It is used to add the action
addActionListener(ActionListener listener to this object.
a)

Java JButton Example


1. import [Link].*;
2. public class ButtonExample {
3. public static void main(String[] args) {
4. JFrame f=new JFrame("Button Example");
5. JButton b=new JButton("Click Here");
6. [Link](50,100,95,30);
7. [Link](b);
8. [Link](400,400);
9. [Link](null);
10. [Link](true);
11. }
12. }

Output:

Java JButton Example with ActionListener


1. import [Link].*;
2. import [Link].*;
3. public class ButtonExample {
4. public static void main(String[] args) {
5. JFrame f=new JFrame("Button Example");
6. final JTextField tf=new JTextField();
7. [Link](50,50, 150,20);
8. JButton b=new JButton("Click Here");
9. [Link](50,100,95,30);
10. [Link](new ActionListener(){
11. public void actionPerformed(ActionEvent e){
12. [Link]("Welcome to Javatpoint.");
13. }
14. });
15. [Link](b);[Link](tf);
16. [Link](400,400);
17. [Link](null);
18. [Link](true);
19. }
20. }

Output:

Example of displaying image on the button:


1. import [Link].*;
2. public class ButtonExample{
3. ButtonExample(){
4. JFrame f=new JFrame("Button Example");
5. JButton b=new JButton(new ImageIcon("D:\\[Link]"));
6. [Link](100,100,100, 40);
7. [Link](b);
8. [Link](300,400);
9. [Link](null);
10. [Link](true);
11. [Link](JFrame.EXIT_ON_CLOSE);
12. }
13. public static void main(String[] args) {
14. new ButtonExample();
15. }
16. }

Output:

Java JLabel
The object of JLabel class is a component for placing text in a container. It
is used to display a single line of read only text. The text can be changed
by an application but a user cannot edit it directly. It inherits JComponent
class.

JLabel class declaration


Let's see the declaration for [Link] class.
1. public class JLabel extends JComponent implements SwingConstants,
Accessible

Commonly used Constructors:

Constructor Description

JLabel() Creates a JLabel instance with no


image and with an empty string for
the title.

JLabel(String s) Creates a JLabel instance with the


specified text.

JLabel(Icon i) Creates a JLabel instance with the


specified image.

JLabel(String s, Icon i, int Creates a JLabel instance with the


horizontalAlignment) specified text, image, and horizontal
alignment.

Commonly used Methods:

Methods Description

String getText() t returns the text string that a


label displays.

void setText(String text) It defines the single line of text


this component will display.

void It sets the alignment of the label's


setHorizontalAlignment(int contents along the X axis.
alignment)

Icon getIcon() It returns the graphic image that


the label displays.

int getHorizontalAlignment() It returns the alignment of the


label's contents along the X axis.

Java JLabel Example


1. import [Link].*;
2. class LabelExample
3. {
4. public static void main(String args[])
5. {
6. JFrame f= new JFrame("Label Example");
7. JLabel l1,l2;
8. l1=new JLabel("First Label.");
9. [Link](50,50, 100,30);
10. l2=new JLabel("Second Label.");
11. [Link](50,100, 100,30);
12. [Link](l1); [Link](l2);
13. [Link](300,300);
14. [Link](null);
15. [Link](true);
16. }
17. }

Output:

Java JLabel Example with ActionListener


1. import [Link].*;
2. import [Link].*;
3. import [Link].*;
4. public class LabelExample extends Frame implements ActionListener{
5. JTextField tf; JLabel l; JButton b;
6. LabelExample(){
7. tf=new JTextField();
8. [Link](50,50, 150,20);
9. l=new JLabel();
10. [Link](50,100, 250,20);
11. b=new JButton("Find IP");
12. [Link](50,150,95,30);
13. [Link](this);
14. add(b);add(tf);add(l);
15. setSize(400,400);
16. setLayout(null);
17. setVisible(true);
18. }
19. public void actionPerformed(ActionEvent e) {
20. try{
21. String host=[Link]();
22. String ip=[Link](host).getHostAddre
ss();
23. [Link]("IP of "+host+" is: "+ip);
24. }catch(Exception ex){[Link](ex);}
25. }
26. public static void main(String[] args) {
27. new LabelExample();
28. }}

Output:
Java JTextField
The object of a JTextField class is a text component that allows the editing
of a single line text. It inherits JTextComponent class.

JTextField class declaration


Let's see the declaration for [Link] class.

1. public class JTextField extends JTextComponent implements SwingCon


stants

Commonly used Constructors:

Constructor Description
JTextField() Creates a new TextField

JTextField(String text) Creates a new TextField initialized with


the specified text.

JTextField(String text, Creates a new TextField initialized with


int columns) the specified text and columns.

JTextField(int columns) Creates a new empty TextField with the


specified number of columns.

Commonly used Methods:

Methods Description

void It is used to add the specified


addActionListener(ActionListener action listener to receive action
l) events from this textfield.

Action getAction() It returns the currently set


Action for this ActionEvent
source, or null if no Action is
set.

void setFont(Font f) It is used to set the current


font.

void It is used to remove the


removeActionListener(ActionList specified action listener so that
ener l) it no longer receives action
events from this textfield.

You might also like