Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.event.*;
- import java.awt.*;
- import java.awt.event.*;
- public class ImageDisplay extends JFrame{
- private JPanel imagePanel;
- private JPanel buttonPanel;
- private JLabel imageLabel;
- private JButton button;
- private String[] images = {"C:\\Users\\Compsci\\Documents\\1031275\\workspace\\cat1.gif",
- "C:\\Users\\Compsci\\Documents\\1031275\\workspace\\cat2.gif",
- "C:\\Users\\Compsci\\Documents\\1031275\\workspace\\cat3.gif",
- "C:\\Users\\Compsci\\Documents\\1031275\\workspace\\cat4.gif"};
- public ImageDisplay()
- {
- super("Image Rotator");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(new BorderLayout());
- buildImagePanel();
- buildButtonPanel();
- add(imagePanel, BorderLayout.CENTER);
- add(buttonPanel, BorderLayout.SOUTH);
- pack();
- setVisible(true);
- }
- public void buildImagePanel()
- {
- imagePanel = new JPanel();
- imageLabel = new JLabel("");
- ImageIcon img = new ImageIcon(images[0]);
- imageLabel.setIcon(img);
- imagePanel.add(imageLabel);
- }
- public void buildButtonPanel()
- {
- buttonPanel = new JPanel();
- button = new JButton("Change Image");
- button.addActionListener(new ButtonListener());
- buttonPanel.add(button);
- }
- private class ButtonListener implements ActionListener
- {
- private int index = 0;
- public void actionPerformed(ActionEvent e)
- {
- imageLabel.setIcon(new ImageIcon(images[index]));
- index++;
- pack();
- if (index == images.length)
- index = 0;
- }
- }
- public static void main(String[] args)
- {
- new ImageDisplay();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment