Nick-O-Rama

ImageDisplay

Mar 11th, 2015
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.event.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class ImageDisplay extends JFrame{
  7.    
  8.     private JPanel imagePanel;
  9.     private JPanel buttonPanel;
  10.     private JLabel imageLabel;
  11.     private JButton button;
  12.     private String[] images = {"C:\\Users\\Compsci\\Documents\\1031275\\workspace\\cat1.gif",
  13.                                "C:\\Users\\Compsci\\Documents\\1031275\\workspace\\cat2.gif",
  14.                                "C:\\Users\\Compsci\\Documents\\1031275\\workspace\\cat3.gif",
  15.                                "C:\\Users\\Compsci\\Documents\\1031275\\workspace\\cat4.gif"};
  16.    
  17.     public ImageDisplay()
  18.     {
  19.         super("Image Rotator");
  20.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21.         setLayout(new BorderLayout());
  22.        
  23.         buildImagePanel();
  24.         buildButtonPanel();
  25.        
  26.         add(imagePanel, BorderLayout.CENTER);
  27.         add(buttonPanel, BorderLayout.SOUTH);
  28.        
  29.         pack();
  30.         setVisible(true);
  31.     }
  32.    
  33.     public void buildImagePanel()
  34.     {
  35.         imagePanel = new JPanel();
  36.         imageLabel = new JLabel("");
  37.         ImageIcon img = new ImageIcon(images[0]);
  38.         imageLabel.setIcon(img);
  39.         imagePanel.add(imageLabel);
  40.     }
  41.    
  42.     public void buildButtonPanel()
  43.     {
  44.         buttonPanel = new JPanel();
  45.         button = new JButton("Change Image");
  46.         button.addActionListener(new ButtonListener());
  47.         buttonPanel.add(button);
  48.     }
  49.    
  50.     private class ButtonListener implements ActionListener
  51.     {
  52.         private int index = 0;
  53.         public void actionPerformed(ActionEvent e)
  54.         {
  55.             imageLabel.setIcon(new ImageIcon(images[index]));
  56.             index++;
  57.             pack();
  58.             if (index == images.length)
  59.                 index = 0;
  60.         }
  61.     }
  62.    
  63.     public static void main(String[] args)
  64.     {
  65.         new ImageDisplay();
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment