2016-05-04 68 views
0

我写,显示,具有以下质量的井字棋板井字游戏程序创建井字游戏:JavaFX中

  • 细胞可以是X,O,或为空。
  • 在程序启动时随机决定在每个单元中显示的内容。
  • 输入X和O作为文本而不是图像。

在我的程序中,我有X和O的.gif文件。我想知道代码在将X和O作为文本而不是图像放置时的外观如何?提前致谢!

这里是我的代码:

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.text.Text; 
import javafx.scene.layout.GridPane; 
import javafx.stage.Stage; 

public class TicTacToe extends Application { 

    @Override 
    public void start(Stage primaryStage) { 

     GridPane pane = new GridPane(); 
     pane.setAlignment(Pos.CENTER); 
     for (int i = 0; i < 3; i++) { 
      for (int j = 0; j < 3; j++) { 
       int random = (int)(Math.random() * 3); 
       if (random != 2) { 
        String text = (random > 0) ? "/image/x.gif" : "/image/o.gif"; 
        pane.add(new ImageView(new Image(image)), j, i); 
       } 
      } 
     } 
     Scene scene = new Scene(pane, 150, 150); 
     primaryStage.setTitle("Tic Tac Toe"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 

     Application.launch(args); 

    } 

} 

    // Create a scene and place it in the stage 
    Scene scene = new Scene(pane); 
    primaryStage.setTitle("TicTacToe"); // Set the stage title 
    primaryStage.setScene(scene); // Place the scene in the stage 
    primaryStage.show(); // Display the stage 
    } 

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

回答

0

替换 “/image/x.gif” 与 “X” 和 “/image/o.gif” 与 “O” 和而不是使用ImageView的,使用标签并将标签文本设置为文本变量,然后将其添加到您的窗格