JButton class in java

JButton class

The JButton class is an implementation of a push button. This component has a label and generates an event when pressed. It is used to create a button that have plateform-independent implementation.

Constructors

  • JButton()
  • JButton(String s)
  • JButton(Icon i)

Methods

  • public void setText(String s)
  • public String getText()
  • public void setEnabled(boolean b)
  • public void setIcon(Icon b)
  • public Icon getIcon()
  • public void addActionListener(ActionListener a)
Example
import java.awt.event.*;
import javax.swing.*;
public class test
{
test()
{
JFrame f=new JFrame();
JButton b=new JButton(new ImageIcon(“src/java1.jpg”));
b.setBounds(100,50,100, 40);
f.add(b);
f.setSize(300,400);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new test();
}
}

Output:

Untitled

Leave a Reply

Your email address will not be published. Required fields are marked *