如何在 createContents 函数之外编辑标签?
How to edit a label outside the createContents function?
我最近开始在 java 中修改 guis,并 运行 解决了 check() 函数的问题。当我尝试 Label1.setText("");在 createContents 函数之外的函数 check() 中,它无法识别 Label1。我知道如何创建一个 public 变量,例如 int 和 Boolean,但不知道如何创建按钮或标签。
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");
}
}
当我尝试设置文本时,它不起作用。为什么,我应该如何解决这个问题?
好吧,事实证明我只是愚蠢。我不知道我可以在创建内容函数之外创建 Label1
所以只需添加这个
Label Label1;
在 main 方法之前(除了其余的静态变量)
我最近开始在 java 中修改 guis,并 运行 解决了 check() 函数的问题。当我尝试 Label1.setText("");在 createContents 函数之外的函数 check() 中,它无法识别 Label1。我知道如何创建一个 public 变量,例如 int 和 Boolean,但不知道如何创建按钮或标签。
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");
}
}
当我尝试设置文本时,它不起作用。为什么,我应该如何解决这个问题?
好吧,事实证明我只是愚蠢。我不知道我可以在创建内容函数之外创建 Label1 所以只需添加这个
Label Label1;
在 main 方法之前(除了其余的静态变量)