Skip to main content

Java Swing : JButton, JLabel

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 javax.swing.JButton class.

  1. public class JButton extends AbstractButton implements Accessible  

Commonly used Constructors:

ConstructorDescription
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:

MethodsDescription
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 addActionListener(ActionListener a)It is used to add the action listener to this object.

Java JButton Example

  1. import javax.swing.*;    
  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.     b.setBounds(50,100,95,30);  
  7.     f.add(b);  
  8.     f.setSize(400,400);  
  9.     f.setLayout(null);  
  10.     f.setVisible(true);   
  11. }  
  12. }  

Output:

JAVA Jbutton 1

Java JButton Example with ActionListener

  1. import java.awt.event.*;  
  2. import javax.swing.*;    
  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.     tf.setBounds(50,50150,20);  
  8.     JButton b=new JButton("Click Here");  
  9.     b.setBounds(50,100,95,30);  
  10.     b.addActionListener(new ActionListener(){  
  11. public void actionPerformed(ActionEvent e){  
  12.             tf.setText("Welcome to Javatpoint.");  
  13.         }  
  14.     });  
  15.     f.add(b);f.add(tf);  
  16.     f.setSize(400,400);  
  17.     f.setLayout(null);  
  18.     f.setVisible(true);   
  19. }  
  20. }  

Output:

JAVA Jbutton 2

Example of displaying image on the button:

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

Output:

JAVA Jbutton 3

ava 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 javax.swing.JLabel class.

  1. public class JLabel extends JComponent implements SwingConstants, Accessible  

Commonly used Constructors:

ConstructorDescription
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 horizontalAlignment)Creates a JLabel instance with the specified text, image, and horizontal alignment.

Commonly used Methods:

MethodsDescription
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 setHorizontalAlignment(int alignment)It sets the alignment of the label's contents along the X axis.
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 javax.swing.*;  
  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.     l1.setBounds(50,50100,30);  
  10.     l2=new JLabel("Second Label.");  
  11.     l2.setBounds(50,100100,30);  
  12.     f.add(l1); f.add(l2);  
  13.     f.setSize(300,300);  
  14.     f.setLayout(null);  
  15.     f.setVisible(true);  
  16.     }  
  17.     }  

Output:

JAVA Jlabel 1

Java JLabel Example with ActionListener

  1. import javax.swing.*;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. public class LabelExample extends Frame implements ActionListener{  
  5.     JTextField tf; JLabel l; JButton b;  
  6.     LabelExample(){  
  7.         tf=new JTextField();  
  8.         tf.setBounds(50,50150,20);  
  9.         l=new JLabel();  
  10.         l.setBounds(50,100250,20);      
  11.         b=new JButton("Find IP");  
  12.         b.setBounds(50,150,95,30);  
  13.         b.addActionListener(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=tf.getText();  
  22.         String ip=java.net.InetAddress.getByName(host).getHostAddress();  
  23.         l.setText("IP of "+host+" is: "+ip);  
  24.         }catch(Exception ex){System.out.println(ex);}  
  25.     }  
  26.     public static void main(String[] args) {  
  27.         new LabelExample();  
  28.     } }  

Output:

JAVA Jlabel 2



Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are following header files important to C++ pro