Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sodev.pandotchi;
- import java.awt.BorderLayout;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.SwingUtilities;
- import javax.swing.border.EmptyBorder;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Random;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- public class SchStPa extends JFrame implements ActionListener {
- private GUI gui;
- public SchStPa(GUI gui) {
- this.gui = gui;
- }
- public void panRand() {
- Random rand = new Random();
- int i;
- i = (rand.nextInt(3));
- switch(i) {
- case 0:
- wahl = "Schere";
- break;
- case 1:
- wahl = "Stein";
- break;
- case 2:
- wahl = "Papier";
- break;
- }
- }
- private JPanel contentPane;
- private JButton btn_schere;
- private JButton btn_Stein;
- private JButton btn_Papier;
- public String auswahl;
- private String wahl;
- private JLabel lblDuHast;
- private JLabel lblNewLabel;
- private JPanel panel;
- private JPanel panel_1;
- private JPanel panel_2;
- private JPanel panel_3;
- private JPanel panel_4;
- private JLabel lblGewonnenHat;
- private String gewinner;
- public SchStPa() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 300, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(new BorderLayout(0, 0));
- panel = new JPanel();
- contentPane.add(panel, BorderLayout.NORTH);
- btn_schere = new JButton("Schere");
- panel.add(btn_schere);
- btn_schere.setActionCommand( "schere" );
- btn_Stein = new JButton("Stein");
- panel.add(btn_Stein);
- btn_Stein.setActionCommand( "stein" );
- btn_Papier = new JButton("Papier");
- panel.add(btn_Papier);
- btn_Papier.setActionCommand( "papier" );
- panel_1 = new JPanel();
- contentPane.add(panel_1, BorderLayout.CENTER);
- panel_1.setLayout(new BorderLayout(0, 0));
- panel_2 = new JPanel();
- panel_1.add(panel_2, BorderLayout.NORTH);
- lblDuHast = new JLabel("Du hast:");
- panel_2.add(lblDuHast);
- lblNewLabel = new JLabel(auswahl);
- panel_2.add(lblNewLabel);
- panel_3 = new JPanel();
- panel_1.add(panel_3, BorderLayout.SOUTH);
- panel_4 = new JPanel();
- panel_1.add(panel_4, BorderLayout.CENTER);
- panel_4.setLayout(new BorderLayout(0, 0));
- lblGewonnenHat = new JLabel("Gewonnen hat:" +gewinner);
- panel_4.add(lblGewonnenHat, BorderLayout.SOUTH);
- btn_schere.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- auswahl = "Schere";
- }
- });
- btn_Stein.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- auswahl = "Stein";
- }
- });
- btn_Papier.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- auswahl = "Papier";
- }
- });
- zeahler();
- panRand();
- }
- public void zeahler() {
- Timer repeat = new Timer();
- TimerTask go = new TimerTask() {
- @Override
- public void run() {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- lblNewLabel.setText(auswahl);
- lblGewonnenHat.setText("Gewonnen hat:" +gewinner);
- }
- }
- );
- }
- };
- repeat.schedule(go, 1000, 500);
- }
- public void vergleich() {
- if (auswahl == wahl) {
- gewinner = "Gleichstand";
- } else if ((auswahl.equals("Schere")) && (wahl.equals("Stein"))) {
- gewinner = "Dein Panda";
- } else if ((auswahl.equals("Schere")) && (wahl.equals("Papier"))) {
- gewinner = "Du!";
- } else if ((auswahl.equals("Stein")) && (wahl.equals("Papier"))) {
- gewinner = "Dein Panda";
- } else if ((auswahl.equals("Stein")) && (wahl.equals("Schere"))) {
- gewinner = "Du!";
- } else if ((auswahl.equals("Papier")) && (wahl.equals("Schere"))) {
- gewinner = "Dein Panda";
- } else if ((auswahl.equals("Papier")) && (wahl.equals("Stein"))) {
- gewinner = "Du!";
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- try {
- SchStPa frame = new SchStPa();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement