Search code examples
javauser-interfacetic-tac-toe

How to edit a label outside the createContents function?


I recently started tinkering with guis in java, and ran into a problem with the check() function. When I tried Label1.setText(""); in the function check(), which is outside the createContents function, it didn't recognize Label1. I know how to create a public variable, like int and Boolean, but not buttons or labels.

import java.util.Random;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
public class TicTacToeTest {
    static int oRoll;
    static int xRoll;
    static boolean matchStart=false;
    static boolean Oturn=false;
    static boolean Xturn=false;
    static Boolean Owins= false;
    static Boolean Xwins= false;
    static int Owin= 0;
    static int Xwin= 0;
    static String Block1 = " ";
    static String Block2 = " ";
    static String Block3 = " ";
    static String Block4 = " ";
    static String Block5 = " ";
    static String Block6 = " ";
    static String Block7 = " ";
    static String Block8 = " ";
    static String Block9 = " ";
    public Shell shell;
    public static void main(String[] args) {
        try {
            TicTacToeTest window = new TicTacToeTest();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    public void createContents() {
        shell = new Shell();
        shell.setSize(450, 314);
        shell.setText("SWT Application");

        Label Oroll = new Label(shell, SWT.NONE);
        Oroll.setBounds(294, 215, 29, 15);
        Oroll.setText("0");

        Label Xroll = new Label(shell, SWT.NONE);
        Xroll.setText("0");
        Xroll.setBounds(371, 213, 29, 15);

        Label Label1 = new Label(shell, SWT.NONE);
        Label.setBounds(268, 75, 120, 15);
        Label.setText("Start to activate turn");
//etc.
}
    public void check() {
        if ((Block1=="O"&&Block2=="O"&&Block3=="O")||(Block4=="O"&&Block5=="O"&&Block6=="O")||(Block7=="O"&&Block8=="O"&&Block9=="O")||(Block1=="O"&&Block4=="O"&&Block7=="O")||(Block2=="O"&&Block5=="O"&&Block8=="O")||(Block3=="O"&&Block6=="O"&&Block9=="O")||(Block1=="O"&&Block5=="O"&&Block9=="O")||(Block3=="O"&&Block5=="O"&&Block7=="O")){
            Owins = true;
            Xwins = false;
            matchStart=false;
            Owin++;
Label1.setText("O wins");
        }
        if ((Block1=="X"&&Block2=="X"&&Block3=="X")||(Block4=="X"&&Block5=="X"&&Block6=="X")||(Block7=="X"&&Block8=="X"&&Block9=="X")||(Block1=="X"&&Block4=="X"&&Block7=="X")||(Block2=="X"&&Block5=="X"&&Block8=="X")||(Block3=="X"&&Block6=="X"&&Block9=="X")||(Block1=="X"&&Block5=="X"&&Block9=="X")||(Block3=="X"&&Block5=="X"&&Block7=="X")){
            Xwins = true;
            Owins = false;
            System.out.println(Xwins);
            matchStart=false;
            Xwin++;
Label1.setText("O wins");
        }
    }

when I tried to setText, it didn't work. why, and how should I fix this?


Solution

  • Okay, comes out I was just stupid. I didn't know that I could create Label1 outside the create contents function so just add this

    Label Label1;
    

    before the main method (beside the rest of static variables)