Sunday, 18 January 2015

How to create simple j button in java

This tutorial shows how to create simple j button and operate in a simple method in this tutorial we show that  clicking on a  exit button frame will closed.

JBUTTON CLASS IN JAVA:

The Jbutton class provides the function of push button .some of its constructor are:-

JButton()

This constructor creates a jbutton with no string,no image icon present on jbutton.

JButton(Icon i)

This constructor is used when we have to create a jbutton with icon on it.

JButton(String s)

Here string in this constructor specifies the text that have to be appear in button.

JButton(String s,Icon i)

This constructor creates jbutton with text and icon on button.


CODING: 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class tab extends JFrame
implements ActionListener {
JButton jb;
tab()
{
super();
setTitle("Simple J Button");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);      
jb =new JButton("EXIT");
FlowLayout f=new FlowLayout();
setLayout(f);
add(jb);
jb.addActionListener(this);
jb.setActionCommand("BUTTON 1");
setSize(250, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
if ("BUTTON 1".equals(ae.getActionCommand()))
{
dispose();
}

}

}
class nbutt
{
public static void main(String args[])
{
tab x= new tab();
}

}

OUTPUT:




No comments:

Post a Comment