#1
13.03.2016 um 21:03 UhrSieht gut aus.
Java :: Aufgabe #99 :: Lösung #3
package java_helloworld;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* Regeln: Schere schneidet Papier; Papier bedeckt Stein; Stein zerquetscht
* Echse; Echse vergiftet Spock; Spock zertrümmert Schere; Schere köpft Echse;
* Echse frisst Papier; Papier widerlegt Spock; Spock verdampft Stein; Stein
* schleift Schere
*/
public class SteinPapierSchereEchseSpock{
private JFrame frm;
private JLabel lblHeader;
private JLabel lblStatus;
private JPanel pnl;
public SteinPapierSchereEchseSpock() {
prepareGUI();
}
public static void main(String[] args) {
SteinPapierSchereEchseSpock spses = new SteinPapierSchereEchseSpock();
spses.showButton();
}
private void prepareGUI() {
frm = new JFrame("Stein, Papier, Schere, Echse, Spock");
frm.setSize(400, 400);
frm.setLayout(new GridLayout(3, 4));
frm.setLocationRelativeTo(null);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lblHeader = new JLabel("", JLabel.CENTER);
lblStatus = new JLabel("", JLabel.CENTER);
lblStatus.setSize(350, 100);
pnl = new JPanel();
pnl.setLayout(new FlowLayout());
frm.add(lblHeader);
frm.add(pnl);
frm.add(lblStatus);
frm.setVisible(true);
}
private void showButton() {
lblHeader.setText("Bitte wähle Deine Figur");
JButton btnSchere = new JButton("Schere");
JButton btnStein = new JButton("Stein");
JButton btnPapier = new JButton("Papier");
JButton btnEchse = new JButton("Echse");
JButton btnSpock = new JButton("Spock");
btnSchere.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int compChoice = (int)(Math.random()*4);
if (compChoice == 0)
lblStatus.setText("Unentschieden, probiers nochmal!");
else if (compChoice == 1)
lblStatus.setText("Verloren - Stein schleift Schere!");
else if (compChoice == 2)
lblStatus.setText("GEWONNEN! Deine Schere schneidet Papier");
else if (compChoice == 3)
lblStatus.setText("GEWONNEN! Deine Schere köpft Echse");
else if (compChoice == 4)
lblStatus.setText("Verloren - Spock zertrümmert Schere!");
}
});
btnStein.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int compChoice = (int)(Math.random()*4);
if (compChoice == 0)
lblStatus.setText("GEWONNEN! Dein Stein schleift Schere");
else if (compChoice == 1)
lblStatus.setText("Unentschieden, probiers nochmal!");
else if (compChoice == 2)
lblStatus.setText("Verloren - Papier bedeckt Stein!");
else if (compChoice == 3)
lblStatus.setText("GEWONNEN! Dein Stein zerquetscht Echse");
else if (compChoice == 4)
lblStatus.setText("Verloren - Spock verdampft Stein!");
}
});
btnPapier.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int compChoice = (int)(Math.random()*4);
if (compChoice == 0)
lblStatus.setText("Verloren - Schere schneidet Papier!");
else if (compChoice == 1)
lblStatus.setText("GEWONNEN! Dein Papier bedeckt Stein");
else if (compChoice == 2)
lblStatus.setText("Unentschieden, probiers nochmal!");
else if (compChoice == 3)
lblStatus.setText("Verloren - Echse frisst Papier!");
else if (compChoice == 4)
lblStatus.setText("GEWONNEN! Dein Papier widerlegt Spock");
}
});
btnEchse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int compChoice = (int)(Math.random()*4);
if (compChoice == 0)
lblStatus.setText("Verloren - Schere köpft Echse!");
else if (compChoice == 1)
lblStatus.setText("Verloren - Stein zerquetscht Echse!");
else if (compChoice == 2)
lblStatus.setText("GEWONNEN! Deine Echse frisst Papier");
else if (compChoice == 3)
lblStatus.setText("Unentschieden, probiers nochmal!");
else if (compChoice == 4)
lblStatus.setText("GEWONNEN! - Deine Echse vergiftet Spock"); }
});
btnSpock.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int compChoice = (int)(Math.random()*4);
if (compChoice == 0)
lblStatus.setText("GEWONNEN! Dein Spock zertrümmert Schere");
else if (compChoice == 1)
lblStatus.setText("GEWONNEN! Dein Spock verdampft Stein");
else if (compChoice == 2)
lblStatus.setText("Verloren - Papier widerlegt Spock!");
else if (compChoice == 3)
lblStatus.setText("Verloren - Echse vergiftet Spock!");
else if (compChoice == 4)
lblStatus.setText("Unentschieden, probiers nochmal!");
}
});
pnl.add(btnSchere);
pnl.add(btnStein);
pnl.add(btnPapier);
pnl.add(btnEchse);
pnl.add(btnSpock);
frm.setVisible(true);
}
}
Kommentare:
Sollder1
Punkte: 270
7 Lösungen
3 Kommentare