import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ImageButtonDemo1 implements ActionListener
{
static JButton b1,b2,b3;
static ImageIcon img1,img2,img3;
boolean check=false;
public static void main(String arg[])
{
img1=new ImageIcon("Image2.gif");
img3=new ImageIcon("Image1.gif");
b1 =new JButton(img1);
b2=new JButton("Swap Image");
//b2.setBackground(Color.yellow);
b3 =new JButton(img3);
b2.addActionListener(new ImageButtonDemo1());
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new FlowLayout());
f.add(b1);
f.add(b2);
f.add(b3);
f.setSize(900,900);
f.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(check==false)
{
b1.setIcon(img3);
b3.setIcon(img1);
check=true;
}
else
{
b1.setIcon(img1);
b3.setIcon(img3);
check=false;
}
}
}
No comments:
Post a Comment