2016-05-04 71 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的,使用標籤並將標籤文本設置爲文本變量,然後將其添加到您的窗格