Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

JAVA Swing 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();
	}
}

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext