Sunday 18 January 2015

Filled Under:

JButton With Image Icon In Java

The following tutorial display four push button and textfield.Each button display an icon that represent a flag when button is pressed name of flag is displayed on a text field.In this tutorial  I have used this constructor JButton(Icon)  to create Jbutton. Because  we have to set an image icon on this button.


CODING:

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

class tab extends JFrame

implements ActionListener 
{
JButton jb,jb1,jb2;
JTextField jtf;
tab()
{
super();
setTitle("IMAGE ICON EXAMPLE");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);      
ImageIcon pakistan = new ImageIcon("pk.png");
JButton jb = new JButton(pakistan);
jb.setActionCommand("Pakistan");
jb.addActionListener(this);
ImageIcon ame = new ImageIcon("am.png");
JButton jb1 = new JButton(ame);
jb1.setActionCommand("America");
jb1.addActionListener(this);
ImageIcon jap = new ImageIcon("japan.png");
JButton jb2 = new JButton(jap);
jb2.setActionCommand("JAPAN");
jb2.addActionListener(this);
jtf=new JTextField(15);
FlowLayout f=new FlowLayout();
setLayout(f);
add(jb);
add(jb1);
add(jb2);
add(jtf);
setSize(250, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
jtf.setText(ae.getActionCommand());
}
}
class butt
{
public static void main(String args[])
{
tab x= new tab();
}
}

OUTPUT:









0 comments:

Post a Comment