/*
Creating GUI with JLabel and JButton
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GUIPic extends JFrame implements ActionListener
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 200;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
private static final int BUTTON_WIDTH = 80;
private static final int BUTTON_HEIGHT = 30;
private JButton okButton;
private JLabel image1;
public static void main(String[] args)
{
GUIPic frame = new GUIPic();
frame.setVisible(true);
}
public GUIPic( )
{
setTitle("Sample Frame with Button Events");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setResizable(false);
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.white);
okButton = new JButton("Ok");
okButton.setBounds(110, 125, BUTTON_WIDTH, BUTTON_HEIGHT);
okButton.addActionListener(this);
contentPane.add(okButton);
image1 = new JLabel(new ImageIcon("austin_powers.gif")); //put your first image here
image1.setBounds(115,25,75,75);
contentPane.add(image1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event) // Actions
{
JButton clickedButton = (JButton) event.getSource();
if (clickedButton == okButton)
{
ImageIcon img = new ImageIcon("magoo.gif"); //put your second image here
image1.setIcon(img);
image1.setVisible(true);
}
}
}