Advertisement
prprice16

Delay test

Mar 25th, 2020
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3.  
  4. import javax.swing.*;
  5.  
  6. public class DelayGUI extends JFrame
  7. {
  8.     private JPanel panel;
  9.     private JLabel lblPic;
  10.     private JButton btnClick;
  11.     private ImageIcon img1, img2;
  12.    
  13.     public DelayGUI()
  14.     {
  15.         super("Delay Test");
  16.         img1 = new ImageIcon("back.png");
  17.         img2 = new ImageIcon("leia.png");
  18.         panel = new JPanel();
  19.         lblPic = new JLabel(img1);
  20.         btnClick = new JButton(img1);
  21.         //panel.add(lblPic);
  22.         panel.add(btnClick);
  23.         btnClick.addActionListener(new ButtonHandler());   
  24.         add(panel);
  25.        
  26.     }
  27.    
  28.     private class ButtonHandler implements ActionListener
  29.     {
  30.         @Override
  31.         public void actionPerformed(ActionEvent e)
  32.         {
  33.             //"show" the image
  34.             btnClick.setIcon(img2);
  35.             //delay
  36.             try {
  37.                 Thread.sleep(5000);
  38.             } catch (InterruptedException e1) {
  39.                 // TODO Auto-generated catch block
  40.                 e1.printStackTrace();
  41.             }
  42.             // now change pic back
  43.             //btnClick.setIcon(img1);      
  44.         }
  45.        
  46.     }
  47.    
  48.     public static void main(String[] args)
  49.     {
  50.         // create object
  51.         DelayGUI frame = new DelayGUI();
  52.         frame.setSize(300, 300);
  53.         frame.setVisible(true);
  54.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.  
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement