Guest User

Untitled

a guest
Dec 10th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. //Imports für GUI
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5.  
  6. // Imports für .jpg Darstellung
  7. import java.awt.Graphics;
  8. import java.awt.image.BufferedImage;
  9. import java.io.File;
  10. import javax.imageio.ImageIO;
  11. import javax.swing.ImageIcon;
  12.  
  13. public class Bildknopftest extends JFrame implements ActionListener{
  14.  
  15.  
  16.  
  17.  
  18.   JPanel panel= new JPanel();
  19.  
  20.   JLabel ergebnis = new JLabel("Bis jetzt wurde der Button nicht gedrueckt");
  21.  
  22.   public static void main (String[] args){
  23.    
  24.     Bildknopftest knopfi = new Bildknopftest();
  25.   }
  26.  
  27.  
  28.   Bildknopftest(){
  29.    
  30.     BufferedImage MeinBild= null;
  31.    
  32.     try {
  33.       MeinBild = ImageIO.read(new File("testbild.jpg"));
  34.     }
  35.     catch (Exception b) {
  36.       b.printStackTrace();
  37.     }  
  38.    
  39.     ImageIcon icon = new ImageIcon(MeinBild);
  40.     JButton PicButton = new JButton(icon);
  41.    
  42.     panel.add(ergebnis);
  43.     panel.add(PicButton);
  44.     add(panel);
  45.    
  46.     PicButton.addActionListener(this);
  47.     PicButton.setActionCommand("knopf");
  48.     setVisible(true);
  49.    
  50.                          
  51.     Timer timi =new Timer(3000, this);
  52.     timi.setRepeats(false);
  53.     timi.setActionCommand("timer");
  54.     timi.start();
  55.    
  56.    
  57.   }
  58.  
  59.  
  60.   public void actionPerformed(ActionEvent e){
  61.      if (e.getActionCommand().equals("knopf")) {
  62.         ergebnis.setText("Button wurde in den letzen 3 Sekunden gedrueckt");
  63.         timi.restart();
  64.      }
  65.      else if (e.getActionCommand().equals("timer")) {
  66.          ergebnis.setText("Button wurde in den letzen 3 Sekunden nicht gedrueckt");
  67.      }
  68.   }  
  69. }
Add Comment
Please, Sign In to add comment