[英]java/javafx issue with static methods & “enclosed classes”
我正在尝试创建一个简单的井字游戏,到目前为止,效果很好,但是在以所需方式操纵变量时遇到了问题。
我有两个类,一个叫做Run,另一个是DrawBoard。 DrawBoard包含用于创建字母的所有方法(X,O,棋盘本身),而Run包含许多主要方法调用和单击处理程序。
我的问题是,如果我使绘图方法不是静态的,那么我将无法在Run(各种getter和setter)中引用方法来修改板状态变量,但是如果使它们成为静态的,则在尝试使用时会出现错误可以从运行中的DrawBoard中获取方法,但由于某些原因,只能封装在单击处理程序中的方法(请注意Drawboard.DrawMainBoard可以工作)。 错误显示“ Drawboard不是封闭类”。
我是Java的新手,所以这可能不是最有效的处理方法,但这是我的代码。
public class Run extends Application
{
Group root = new Group();
Pane characters = new Pane();
boolean isPlayerOneTurn = true;
//Values representing state of a square
int TL, TM, TR, ML, M, MR, BL, BM, BR;
public void start (Stage primaryStage)
{
Scene mainScene = new Scene(root, 600, 600);
DrawBoard.DrawMainBoard board = new DrawBoard.DrawMainBoard();
clicky(mainScene);
root.getChildren().addAll(board,characters);
primaryStage.setTitle("Test Game");
primaryStage.setScene(mainScene);
primaryStage.show();
}
void clicky (Scene scene)
{
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent event) {
if (!event.isControlDown())
{
double mouseXPos = event.getSceneX();
double mouseYPos = event.getSceneY();
if(isPlayerOneTurn)
{
DrawBoard.drawX ex = new DrawBoard.drawX(mouseXPos, mouseYPos);
//The 2 method calls I was referring to
characters.getChildren().add(ex);
isPlayerOneTurn = false;
}
else
{
DrawBoard.drawO oh = new DrawBoard.drawO(mouseXPos, mouseYPos);
characters.getChildren().add(oh);
isPlayerOneTurn = true;
}
}
}
});
}
public int getTL()
{
return TL;
}
public int getTM()
{
return TM;
}
public int getTR()
{
return TR;
}
public int getML()
{
return ML;
}
public int getM()
{
return M;
}
public int getMR()
{
return MR;
}
public int getBL()
{
return BL;
}
public int getBM()
{
return BM;
}
public int getBR()
{
return BR;
}
public void setTL(int newTL)
{
TL = newTL;
}
public void setTM(int newTM)
{
TM = newTM;
}
public void setTR(int newTR)
{
TR = newTR;
}
public void setML(int newML)
{
ML = newML;
}
public void setM(int newM)
{
M = newM;
}
public void setMR(int newMR)
{
MR = newMR;
}
public void setBL(int newBL)
{
BL = newBL;
}
public void setBM(int newBM)
{
BM = newBM;
}
public void setBR(int newBR)
{
BR = newBR;
}
}
public class DrawBoard extends Run
{
public static class DrawMainBoard extends Pane
{
public DrawMainBoard()
{
Line v1 = new Line();
Line v2 = new Line();
Line h1 = new Line();
Line h2 = new Line();
v1.setStartX(200);
v1.setStartY(0);
v1.setEndX(200);
v1.setEndY(600);
v2.setStartX(400);
v2.setStartY(0);
v2.setEndX(400);
v2.setEndY(600);
h1.setStartX(0);
h1.setStartY(200);
h1.setEndX(600);
h1.setEndY(200);
h2.setStartX(0);
h2.setStartY(400);
h2.setEndX(600);
h2.setEndY(400);
getChildren().addAll(v1,v2,h1,h2);
}
}
public class drawX extends Pane
{
public drawX(double mouseXPos, double mouseYPos)
{
Line x1 = new Line();
Line x2 = new Line();
//Top Left
if(mouseXPos < 200 && mouseYPos < 200)
{
if(getTL() != 1 && getTL() != 2)
{
x1.setStartX(50);
x1.setStartY(50);
x1.setEndX(150);
x1.setEndY(150);
x2.setStartX(150);
x2.setStartY(50);
x2.setEndX(50);
x2.setEndY(150);
setTL(1);
}
}
//Top middle
else if (mouseXPos > 200 && mouseXPos < 400 && mouseYPos < 200 )
{
if(getTM() != 1 && getTM() != 2)
{
x1.setStartX(250);
x1.setStartY(50);
x1.setEndX(350);
x1.setEndY(150);
x2.setStartX(350);
x2.setStartY(50);
x2.setEndX(250);
x2.setEndY(150);
setTM(1);
}
}
//Top right
else if (mouseXPos > 400 && mouseYPos < 200)
{
if(getTR() != 1 && getTR() != 2)
{
x1.setStartX(450);
x1.setStartY(50);
x1.setEndX(550);
x1.setEndY(150);
x2.setStartX(550);
x2.setStartY(50);
x2.setEndX(450);
x2.setEndY(150);
setTR(1);
}
}
//Middle left
else if (mouseXPos < 200 && mouseYPos > 200 && mouseYPos < 400)
{
x1.setStartX(50);
x1.setStartY(250);
x1.setEndX(150);
x1.setEndY(350);
x2.setStartX(150);
x2.setStartY(250);
x2.setEndX(50);
x2.setEndY(350);
setML(1);
}
//middle
else if (mouseXPos > 200 && mouseXPos < 400 &mouseYPos > 200 && mouseYPos < 400)
{
x1.setStartX(250);
x1.setStartY(250);
x1.setEndX(350);
x1.setEndY(350);
x2.setStartX(350);
x2.setStartY(250);
x2.setEndX(250);
x2.setEndY(350);
setM(1);
}
//middle right
else if (mouseXPos > 400 && mouseYPos > 200 && mouseYPos < 400)
{
x1.setStartX(450);
x1.setStartY(250);
x1.setEndX(550);
x1.setEndY(350);
x2.setStartX(550);
x2.setStartY(250);
x2.setEndX(450);
x2.setEndY(350);
setMR(1);
}
//bottom left
else if (mouseXPos < 200 && mouseYPos > 400)
{
x1.setStartX(50);
x1.setStartY(450);
x1.setEndX(150);
x1.setEndY(550);
x2.setStartX(150);
x2.setStartY(450);
x2.setEndX(50);
x2.setEndY(550);
setBL(1);
}
//bottom middle
else if (mouseXPos > 200 && mouseXPos < 400 && mouseYPos > 400)
{
x1.setStartX(250);
x1.setStartY(450);
x1.setEndX(350);
x1.setEndY(550);
x2.setStartX(350);
x2.setStartY(450);
x2.setEndX(250);
x2.setEndY(550);
setBM(1);
}
//bottom right
else if(mouseXPos > 400 && mouseYPos > 400)
{
x1.setStartX(450);
x1.setStartY(450);
x1.setEndX(550);
x1.setEndY(550);
x2.setStartX(550);
x2.setStartY(450);
x2.setEndX(450);
x2.setEndY(550);
setBR(1);
}
getChildren().addAll(x1,x2);
}
}
public class drawO extends Pane
{
public drawO (double mouseXPos, double mouseYPos)
{
Circle circle = new Circle(75);
//Top Left
if(mouseXPos < 200 && mouseYPos < 200)
{
circle.setCenterX(100);
circle.setCenterY(100);
}
//Top middle
else if (mouseXPos > 200 && mouseXPos < 400 && mouseYPos < 200 )
{
circle.setCenterX(300);
circle.setCenterY(100);
}
//Top right
else if (mouseXPos > 400 && mouseYPos < 200)
{
circle.setCenterX(500);
circle.setCenterY(100);
}
//Middle left
else if (mouseXPos < 200 && mouseYPos > 200 && mouseYPos < 400)
{
circle.setCenterX(100);
circle.setCenterY(300);
}
//middle
else if (mouseXPos > 200 && mouseXPos < 400 &mouseYPos > 200 && mouseYPos < 400)
{
circle.setCenterX(300);
circle.setCenterY(300);
}
//middle right
else if (mouseXPos > 400 && mouseYPos > 200 && mouseYPos < 400)
{
circle.setCenterX(500);
circle.setCenterY(300);
}
//bottom left
else if (mouseXPos < 200 && mouseYPos > 400)
{
circle.setCenterX(100);
circle.setCenterY(500);
}
//bottom middle
else if (mouseXPos > 200 && mouseXPos < 400 && mouseYPos > 400)
{
circle.setCenterX(300);
circle.setCenterY(500);
}
//bottom right
else if(mouseXPos > 400 && mouseYPos > 400)
{
circle.setCenterX(500);
circle.setCenterY(500);
}
getChildren().addAll(circle);
}
}
}
为什么DrawBoard
可以扩展Run? 这对我来说毫无意义。 去掉它。
也有DrawBoard board;
作为Run
成员。
您甚至不需要DrawMainBoard
类,它也应该是DrawBoard
的简单功能:
public Pane drawMainBoard()
{ ... your code here ... }
还有一点,因为这个类嵌套真的很奇怪:
有这样一个public class drawO extends Pane
的背后是什么?
此方法可以简单地是DrawBoard
方法:
public Pane drawO(double mouseXPos, double mouseYPos)
对于drawX
。
这种方式:Run具有成员drawBoard
,您可以将主板添加为: root.getChildren().addAll(drawBoard.drawMainBoard(),characters);
您可以像drawBoard.drawO(x,y);
一样绘制X和O。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.