JButton Class

The JButton class which extends AbstractButton class that is used to create a button. JButton supports ActionEvent, that means when a button is clicked, an ActionEvent is generated.

JButton class having three commonly used Constructors, such as:

  1. JButton(): creates a button with no text and icon.
  2. JButton(String str): creates a button with the specified text.
  3. JButton(Icon i): creates a button with the specified icon object.

 

Let?s see an example for displaying an image on the button.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class JButtonDemo extends JFrame 
{
JButtonDemo()
{
JButton b1 = new JButton("save");      //Creating a save Button. 
JButton b2 = new JButton("delete");   //Creating a delete Button.  
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setLayout(new FlowLayout());		//setting layout using FlowLayout object  
setSize(300, 300);			//setting size of Jframe 
add(b1);		                //adding save button to frame. 
add(b2);		                //adding delete button to frame. 
setVisible(true);
}
public static void main(String[] args)
{
 new JButtonDemo();
 }
}
Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

Silansoftware Pvt.Ltd, BBSR
Call to: 0674-2361252

Comment