JAVA javafx窗格上有多个形状

时间:2015-11-29 20:57:07

标签: java user-interface javafx

我需要制作一个简单的tic-tac-toe板(不需要是交互式的)我已经为电路板工作了网格,但是当我去添加所需的文本时,我得到一个相当长的错误消息从我的IDE,我可以根据需要复制粘贴。这是我的代码:如果我删除第39行addText(myPane);它工作正常。我不确定为什么这不起作用,因为我在一个几乎相同的例子之后对它进行了建模。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TicTacToe extends Application {

final double WIDTH = 500;
final double HEIGHT = 500;

public static void main(String[] args) {
    Application.launch(args);
}

@Override
public void start(Stage primaryStage) {
    Pane myPane = new Pane();
    //didn't work as FlowPane or StackPane

    drawBoard(myPane);
    addText(myPane);

    Scene scene = new Scene(myPane, WIDTH, HEIGHT);
    primaryStage.setTitle("Tic-Tac-Toe!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

//Draws the Tic-tac-toe board
public void drawBoard(Pane myPane) {

    Rectangle verticalLine1 = new Rectangle();
    verticalLine1.setX(160);
    verticalLine1.setY(11);
    verticalLine1.setWidth(2);
    verticalLine1.setHeight(HEIGHT - 100);
    verticalLine1.setStroke(Color.BLACK);
    verticalLine1.setFill(Color.BLACK);

    Rectangle verticalLine2 = new Rectangle();
    verticalLine2.setX(330);
    verticalLine2.setY(11);
    verticalLine2.setWidth(2);
    verticalLine2.setHeight(HEIGHT - 100);
    verticalLine2.setStroke(Color.BLACK);
    verticalLine2.setFill(Color.BLACK);

    Rectangle horizontalLine1 = new Rectangle();
    horizontalLine1.setX(11);
    horizontalLine1.setY(130);
    horizontalLine1.setWidth(WIDTH - 30);
    horizontalLine1.setHeight(2);
    horizontalLine1.setStroke(Color.BLACK);
    horizontalLine1.setFill(Color.BLACK);

    Rectangle horizontalLine2 = new Rectangle();
    horizontalLine2.setX(11);
    horizontalLine2.setY(280);
    horizontalLine2.setWidth(WIDTH - 30);
    horizontalLine2.setHeight(2);
    horizontalLine2.setStroke(Color.BLACK);
    horizontalLine2.setFill(Color.BLACK);

    myPane.getChildren().addAll(verticalLine1, verticalLine2, horizontalLine1, horizontalLine2);
}

//Adds message to board
public void addText(Pane myPane){
    String gameMessage = "Let's Play a Game!";

    Text text2 = new Text(100,100, "Let's Play a Game!");
    myPane.getChildren().addAll(text2);
    myPane.getChildren().add(text2); //Doesn't add any text to pane?
}

}

0 个答案:

没有答案