Advertisement
Guest User

Untitled

a guest
Nov 25th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.42 KB | None | 0 0
  1. package com.sodev.pandotchi;
  2.  
  3. import java.awt.BorderLayout;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.SwingUtilities;
  7. import javax.swing.border.EmptyBorder;
  8.  
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.util.Random;
  12. import java.util.Timer;
  13. import java.util.TimerTask;
  14.  
  15. import javax.swing.JButton;
  16. import javax.swing.JLabel;
  17.  
  18. public class SchStPa extends JFrame implements ActionListener {
  19.     private GUI gui;
  20.  
  21.     public SchStPa(GUI gui) {
  22.         this.gui = gui;
  23.     }
  24.    
  25.     public void panRand() {
  26.         Random rand = new Random();
  27.         int i;
  28.         i = (rand.nextInt(3));
  29.         switch(i) {
  30.         case 0:
  31.             wahl = "Schere";
  32.             break;
  33.         case 1:
  34.             wahl = "Stein";
  35.             break;
  36.         case 2:
  37.             wahl = "Papier";
  38.             break;
  39.         }
  40.     }
  41.  
  42.     private JPanel contentPane;
  43.     private JButton btn_schere;
  44.     private JButton btn_Stein;
  45.     private JButton btn_Papier;
  46.     public String auswahl;
  47.     private String wahl;
  48.     private JLabel lblDuHast;
  49.     private JLabel lblNewLabel;
  50.     private JPanel panel;
  51.     private JPanel panel_1;
  52.     private JPanel panel_2;
  53.     private JPanel panel_3;
  54.     private JPanel panel_4;
  55.     private JLabel lblGewonnenHat;
  56.     private String gewinner;
  57.  
  58.     public SchStPa() {
  59.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60.         setBounds(100, 100, 300, 300);
  61.         contentPane = new JPanel();
  62.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  63.         setContentPane(contentPane);
  64.         contentPane.setLayout(new BorderLayout(0, 0));
  65.        
  66.         panel = new JPanel();
  67.         contentPane.add(panel, BorderLayout.NORTH);
  68.        
  69.         btn_schere = new JButton("Schere");
  70.         panel.add(btn_schere);
  71.         btn_schere.setActionCommand( "schere" );
  72.        
  73.         btn_Stein = new JButton("Stein");
  74.         panel.add(btn_Stein);
  75.         btn_Stein.setActionCommand( "stein" );
  76.        
  77.         btn_Papier = new JButton("Papier");
  78.         panel.add(btn_Papier);
  79.         btn_Papier.setActionCommand( "papier" );
  80.        
  81.         panel_1 = new JPanel();
  82.         contentPane.add(panel_1, BorderLayout.CENTER);
  83.         panel_1.setLayout(new BorderLayout(0, 0));
  84.        
  85.         panel_2 = new JPanel();
  86.         panel_1.add(panel_2, BorderLayout.NORTH);
  87.        
  88.         lblDuHast = new JLabel("Du hast:");
  89.         panel_2.add(lblDuHast);
  90.        
  91.         lblNewLabel = new JLabel(auswahl);
  92.         panel_2.add(lblNewLabel);
  93.        
  94.         panel_3 = new JPanel();
  95.         panel_1.add(panel_3, BorderLayout.SOUTH);
  96.        
  97.         panel_4 = new JPanel();
  98.         panel_1.add(panel_4, BorderLayout.CENTER);
  99.         panel_4.setLayout(new BorderLayout(0, 0));
  100.        
  101.         lblGewonnenHat = new JLabel("Gewonnen hat:" +gewinner);
  102.         panel_4.add(lblGewonnenHat, BorderLayout.SOUTH);
  103.        
  104.         btn_schere.addActionListener(new ActionListener() {
  105.             @Override
  106.             public void actionPerformed(ActionEvent arg0) {
  107.                 auswahl = "Schere";
  108.  
  109.             }
  110.  
  111.         });
  112.        
  113.         btn_Stein.addActionListener(new ActionListener() {
  114.             @Override
  115.             public void actionPerformed(ActionEvent arg0) {
  116.                 auswahl = "Stein";
  117.  
  118.             }
  119.  
  120.         });
  121.        
  122.         btn_Papier.addActionListener(new ActionListener() {
  123.             @Override
  124.             public void actionPerformed(ActionEvent arg0) {
  125.                 auswahl = "Papier";
  126.  
  127.             }
  128.  
  129.         });
  130.        
  131.         zeahler();
  132.         panRand();
  133.     }
  134.     public void zeahler() {
  135.         Timer repeat = new Timer();
  136.         TimerTask go = new TimerTask() {
  137.             @Override
  138.             public void run() {
  139.  
  140.                 SwingUtilities.invokeLater(new Runnable() {
  141.                     @Override
  142.                     public void run() {
  143.                         lblNewLabel.setText(auswahl);
  144.                         lblGewonnenHat.setText("Gewonnen hat:" +gewinner);
  145.                        
  146.                     }
  147.                 }
  148.  
  149.                 );
  150.             }
  151.         };
  152.         repeat.schedule(go, 1000, 500);
  153.     }
  154.  
  155.     public void vergleich() {
  156.         if (auswahl == wahl) {
  157.             gewinner = "Gleichstand";
  158.         } else if ((auswahl.equals("Schere")) && (wahl.equals("Stein"))) {
  159.             gewinner = "Dein Panda";
  160.         } else if ((auswahl.equals("Schere")) && (wahl.equals("Papier"))) {
  161.             gewinner = "Du!";
  162.         } else if ((auswahl.equals("Stein")) && (wahl.equals("Papier"))) {
  163.             gewinner = "Dein Panda";
  164.         } else if ((auswahl.equals("Stein")) && (wahl.equals("Schere"))) {
  165.             gewinner = "Du!";
  166.         } else if ((auswahl.equals("Papier")) && (wahl.equals("Schere"))) {
  167.             gewinner = "Dein Panda";
  168.         } else if ((auswahl.equals("Papier")) && (wahl.equals("Stein"))) {
  169.             gewinner = "Du!";
  170.         }
  171.     }
  172.    
  173.  
  174.     @Override
  175.     public void actionPerformed(ActionEvent e) {
  176.        
  177.     }
  178.    
  179.    
  180.     public static void main(String[] args) {
  181.         SwingUtilities.invokeLater(new Runnable() {
  182.             public void run() {
  183.                 try {
  184.                     SchStPa frame = new SchStPa();
  185.  
  186.                     frame.setVisible(true);
  187.                 } catch (Exception e) {
  188.                     e.printStackTrace();
  189.                 }
  190.             }
  191.        
  192.         });
  193.     }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement