main>Karl Kirst |
main>Karl Kirst |
Zeile 1: |
Zeile 1: |
| [[Bild:Tictactoe.jpg]]
| | {{Kurzinfo-2|Java|Idee}} |
|
| |
|
| ==Variante mit Konsolenein- und ausgabe== | | == Motivation == |
| Eine einfache TicTacToe-Variante für [[BlueJ]]. Ein und Ausgabe in der Konsole.
| |
|
| |
|
| <source lang="java">
| | Häufig benötigt man beim Programmieren eine mehrfache Ausführung eines Teilbereichs. |
| import java.io.*; // wird von der Eingabe benötigt
| | |
| | Denkbar ist es, dass ein Programmierer dazu einfach Zeilen wiederholt. |
|
| |
|
| public class Spielfeld {
| | So lässt sich z.B. das kleine 1 x 1 von 3 wie folgt ausgeben: |
| //Eigenschaften
| |
| private Kaestchen[][] feld;
| |
| private static BufferedReader in; // Eingabe
| |
|
| |
| //Konstruktor
| |
| public Spielfeld() {
| |
| feld = new Kaestchen[3][3];
| |
| for (int i=0;i<3;i++){// senkrecht
| |
| for (int j=0;j<3;j++){// waagerecht
| |
| feld[j][i]=new Kaestchen();
| |
| }
| |
| }
| |
|
| |
| // Vorbereitung für die Eingabe
| |
| in = new BufferedReader(new InputStreamReader(System.in));
| |
|
| |
| }
| |
|
| |
| //Methoden
| |
| public void setzeSpielstein(int x, int y, String stein){
| |
| feld[x][y].setzeStein(stein);
| |
| }
| |
|
| |
| public void gibSpielAus(){
| |
| System.out.println("\n 012");
| |
| for (int i=0;i<3;i++){
| |
| System.out.print(i+" ");
| |
| for (int j=0;j<3;j++){
| |
| System.out.print(feld[j][i].getZeichen());
| |
| }
| |
| System.out.println(); // Zeilenumbruch nach 3 Kaestchen
| |
| }
| |
|
| |
| }
| |
|
| |
| public void spieleEineRunde(){
| |
| // unguenstig: Starre Anzahl von Zügen, immer 10 Züge
| |
| int x,y;
| |
| for (int i=1;i<5;i++){
| |
| gibSpielAus();
| |
| System.out.print("Spieler X bitte die x-Koordinate (0-2)");
| |
| try {
| |
| x=liesInt();
| |
| }
| |
| catch(IOException e) {
| |
| x=-1;
| |
| }
| |
| System.out.print("Spieler X bitte die y-Koordinate (0-2)");
| |
| try {
| |
| y=liesInt();
| |
| }
| |
| catch(IOException e) {
| |
| y=-1;
| |
| }
| |
| setzeSpielstein(x, y, "X");
| |
| gibSpielAus();
| |
| System.out.print("Spieler O bitte die x-Koordinate (0-2)");
| |
| try {
| |
| x=liesInt();
| |
| }
| |
| catch(IOException e) {
| |
| x=-1;
| |
| }
| |
| System.out.print("Spieler O bitte die y-Koordinate (0-2)");
| |
| try {
| |
| y=liesInt();
| |
| }
| |
| catch(IOException e) {
| |
| y=-1;
| |
| }
| |
| setzeSpielstein(x, y, "O");
| |
| }
| |
| }
| |
|
| |
| // Methode für die Eingabe
| |
| // Mehr hier: http://www.wifo.uni-mannheim.de/Java/oop/OOPinJava/kapitel11/StandardEingabe_java.html
| |
| public int liesInt() throws IOException {
| |
| try {
| |
| return Integer.parseInt(in.readLine());
| |
| }
| |
| catch(IOException e) {
| |
| return (-1);
| |
| }
| |
| }
| |
| }
| |
| </source>
| |
|
| |
|
| <source lang="java"> | | <source lang="java"> |
| public class Kaestchen {
| | System.out.println("3 x 1 = 3"); |
| String zeichen;
| | System.out.println("3 x 2 = 6"); |
|
| | System.out.println("3 x 3 = 9"); |
| public Kaestchen() {
| | System.out.println("3 x 4 = 12"); |
| zeichen=" "; // Am Anfang leer
| | System.out.println("3 x 5 = 15"); |
| }
| | System.out.println("3 x 6 = 18"); |
|
| | System.out.println("3 x 7 = 21"); |
| public void setzeStein(String pZeichen) {
| | System.out.println("3 x 8 = 24"); |
| if (!zeichen.equals(" ")) {System.out.println("Besetzt!");}
| | System.out.println("3 x 9 = 27"); |
| else {
| |
| if (pZeichen.equals("X") || pZeichen.equals("O")) {zeichen=pZeichen;}
| |
| else System.out.println("Ungueltige Eingabe, bitte X oder O waehlen!");
| |
| }
| |
| }
| |
|
| |
| public String getZeichen(){
| |
| return zeichen;
| |
| }
| |
| }
| |
| </source> | | </source> |
|
| |
|
| ==Variante mit GUI== | | == Lösung mit while-Schleife == |
|
| |
|
| Die folgende Variante wurde mit Netbeans-BlueJ erstellt. Sie soll ein Beispiel für den Umgang mit GUIs sein.
| | Eleganter ist da schon die Verwendung der [[java/while-Schleife|while-Schleife]] |
|
| |
|
| <source lang="java"> | | <source lang="java"> |
| /*
| | int faktor = 1; |
| * TicTacGUI.java
| | while ( faktor <= 9 ) { |
| *
| | System.out.println("3 x " + faktor + " = " + 3*faktor ); |
| * Created on 12. März 2007, 19:15
| | faktor++; |
| */
| | } |
| | </source> |
|
| |
|
| /**
| | == Lösung mit for-Schleife == |
| *
| |
| * @author ugh
| |
| */
| |
| public class TicTacGUI extends javax.swing.JFrame {
| |
| private int amZug;
| |
| /** Creates new form TicTacGUI */
| |
| public TicTacGUI() {
| |
| initComponents();
| |
| amZug=0;
| |
| textField2.setText("0 startet das Spiel.");
| |
| label1.setText("Tic-Tac-Toe");
| |
| button1.setLabel("_");
| |
| button2.setLabel("_");
| |
| button3.setLabel("_");
| |
| button4.setLabel("_");
| |
| button5.setLabel("_");
| |
| button6.setLabel("_");
| |
| button7.setLabel("_");
| |
| button8.setLabel("_");
| |
| button9.setLabel("_");
| |
| }
| |
|
| |
| /** This method is called from within the constructor to
| |
| * initialize the form.
| |
| * WARNING: Do NOT modify this code. The content of this method is
| |
| * always regenerated by the Form Editor.
| |
| */
| |
| // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
| |
| private void initComponents() {
| |
| button1 = new java.awt.Button();
| |
| button2 = new java.awt.Button();
| |
| button3 = new java.awt.Button();
| |
| button4 = new java.awt.Button();
| |
| button5 = new java.awt.Button();
| |
| button6 = new java.awt.Button();
| |
| button7 = new java.awt.Button();
| |
| button8 = new java.awt.Button();
| |
| button9 = new java.awt.Button();
| |
| textField1 = new java.awt.TextField();
| |
| label1 = new java.awt.Label();
| |
| textField2 = new java.awt.TextField();
| |
|
| |
|
| setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
| | Für genau diesen Fall gibt es aber auch eine Schleife: die for-Schleife. Sie ist immer dann sinnvoll einsetzbar, |
| button1.setLabel("button1");
| | * wenn eine bestimmte Anzahl von Wiederholungen durchgeführt werden soll. |
| button1.addMouseListener(new java.awt.event.MouseAdapter() {
| | * wenn eine Variable von einem bestimmten Wert bis zu einem anderen gezählt werden soll. |
| public void mouseClicked(java.awt.event.MouseEvent evt) {
| |
| button1MouseClicked(evt);
| |
| }
| |
| });
| |
|
| |
|
| button2.setLabel("button2");
| | Das folgende Beispiel macht genau das selbe wie die while-Schleife im obigen Beispiel: |
| button2.addMouseListener(new java.awt.event.MouseAdapter() {
| |
| public void mouseClicked(java.awt.event.MouseEvent evt) {
| |
| button2MouseClicked(evt);
| |
| }
| |
| });
| |
|
| |
|
| button3.setLabel("button3");
| | <source lang="java"> |
| button3.addMouseListener(new java.awt.event.MouseAdapter() {
| | for ( int faktor = 1; faktor <= 9; faktor ++ ) { |
| public void mouseClicked(java.awt.event.MouseEvent evt) {
| | System.out.println("3 x " + faktor + " = " + 3*faktor ); |
| button3MouseClicked(evt);
| | } |
| }
| | </source> |
| });
| |
|
| |
|
| button4.setLabel("button4");
| | == Weitere Beispiele == |
| button4.addMouseListener(new java.awt.event.MouseAdapter() {
| |
| public void mouseClicked(java.awt.event.MouseEvent evt) {
| |
| button4MouseClicked(evt);
| |
| }
| |
| });
| |
|
| |
|
| button5.setLabel("button5");
| | <source lang="java"> |
| button5.addMouseListener(new java.awt.event.MouseAdapter() {
| | //allgemeiner Aufbau |
| public void mouseClicked(java.awt.event.MouseEvent evt) {
| | for ( Zählvariable mit Anfangswert; Anfangswert; Bedingung; Schrittweite) { |
| button5MouseClicked(evt);
| | // Anweisung, die wiederholt werden soll |
| }
| | } |
| });
| |
|
| |
|
| button6.setLabel("button6");
| | // hochzählen in 1er-Schritten |
| button6.addMouseListener(new java.awt.event.MouseAdapter() {
| | for ( int zählvariable = Anfangswert; zählvariable <= Endwert; zählvariable++ ) { |
| public void mouseClicked(java.awt.event.MouseEvent evt) {
| | // Anweisung, die wiederholt werden soll |
| button6MouseClicked(evt);
| | } |
| }
| |
| });
| |
|
| |
|
| button7.setLabel("button7");
| | // hochzählen in 5er-Schritten |
| button7.addMouseListener(new java.awt.event.MouseAdapter() {
| | for ( int zählvariable = Anfangswert; zählvariable <= Endwert; zählvariable+=5 ) { |
| public void mouseClicked(java.awt.event.MouseEvent evt) {
| | // Anweisung, die wiederholt werden soll |
| button7MouseClicked(evt);
| | } |
| }
| |
| });
| |
|
| |
|
| button8.setLabel("button8");
| | // Countdown |
| button8.addMouseListener(new java.awt.event.MouseAdapter() {
| | for ( int zählvariable = Anfangswert; zählvariable > Endwert; zählvariable-- ) { |
| public void mouseClicked(java.awt.event.MouseEvent evt) {
| | // Anweisung, die wiederholt werden soll |
| button8MouseClicked(evt);
| | } |
| }
| | </source> |
| });
| |
|
| |
|
| button9.setLabel("button9");
| | == Aufgaben == |
| button9.addMouseListener(new java.awt.event.MouseAdapter() {
| |
| public void mouseClicked(java.awt.event.MouseEvent evt) {
| |
| button9MouseClicked(evt);
| |
| }
| |
| });
| |
|
| |
|
| textField1.setText("textField1");
| | {{Übung| |
| | * Nennen Sie Anwendungsgebiete der for-Schleife |
| | * Lassen Sie die Zahlen von 1 bis 100 auf dem Bildschirm ausgeben |
| | * Diesmal nur die geraden Zahlen |
| | * Diesmal von 100 bis -100 |
| | * Die ungeraden von 200 bis -1000 |
| | * Lassen Sie 50 mal den Buchstaben "A" auf den Bildschirm schreiben |
| | * Die Zahlen von -10 bis 10 ohne die 0 |
| | }} |
|
| |
|
| label1.setText("label1");
| | == Linkliste == |
|
| |
|
| textField2.setText("textField2");
| | * http://es.wikibooks.org/wiki/Programaci%C3%B3n_en_Java/Sentencia_for |
| | * http://www.programmersbase.net/Content/Java/Content/Tutorial/Java/Loop.htm |
| | * http://mezmedia.de/cj-share/03-02-26_for-schleife/index.html |
|
| |
|
| org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
| |
| getContentPane().setLayout(layout);
| |
| layout.setHorizontalGroup(
| |
| layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
| |
| .add(layout.createSequentialGroup()
| |
| .add(25, 25, 25)
| |
| .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
| |
| .add(layout.createSequentialGroup()
| |
| .add(textField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
| |
| .addContainerGap())
| |
| .add(layout.createSequentialGroup()
| |
| .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
| |
| .add(label1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
| |
| .add(layout.createSequentialGroup()
| |
| .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
| |
| .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
| |
| .add(button7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
| |
| .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 20, Short.MAX_VALUE)
| |
| .add(button8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
| |
| .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
| |
| .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
| |
| .add(button1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
| |
| .add(button4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
| |
| .add(20, 20, 20)
| |
| .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
| |
| .add(button5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
| |
| .add(button2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
| |
| .add(29, 29, 29)
| |
| .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
| |
| .add(button6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
| |
| .add(button9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
| |
| .add(button3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
| |
| .add(55, 55, 55))
| |
| .add(textField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE))
| |
| .add(75, 75, 75))))
| |
| );
| |
|
| |
| layout.linkSize(new java.awt.Component[] {button1, button2, button3, button4, button5, button6, button7, button8, button9}, org.jdesktop.layout.GroupLayout.HORIZONTAL);
| |
|
| |
| layout.setVerticalGroup(
| |
| layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
| |
| .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
| |
| .addContainerGap()
| |
| .add(label1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
| |
| .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
| |
| .add(textField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
| |
| .add(23, 23, 23)
| |
| .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
| |
| .add(button2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
| |
| .add(button1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
| |
| .add(button3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
| |
| .add(10, 10, 10)
| |
| .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
| |
| .add(layout.createSequentialGroup()
| |
| .add(button6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
| |
| .add(0, 0, 0))
| |
| .add(button5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
| |
| .add(org.jdesktop.layout.GroupLayout.TRAILING, button4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
| |
| .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
| |
| .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
| |
| .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
| |
| .add(button7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
| |
| .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
| |
| .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
| |
| .add(button9, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
| |
| .add(button8, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
| |
| .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
| |
| .add(textField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
| |
| .addContainerGap())
| |
| );
| |
|
| |
| layout.linkSize(new java.awt.Component[] {button1, button2, button3, button4, button5, button6, button7, button8, button9}, org.jdesktop.layout.GroupLayout.VERTICAL);
| |
|
| |
| pack();
| |
| }// </editor-fold>//GEN-END:initComponents
| |
|
| |
| private void button9MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button9MouseClicked
| |
| if (button9.getLabel()=="_") {
| |
| if (amZug==0) button9.setLabel("0");
| |
| else button9.setLabel("X");
| |
| werteAus(2,2);
| |
| }
| |
| else {
| |
| textField1.setText("Hier ist schon ein Stein!");
| |
| }
| |
| }//GEN-LAST:event_button9MouseClicked
| |
|
| |
| private void button8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button8MouseClicked
| |
| if (button8.getLabel()=="_") {
| |
| if (amZug==0) button8.setLabel("0");
| |
| else button8.setLabel("X");
| |
| werteAus(1,2);
| |
| }
| |
| else {
| |
| textField1.setText("Hier ist schon ein Stein!");
| |
| }
| |
| }//GEN-LAST:event_button8MouseClicked
| |
|
| |
| private void button7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button7MouseClicked
| |
| if (button7.getLabel()=="_") {
| |
| if (amZug==0) button7.setLabel("0");
| |
| else button7.setLabel("X");
| |
| werteAus(0,2);
| |
| }
| |
| else {
| |
| textField1.setText("Hier ist schon ein Stein!");
| |
| }
| |
| }//GEN-LAST:event_button7MouseClicked
| |
|
| |
| private void button6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button6MouseClicked
| |
| if (button6.getLabel()=="_") {
| |
| if (amZug==0) button6.setLabel("0");
| |
| else button6.setLabel("X");
| |
| werteAus(2,1);
| |
| }
| |
| else {
| |
| textField1.setText("Hier ist schon ein Stein!");
| |
| }
| |
| }//GEN-LAST:event_button6MouseClicked
| |
|
| |
| private void button5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button5MouseClicked
| |
| if (button5.getLabel()=="_") {
| |
| if (amZug==0) button5.setLabel("0");
| |
| else button5.setLabel("X");
| |
| werteAus(1,1);
| |
| }
| |
| else {
| |
| textField1.setText("Hier ist schon ein Stein!");
| |
| }
| |
| }//GEN-LAST:event_button5MouseClicked
| |
|
| |
| private void button4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button4MouseClicked
| |
| if (button4.getLabel()=="_") {
| |
| if (amZug==0) button4.setLabel("0");
| |
| else button4.setLabel("X");
| |
| werteAus(0,1);
| |
| }
| |
| else {
| |
| textField1.setText("Hier ist schon ein Stein!");
| |
| }
| |
| }//GEN-LAST:event_button4MouseClicked
| |
|
| |
| private void button3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button3MouseClicked
| |
| if (button3.getLabel()=="_") {
| |
| if (amZug==0) button3.setLabel("0");
| |
| else button3.setLabel("X");
| |
| werteAus(2,0);
| |
| }
| |
| else {
| |
| textField1.setText("Hier ist schon ein Stein!");
| |
| }
| |
| }//GEN-LAST:event_button3MouseClicked
| |
|
| |
| private void button2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button2MouseClicked
| |
| if (button2.getLabel()=="_") {
| |
| if (amZug==0) button2.setLabel("0");
| |
| else button2.setLabel("X");
| |
| werteAus(1,0);
| |
| }
| |
| else {
| |
| textField1.setText("Hier ist schon ein Stein!");
| |
| }
| |
| }//GEN-LAST:event_button2MouseClicked
| |
|
| |
| private void button1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button1MouseClicked
| |
| if (button1.getLabel()=="_") {
| |
| if (amZug==0) button1.setLabel("0");
| |
| else button1.setLabel("X");
| |
| werteAus(0,0);
| |
| }
| |
| else {
| |
| textField1.setText("Hier ist schon ein Stein!");
| |
| }
| |
| }//GEN-LAST:event_button1MouseClicked
| |
|
| |
|
| |
|
| |
| private void werteAus(int pX, int pY){
| |
| if (amZug==0) {
| |
| amZug=1;
| |
| textField2.setText("X setzt nun.");
| |
| }
| |
| else {
| |
| amZug=0;
| |
| textField2.setText("O ist dran.");
| |
| }
| |
| }
| |
|
| |
|
| |
| /**
| |
| * @param args the command line arguments
| |
| */
| |
| public static void main(String args[]) {
| |
| java.awt.EventQueue.invokeLater(new Runnable() {
| |
| public void run() {
| |
| new TicTacGUI().setVisible(true);
| |
| }
| |
| });
| |
| }
| |
|
| |
| // Variables declaration - do not modify//GEN-BEGIN:variables
| |
| private java.awt.Button button1;
| |
| private java.awt.Button button2;
| |
| private java.awt.Button button3;
| |
| private java.awt.Button button4;
| |
| private java.awt.Button button5;
| |
| private java.awt.Button button6;
| |
| private java.awt.Button button7;
| |
| private java.awt.Button button8;
| |
| private java.awt.Button button9;
| |
| private java.awt.Label label1;
| |
| private java.awt.TextField textField1;
| |
| private java.awt.TextField textField2;
| |
| // End of variables declaration//GEN-END:variables
| |
|
| |
| }
| |
| </source>
| |
|
| |
|
| [[Kategorie:Java]] | | [[Kategorie:Java]] |