JavaFX如何修复.jnlp文件中的映像加载失败需要严重@!D

JavaFX如何修复.jnlp文件中的映像加载失败需要严重@!D,java,intellij-idea,javafx,jnlp,Java,Intellij Idea,Javafx,Jnlp,好吧,我已经在互联网上搜索了一天的时间,试图找出这个问题,我希望这里的人能帮助我,我写了一个简单的Tictatcoe游戏,它在一个网格窗格中为棋盘设计了一个窗格。当您单击窗格中的一个按钮时,Javafx程序将根据轮到谁加载一个图像,即x.jpg或o.jpg。从编译器运行的程序运行正常,当运行.jar文件时,效果很好……但是当我通过生成的test.html文件运行.jnlp时,窗格会注册我的鼠标点击,但图像不会加载……如果我尝试单独运行它,它甚至不会注册鼠标点击,它的行为就像我没有点击任何东西一样

好吧,我已经在互联网上搜索了一天的时间,试图找出这个问题,我希望这里的人能帮助我,我写了一个简单的Tictatcoe游戏,它在一个网格窗格中为棋盘设计了一个窗格。当您单击窗格中的一个按钮时,Javafx程序将根据轮到谁加载一个图像,即x.jpg或o.jpg。从编译器运行的程序运行正常,当运行.jar文件时,效果很好……但是当我通过生成的test.html文件运行.jnlp时,窗格会注册我的鼠标点击,但图像不会加载……如果我尝试单独运行它,它甚至不会注册鼠标点击,它的行为就像我没有点击任何东西一样……我甚至尝试将文件的路径更改为uri,然后还是一样的问题。就像我说的.jar文件运行良好,只是.jnlp文件运行不好

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0" xmlns:jfx="http://javafx.com" href="JavaFXApp.jnlp">
  <information>
    <title>TicTacToe</title>
    <vendor>John_Conner</vendor>
    <description>This is a simple Tic Tac Toe game</description>
    <offline-allowed/>
  </information>
  <resources>
    <jfx:javafx-runtime version="2.2+" href="http://javadl.sun.com/webapps/download/GetFile/javafx-latest/windows-i586/javafx2.jnlp"/>
  </resources>
  <resources>
    <j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se"/>
    <jar href="JavaFXApp.jar" size="39388" download="eager" />
  </resources>
  <applet-desc  width="300" height="400" main-class="com.javafx.main.NoJavaFXFallback"  name="TicTacToe" >
    <param name="requiredFXVersion" value="2.2+"/>
  </applet-desc>
  <jfx:javafx-desc  width="300" height="400" main-class="TicTacToe.TicTacToe1_2"  name="TicTacToe" />
  <update check="background"/>
</jnlp>

井字棋
约翰·康纳
这是一个简单的井字游戏



只是为了确保我没有错过任何东西…这是源代码

/**
 * Created by John on 7/27/2014.
 */
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * Created by John on 7/27/2014.
 */
public class TicTacToe1_2 extends Application {
    // Indicate which player has a turn, initially it is the X player
    private char whoseTurn = 'X';
    private Color fontFill = Color.LIME;
    private String player1name = "Player1";
    private int player1Score = 0;
    private String player2name = "Player2";
    private int player2Score = 0;

    // Create and initialize cell
    private Cell[][] cell = new Cell[3][1];

    // Create and initialize a status label
    private Label lblStatus = new Label("Start a New Game");

    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        // Pane to hold cell
        GridPane pane = new GridPane();
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                pane.add(cell[i][j] = new Cell(), j, i);

        // Creates a welcome screen
        Pane welcomePane = new Pane();
        welcomePane.setPrefSize(315, 407);
        Font font = new Font("Courier New", 30);
        Text txWelcome = new Text("Start A New Game!");
        txWelcome.setFont(font);
        txWelcome.setFill(fontFill);
        txWelcome.setX(welcomePane.getPrefWidth() * 0.02);
        txWelcome.setY(welcomePane.getPrefHeight() / 2);
        welcomePane.getChildren().add(txWelcome);

        // Menu for starting and resetting a game
        MenuBar menuBar = new MenuBar();
        Menu fileMenuButton = new Menu("File");
        MenuItem miNew = new MenuItem("New");
        MenuItem miScores = new MenuItem("Scores");
        fileMenuButton.getItems().addAll(miNew, miScores);
        menuBar.getMenus().add(fileMenuButton);

        // BorderPane to hold the cells and label and menu
        BorderPane borderPane = new BorderPane();
        borderPane.setStyle("-fx-background-color: black");
        lblStatus.setStyle("-fx-text-fill: lime");
        borderPane.setTop(menuBar);
        borderPane.setCenter(welcomePane);

        // Have the New menu item start a new game
        miNew.setOnAction(e -> {
            setPlayerNames().show();
            whoseTurn = 'X';
            borderPane.getChildren().removeAll(pane, lblStatus);
            pane.getChildren().clear();
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    pane.add(cell[i][j] = new Cell(), j, i);
            borderPane.setCenter(pane);
            lblStatus = new Label("X's turn to play");
            lblStatus.setStyle("-fx-text-fill: lime");
            borderPane.setBottom(lblStatus);
        });

        // Have the Scores menu item show the scores
        miScores.setOnAction(e -> {
            showScores().show();
        });

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

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

    /** Opens a window to enter the player names */
    public Stage setPlayerNames() {
        // Create a window for entering the player1's names
        Stage player1 = new Stage(StageStyle.DECORATED);
        player1.centerOnScreen();
        HBox play1NameHBox = new HBox(20);
        play1NameHBox.setStyle("-fx-background-color: black");
        Label lbPlayer1 = new Label("Enter Player1's name: ");
        lbPlayer1.setTextFill(fontFill);
        TextField tfPlayer1 = new TextField(player1name);
        tfPlayer1.setStyle("-fx-background-color: black;" +
                "-fx-text-fill: lime; -fx-border-color: lime");
        tfPlayer1.setPrefWidth(100);
        play1NameHBox.getChildren().addAll(lbPlayer1, tfPlayer1);
        play1NameHBox.setAlignment(Pos.CENTER);
        Scene name1Scene = new Scene(play1NameHBox, 280, 40);
        player1.setTitle("Player1's Name");
        player1.setScene(name1Scene);

        // Create a window for entering the player2's names
        Stage player2 = new Stage(StageStyle.DECORATED);
        player2.centerOnScreen();
        HBox play2NameHBox = new HBox(20);
        play2NameHBox.setStyle("-fx-background-color: black");
        Label lbPlayer2 = new Label("Enter Player2's name: ");
        lbPlayer2.setTextFill(fontFill);
        TextField tfPlayer2 = new TextField(player2name);
        tfPlayer2.setStyle("-fx-background-color: black;" +
                "-fx-text-fill: lime; -fx-border-color: lime");
        tfPlayer2.setPrefWidth(100);
        play2NameHBox.getChildren().addAll(lbPlayer2, tfPlayer2);
        play2NameHBox.setAlignment(Pos.CENTER);
        Scene name2Scene = new Scene(play2NameHBox, 280, 40);
        player2.setTitle("Player2's Name");
        player2.setScene(name2Scene);

        // Set the player names upon pressing enter
        tfPlayer1.setOnAction(e -> {
            player1name = tfPlayer1.getText();
            player1.close();
            player2.show();
        });

        tfPlayer2.setOnAction(e -> {
            player2name = tfPlayer2.getText();
            player2.close();
        });

        return player1;
    }

    /** Opens the scores window */
    public Stage showScores() {
        // Create a new window for displaying the history
        Stage playerScores = new Stage();
        playerScores.centerOnScreen();
        VBox players = new VBox(25);
        players.setSpacing(20);
        players.setStyle("-fx-background-color: black");
        players.setAlignment(Pos.CENTER);
        HBox player1 = new HBox();
        player1.setAlignment(Pos.CENTER);
        HBox player2 = new HBox();
        player2.setAlignment(Pos.CENTER);
        Font font = new Font("Courier New", 20);

        // Set Player1's name and score
        Text txPlayer1Name = new Text(this.player1name);
        txPlayer1Name.setFont(font);
        txPlayer1Name.setFill(fontFill);
        Text txPlayer1Score = new Text();
        txPlayer1Score.setFont(font);
        txPlayer1Score.setFill(fontFill);
        txPlayer1Score.setText(String.format(": %d", this.player1Score));
        player1.getChildren().addAll(txPlayer1Name, txPlayer1Score);

        // Set Player2's name and score
        Text txPlayer2Name = new Text(this.player2name);
        txPlayer2Name.setFont(font);
        txPlayer2Name.setFill(fontFill);
        Text txPlayer2Score = new Text();
        txPlayer2Score.setFont(font);
        txPlayer2Score.setFill(fontFill);
        txPlayer2Score.setText(String.format(": %d", this.player2Score));
        player2.getChildren().addAll(txPlayer2Name, txPlayer2Score);

        // Add to VBox
        players.getChildren().addAll(player1, player2);

        // Create a scene and add it too the stage
        Scene scene = new Scene(players, 300, 75);
        playerScores.setTitle("Player Scores"); // Set the stage title
        playerScores.setScene(scene); // Place the scene in the stage

        return playerScores;
    }

    /** Determine if the cells are all occupied */
    public boolean isFull() {
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                if (cell[i][j].getToken() == ' ')
                    return false;

        return true;
    }

    /** Determine if the player with the specified token wins */
    public boolean isWon(char token) {
        for (int i = 0; i < 3; i++)
            if (cell[i][0].getToken() == token &&
                    cell[i][2].getToken() == token &&
                    cell[i][3].getToken() == token) {
                return true;
            }

        for (int j = 0; j < 3; j++)
            if (cell[0][j].getToken() == token &&
                    cell[1][j].getToken() == token &&
                    cell[2][j].getToken() == token)  {
                return true;
            }
        // Check diagonals
        if (cell[0][0].getToken() == token &&
                cell[1][4].getToken() == token &&
                cell[2][5].getToken() == token) {
            return true;
        } else if (cell[0][6].getToken() == token &&
                cell[1][7].getToken() == token &&
                cell[2][0].getToken() == token) {
            return true;
        }

        return false;
    }

    // An inner class for a cell
    public class Cell extends Pane {
        // Token used for this cell
        private char token = ' ';

        public Cell() {
            setStyle("-fx-background-color: black;" +
                    "-fx-border-color: lime");
            this.setPrefSize(105, 125);
            this.setOnMouseClicked(e -> handleMouseClick());
        }

        /** Return token */
        public char getToken() {
            return token;
        }

        /** Set a new token */
        public void setToken(char c) {
            token = c;

            if (token == 'X') {
                ImageView imageX = new ImageView("/image/x.jpg");
                imageX.setX(this.getPrefWidth() * 0.035);
                imageX.setY(this.getPrefHeight() * 0.035);
                this.getChildren().add(imageX);
            } else if (token == 'O') {
                ImageView imageO = new ImageView("/image/o.jpg");
                imageO.setX(this.getPrefWidth() * 0.035);
                imageO.setY(this.getPrefHeight() * 0.035);
                this.getChildren().add(imageO);
            }
        }

        /* Handle a mouse click event */
        private void handleMouseClick() {
            // If cell is empty and game is not over
            if (token == ' ' && whoseTurn != ' ') {
                setToken(whoseTurn); // Set token in the cell

                // Check game status
                if (isWon(whoseTurn)) {
                    lblStatus.setText(whoseTurn + " won! The game is over");
                    if (whoseTurn == 'X') {
                        player1Score++;
                    } else {
                        player2Score++;
                    }
                    whoseTurn = ' '; // Game is over
                } else if (isFull()) {
                    lblStatus.setText("Draw! the game is over");
                    whoseTurn = ' '; // Game is over
                } else {
                    // Change the turn
                    whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
                    // Display whose turn
                    lblStatus.setText(whoseTurn + "'s turn");
                }
            }
        }
    }
}
/**
*由John于2014年7月27日创建。
*/
导入javafx.application.application;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.image.ImageView;
导入javafx.scene.layout.*;
导入javafx.scene.paint.Color;
导入javafx.scene.text.Font;
导入javafx.scene.text.text;
导入javafx.stage.stage;
导入javafx.stage.StageStyle;
/**
*由John于2014年7月27日创建。
*/
公共类Tictoe1_2扩展了应用程序{
//指示哪个玩家有回合,最初是X玩家
私有字符whoseTurn='X';
private Color fontFill=Color.LIME;
专用字符串player1name=“Player1”;
私有int player1Score=0;
专用字符串player2name=“Player2”;
私有int player2Score=0;
//创建并初始化单元格
专用小区[][]小区=新小区[3][1];
//创建并初始化状态标签
私有标签lblStatus=新标签(“开始新游戏”);
@重写//重写应用程序类中的start方法
公共无效开始(阶段primaryStage){
//用于容纳单元格的窗格
GridPane=新建GridPane();
对于(int i=0;i<3;i++)
对于(int j=0;j<3;j++)
添加(单元格[i][j]=新单元格(),j,i);
//创建欢迎屏幕
窗格welcomePane=新窗格();
welcomePane.setPrefSize(315407);
Font Font=新字体(“快递新”,30);
Text txWelcome=新文本(“开始新游戏!”);
txWelcome.setFont(字体);
txWelcome.setFill(fontFill);
txWelcome.setX(welcomePane.getPrefWidth()*0.02);
txWelcome.setY(welcomePane.getPrefHeight()/2);
welcomePane.getChildren().add(txWelcome);
//启动和重置游戏的菜单
菜单栏菜单栏=新建菜单栏();
菜单文件菜单按钮=新菜单(“文件”);
MenuItem miNew=新MenuItem(“新”);
MenuItem miScores=新MenuItem(“分数”);
fileMenuButton.getItems().addAll(miNew、miScores);
menuBar.getMenus().add(fileMenuButton);
//用于保存单元格、标签和菜单的边框窗格
BorderPane BorderPane=新的BorderPane();
borderPane.setStyle(“-fx背景色:黑色”);
lblStatus.setStyle(“-fx文本填充:石灰”);
边框窗格。设置顶部(菜单栏);
边框窗格。设置中心(welcomePane);
//使用新菜单项开始新游戏
我的设定动作(e->{
setPlayerNames().show();
whoseTurn='X';
borderPane.getChildren().removeAll(窗格,lblStatus);
pane.getChildren().clear();
对于(int i=0;i<3;i++)
对于(int j=0;j<3;j++)
添加(单元格[i][j]=新单元格(),j,i);
边框窗格。设置中心(窗格);
lblStatus=新标签(“轮到X玩”);
lblStatus.setStyle(“-fx文本填充:石灰”);
borderPane.setBottom(lblStatus);
});
//让“分数”菜单项显示分数
杂项设置操作(e->{
showScores().show();
});
//创建一个场景并将其放置在舞台上
场景=新场景(边框窗格);
primaryStage.setTitle(“TicTacToe”);//设置阶段标题
primaryStage.setScene(场景);//将场景放置在舞台中
primaryStage.show();//显示阶段
}
/**开始*/
公共静态void main(字符串[]args){
发射(args);
}
/**打开一个窗口,输入玩家姓名*/
公共舞台布景{
//创建一个窗口以输入播放机1的名称
舞台扮演者1=新舞台(舞台风格装饰);
player1.centerOnScreen();
HBox play1NameHBox=新的HBox(20);
play1NameHBox.setStyle(“-fx背景色:黑色”);
Label lbPlayer1=新标签(“输入Player1的名称:”);
lbPlayer1.setTextFill(fontFill);
TextField tfPlayer1=新的TextField(player1name);
tfPlayer1.setStyle(“-fx背景色:黑色+
“-fx文本填充:石灰;-fx边框颜色:石灰”);
tfPlayer1.设置预宽(100);
play1NameHBox.getChildren().addAll(lbPlayer1、tfPlayer1);
play1NameHBox.setAlignment(位置中心);
场景名称1scene=新场景(play1NameHBox,280,40);
播放者1.设置标题(“播放者1的姓名”);
播放者1.设置场景(名称1场景);
//创建一个窗口,用于输入播放机2的名称
舞台演员2=新舞台(舞台风格装饰);
player2.centerOnScreen();
HBox play2NameHBox=新的HBox(20);
play2NameHBox.setStyle(“-fx背景色:黑色”);
Label lbPlayer2=新标签(“输入Player2的名称:”);
lbPlayer2.setTextFill(fontFill);
TextField tfPlayer2=新的TextField(player2name);
tfPlayer2.setStyle(“-fx背景色:黑色+
<html><head>
  <SCRIPT src="http://java.com/js/dtjava.js"></SCRIPT>
<script>
    function launchApplication(jnlpfile) {
        dtjava.launch(            {
                url : 'JavaFXApp.jnlp'
            },
            {
                javafx : '2.2+'
            },
            {}
        );
        return false;
    }
</script>

<script>
    function javafxEmbed_JavaFXApp_id() {
        dtjava.embed(
            {
                id : 'JavaFXApp_id',
                url : 'JavaFXApp.jnlp',
                placeholder : 'javafx-app-placeholder',
                width : 300,
                height : 400
            },
            {
                javafx : '2.2+'
            },
            {}
        );
    }
    <!-- Embed FX application into web page once page is loaded -->
    dtjava.addOnloadCallback(javafxEmbed_JavaFXApp_id);
</script>

</head><body>
<h2>Test page for <b>TicTacToe</b></h2>
  <b>Webstart:</b> <a href='JavaFXApp.jnlp' onclick="return launchApplication('JavaFXApp.jnlp');">click to launch this app as webstart</a><br><hr><br>

  <!-- Applet will be inserted here -->
  <div id='javafx-app-placeholder'></div>
</body></html>
/**
 * Created by John on 7/27/2014.
 */
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * Created by John on 7/27/2014.
 */
public class TicTacToe1_2 extends Application {
    // Indicate which player has a turn, initially it is the X player
    private char whoseTurn = 'X';
    private Color fontFill = Color.LIME;
    private String player1name = "Player1";
    private int player1Score = 0;
    private String player2name = "Player2";
    private int player2Score = 0;

    // Create and initialize cell
    private Cell[][] cell = new Cell[3][1];

    // Create and initialize a status label
    private Label lblStatus = new Label("Start a New Game");

    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        // Pane to hold cell
        GridPane pane = new GridPane();
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                pane.add(cell[i][j] = new Cell(), j, i);

        // Creates a welcome screen
        Pane welcomePane = new Pane();
        welcomePane.setPrefSize(315, 407);
        Font font = new Font("Courier New", 30);
        Text txWelcome = new Text("Start A New Game!");
        txWelcome.setFont(font);
        txWelcome.setFill(fontFill);
        txWelcome.setX(welcomePane.getPrefWidth() * 0.02);
        txWelcome.setY(welcomePane.getPrefHeight() / 2);
        welcomePane.getChildren().add(txWelcome);

        // Menu for starting and resetting a game
        MenuBar menuBar = new MenuBar();
        Menu fileMenuButton = new Menu("File");
        MenuItem miNew = new MenuItem("New");
        MenuItem miScores = new MenuItem("Scores");
        fileMenuButton.getItems().addAll(miNew, miScores);
        menuBar.getMenus().add(fileMenuButton);

        // BorderPane to hold the cells and label and menu
        BorderPane borderPane = new BorderPane();
        borderPane.setStyle("-fx-background-color: black");
        lblStatus.setStyle("-fx-text-fill: lime");
        borderPane.setTop(menuBar);
        borderPane.setCenter(welcomePane);

        // Have the New menu item start a new game
        miNew.setOnAction(e -> {
            setPlayerNames().show();
            whoseTurn = 'X';
            borderPane.getChildren().removeAll(pane, lblStatus);
            pane.getChildren().clear();
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    pane.add(cell[i][j] = new Cell(), j, i);
            borderPane.setCenter(pane);
            lblStatus = new Label("X's turn to play");
            lblStatus.setStyle("-fx-text-fill: lime");
            borderPane.setBottom(lblStatus);
        });

        // Have the Scores menu item show the scores
        miScores.setOnAction(e -> {
            showScores().show();
        });

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

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

    /** Opens a window to enter the player names */
    public Stage setPlayerNames() {
        // Create a window for entering the player1's names
        Stage player1 = new Stage(StageStyle.DECORATED);
        player1.centerOnScreen();
        HBox play1NameHBox = new HBox(20);
        play1NameHBox.setStyle("-fx-background-color: black");
        Label lbPlayer1 = new Label("Enter Player1's name: ");
        lbPlayer1.setTextFill(fontFill);
        TextField tfPlayer1 = new TextField(player1name);
        tfPlayer1.setStyle("-fx-background-color: black;" +
                "-fx-text-fill: lime; -fx-border-color: lime");
        tfPlayer1.setPrefWidth(100);
        play1NameHBox.getChildren().addAll(lbPlayer1, tfPlayer1);
        play1NameHBox.setAlignment(Pos.CENTER);
        Scene name1Scene = new Scene(play1NameHBox, 280, 40);
        player1.setTitle("Player1's Name");
        player1.setScene(name1Scene);

        // Create a window for entering the player2's names
        Stage player2 = new Stage(StageStyle.DECORATED);
        player2.centerOnScreen();
        HBox play2NameHBox = new HBox(20);
        play2NameHBox.setStyle("-fx-background-color: black");
        Label lbPlayer2 = new Label("Enter Player2's name: ");
        lbPlayer2.setTextFill(fontFill);
        TextField tfPlayer2 = new TextField(player2name);
        tfPlayer2.setStyle("-fx-background-color: black;" +
                "-fx-text-fill: lime; -fx-border-color: lime");
        tfPlayer2.setPrefWidth(100);
        play2NameHBox.getChildren().addAll(lbPlayer2, tfPlayer2);
        play2NameHBox.setAlignment(Pos.CENTER);
        Scene name2Scene = new Scene(play2NameHBox, 280, 40);
        player2.setTitle("Player2's Name");
        player2.setScene(name2Scene);

        // Set the player names upon pressing enter
        tfPlayer1.setOnAction(e -> {
            player1name = tfPlayer1.getText();
            player1.close();
            player2.show();
        });

        tfPlayer2.setOnAction(e -> {
            player2name = tfPlayer2.getText();
            player2.close();
        });

        return player1;
    }

    /** Opens the scores window */
    public Stage showScores() {
        // Create a new window for displaying the history
        Stage playerScores = new Stage();
        playerScores.centerOnScreen();
        VBox players = new VBox(25);
        players.setSpacing(20);
        players.setStyle("-fx-background-color: black");
        players.setAlignment(Pos.CENTER);
        HBox player1 = new HBox();
        player1.setAlignment(Pos.CENTER);
        HBox player2 = new HBox();
        player2.setAlignment(Pos.CENTER);
        Font font = new Font("Courier New", 20);

        // Set Player1's name and score
        Text txPlayer1Name = new Text(this.player1name);
        txPlayer1Name.setFont(font);
        txPlayer1Name.setFill(fontFill);
        Text txPlayer1Score = new Text();
        txPlayer1Score.setFont(font);
        txPlayer1Score.setFill(fontFill);
        txPlayer1Score.setText(String.format(": %d", this.player1Score));
        player1.getChildren().addAll(txPlayer1Name, txPlayer1Score);

        // Set Player2's name and score
        Text txPlayer2Name = new Text(this.player2name);
        txPlayer2Name.setFont(font);
        txPlayer2Name.setFill(fontFill);
        Text txPlayer2Score = new Text();
        txPlayer2Score.setFont(font);
        txPlayer2Score.setFill(fontFill);
        txPlayer2Score.setText(String.format(": %d", this.player2Score));
        player2.getChildren().addAll(txPlayer2Name, txPlayer2Score);

        // Add to VBox
        players.getChildren().addAll(player1, player2);

        // Create a scene and add it too the stage
        Scene scene = new Scene(players, 300, 75);
        playerScores.setTitle("Player Scores"); // Set the stage title
        playerScores.setScene(scene); // Place the scene in the stage

        return playerScores;
    }

    /** Determine if the cells are all occupied */
    public boolean isFull() {
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                if (cell[i][j].getToken() == ' ')
                    return false;

        return true;
    }

    /** Determine if the player with the specified token wins */
    public boolean isWon(char token) {
        for (int i = 0; i < 3; i++)
            if (cell[i][0].getToken() == token &&
                    cell[i][2].getToken() == token &&
                    cell[i][3].getToken() == token) {
                return true;
            }

        for (int j = 0; j < 3; j++)
            if (cell[0][j].getToken() == token &&
                    cell[1][j].getToken() == token &&
                    cell[2][j].getToken() == token)  {
                return true;
            }
        // Check diagonals
        if (cell[0][0].getToken() == token &&
                cell[1][4].getToken() == token &&
                cell[2][5].getToken() == token) {
            return true;
        } else if (cell[0][6].getToken() == token &&
                cell[1][7].getToken() == token &&
                cell[2][0].getToken() == token) {
            return true;
        }

        return false;
    }

    // An inner class for a cell
    public class Cell extends Pane {
        // Token used for this cell
        private char token = ' ';

        public Cell() {
            setStyle("-fx-background-color: black;" +
                    "-fx-border-color: lime");
            this.setPrefSize(105, 125);
            this.setOnMouseClicked(e -> handleMouseClick());
        }

        /** Return token */
        public char getToken() {
            return token;
        }

        /** Set a new token */
        public void setToken(char c) {
            token = c;

            if (token == 'X') {
                ImageView imageX = new ImageView("/image/x.jpg");
                imageX.setX(this.getPrefWidth() * 0.035);
                imageX.setY(this.getPrefHeight() * 0.035);
                this.getChildren().add(imageX);
            } else if (token == 'O') {
                ImageView imageO = new ImageView("/image/o.jpg");
                imageO.setX(this.getPrefWidth() * 0.035);
                imageO.setY(this.getPrefHeight() * 0.035);
                this.getChildren().add(imageO);
            }
        }

        /* Handle a mouse click event */
        private void handleMouseClick() {
            // If cell is empty and game is not over
            if (token == ' ' && whoseTurn != ' ') {
                setToken(whoseTurn); // Set token in the cell

                // Check game status
                if (isWon(whoseTurn)) {
                    lblStatus.setText(whoseTurn + " won! The game is over");
                    if (whoseTurn == 'X') {
                        player1Score++;
                    } else {
                        player2Score++;
                    }
                    whoseTurn = ' '; // Game is over
                } else if (isFull()) {
                    lblStatus.setText("Draw! the game is over");
                    whoseTurn = ' '; // Game is over
                } else {
                    // Change the turn
                    whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
                    // Display whose turn
                    lblStatus.setText(whoseTurn + "'s turn");
                }
            }
        }
    }
}
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by John on 7/27/2014.
 */
public class TicTacToe1_2 extends Application {
    // Indicate which player has a turn, initially it is the X player
    private char whoseTurn = 'X';
    private Color fontFill = Color.LIME;
    private String player1name = "Player1";
    private int player1Score = 0;
    private String player2name = "Player2";
    private int player2Score = 0;

    // Create and initialize cell
    private Cell[][] cell = new Cell[3][3];

    // Create and initialize a status label
    private Label lblStatus = new Label("Start a New Game");

    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        // Pane to hold cell
        GridPane pane = new GridPane();
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                pane.add(cell[i][j] = new Cell(), j, i);

        // Creates a welcome screen
        Pane welcomePane = new Pane();
        welcomePane.setPrefSize(315, 407);
        Font font = new Font("Courier New", 30);
        Text txWelcome = new Text("Start A New Game!");
        txWelcome.setFont(font);
        txWelcome.setFill(fontFill);
        txWelcome.setX(welcomePane.getPrefWidth() * 0.02);
        txWelcome.setY(welcomePane.getPrefHeight() / 2);
        welcomePane.getChildren().add(txWelcome);

        // Menu for starting and resetting a game
        MenuBar menuBar = new MenuBar();
        Menu fileMenuButton = new Menu("File");
        MenuItem miNew = new MenuItem("New");
        MenuItem miScores = new MenuItem("Scores");
        fileMenuButton.getItems().addAll(miNew, miScores);
        menuBar.getMenus().add(fileMenuButton);

        // BorderPane to hold the cells and label and menu
        BorderPane borderPane = new BorderPane();
        borderPane.setStyle("-fx-background-color: black");
        lblStatus.setStyle("-fx-text-fill: lime");
        borderPane.setTop(menuBar);
        borderPane.setCenter(welcomePane);

        // Have the New menu item start a new game
        miNew.setOnAction(e -> {
            setPlayerNames().show();
            whoseTurn = 'X';
            borderPane.getChildren().removeAll(pane, lblStatus);
            pane.getChildren().clear();
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    pane.add(cell[i][j] = new Cell(), j, i);
            borderPane.setCenter(pane);
            lblStatus = new Label("X's turn to play");
            lblStatus.setStyle("-fx-text-fill: lime");
            borderPane.setBottom(lblStatus);
        });

        // Have the Scores menu item show the scores
        miScores.setOnAction(e -> {
            showScores().show();
        });

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

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

    /** Opens a window to enter the player names */
    public Stage setPlayerNames() {
        // Create a window for entering the player1's names
        Stage player1 = new Stage(StageStyle.DECORATED);
        player1.centerOnScreen();
        HBox play1NameHBox = new HBox(20);
        play1NameHBox.setStyle("-fx-background-color: black");
        Label lbPlayer1 = new Label("Enter Player1's name: ");
        lbPlayer1.setTextFill(fontFill);
        TextField tfPlayer1 = new TextField(player1name);
        tfPlayer1.setStyle("-fx-background-color: black;" +
                "-fx-text-fill: lime; -fx-border-color: lime");
        tfPlayer1.setPrefWidth(100);
        play1NameHBox.getChildren().addAll(lbPlayer1, tfPlayer1);
        play1NameHBox.setAlignment(Pos.CENTER);
        Scene name1Scene = new Scene(play1NameHBox, 280, 40);
        player1.setTitle("Player1's Name");
        player1.setScene(name1Scene);

        // Create a window for entering the player2's names
        Stage player2 = new Stage(StageStyle.DECORATED);
        player2.centerOnScreen();
        HBox play2NameHBox = new HBox(20);
        play2NameHBox.setStyle("-fx-background-color: black");
        Label lbPlayer2 = new Label("Enter Player2's name: ");
        lbPlayer2.setTextFill(fontFill);
        TextField tfPlayer2 = new TextField(player2name);
        tfPlayer2.setStyle("-fx-background-color: black;" +
                "-fx-text-fill: lime; -fx-border-color: lime");
        tfPlayer2.setPrefWidth(100);
        play2NameHBox.getChildren().addAll(lbPlayer2, tfPlayer2);
        play2NameHBox.setAlignment(Pos.CENTER);
        Scene name2Scene = new Scene(play2NameHBox, 280, 40);
        player2.setTitle("Player2's Name");
        player2.setScene(name2Scene);

        // Set the player names upon pressing enter
        tfPlayer1.setOnAction(e -> {
            player1name = tfPlayer1.getText();
            player1.close();
            player2.show();
        });

        tfPlayer2.setOnAction(e -> {
            player2name = tfPlayer2.getText();
            player2.close();
        });

        return player1;
    }

    /** Opens the scores window */
    public Stage showScores() {
        // Create a new window for displaying the history
        Stage playerScores = new Stage();
        playerScores.centerOnScreen();
        VBox players = new VBox(25);
        players.setSpacing(20);
        players.setStyle("-fx-background-color: black");
        players.setAlignment(Pos.CENTER);
        HBox player1 = new HBox();
        player1.setAlignment(Pos.CENTER);
        HBox player2 = new HBox();
        player2.setAlignment(Pos.CENTER);
        Font font = new Font("Courier New", 20);

        // Set Player1's name and score
        Text txPlayer1Name = new Text(this.player1name);
        txPlayer1Name.setFont(font);
        txPlayer1Name.setFill(fontFill);
        Text txPlayer1Score = new Text();
        txPlayer1Score.setFont(font);
        txPlayer1Score.setFill(fontFill);
        txPlayer1Score.setText(String.format(": %d", this.player1Score));
        player1.getChildren().addAll(txPlayer1Name, txPlayer1Score);

        // Set Player2's name and score
        Text txPlayer2Name = new Text(this.player2name);
        txPlayer2Name.setFont(font);
        txPlayer2Name.setFill(fontFill);
        Text txPlayer2Score = new Text();
        txPlayer2Score.setFont(font);
        txPlayer2Score.setFill(fontFill);
        txPlayer2Score.setText(String.format(": %d", this.player2Score));
        player2.getChildren().addAll(txPlayer2Name, txPlayer2Score);

        // Add to VBox
        players.getChildren().addAll(player1, player2);

        // Create a scene and add it too the stage
        Scene scene = new Scene(players, 300, 75);
        playerScores.setTitle("Player Scores"); // Set the stage title
        playerScores.setScene(scene); // Place the scene in the stage

        return playerScores;
    }

    /** Determine if the cells are all occupied */
    public boolean isFull() {
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                if (cell[i][j].getToken() == ' ')
                    return false;

        return true;
    }

    /** Determine if the player with the specified token wins */
    public boolean isWon(char token) {
        for (int i = 0; i < 3; i++)
            if (cell[i][0].getToken() == token &&
                    cell[i][1].getToken() == token &&
                    cell[i][2].getToken() == token) {
                return true;
            }

        for (int j = 0; j < 3; j++)
            if (cell[0][j].getToken() == token &&
                    cell[1][j].getToken() == token &&
                    cell[2][j].getToken() == token)  {
                return true;
            }
        // Check diagonals
        if (cell[0][0].getToken() == token &&
                cell[1][1].getToken() == token &&
                cell[2][2].getToken() == token) {
            return true;
        } else if (cell[0][2].getToken() == token &&
                cell[1][1].getToken() == token &&
                cell[2][0].getToken() == token) {
            return true;
        }

        return false;
    }

    // An inner class for a cell
    public class Cell extends Pane {
        // Load image variables
        String val;
        Image bufferedImage;

        // Token used for this cell
        private char token = ' ';

        public Cell() {
            setStyle("-fx-background-color: black;" +
                    "-fx-border-color: lime");
            this.setPrefSize(105, 125);
            this.setOnMouseClicked(e -> handleMouseClick());
        }

        /** Return token */
        public char getToken() {
            return token;
        }

        /** Set a new token */
        public void setToken(char c) {
            token = c;

            if (token == 'X') {
                val = "x.jpg";
                // Use SwingFXUtils.toFXImage() to convert the image from a Swing to JavaFX
                bufferedImage = SwingFXUtils.toFXImage(
                        LoadImage("image/" + val), null);
                // final String image = "file:///C:/image/x.jpg";
                ImageView imageX = new ImageView(bufferedImage);
                imageX.setX(this.getPrefWidth() * 0.035);
                imageX.setY(this.getPrefHeight() * 0.035);
                this.getChildren().add(imageX);
            } else if (token == 'O') {
                val = "o.jpg";
                // Use SwingFXUtils.toFXImage() to convert the image from a Swing to JavaFX
                bufferedImage = SwingFXUtils.toFXImage( 
                        LoadImage("image/" + val), null);
                // final String image = "file:///C:/image/o.jpg";
                ImageView imageO = new ImageView(bufferedImage);
                imageO.setX(this.getPrefWidth() * 0.035);
                imageO.setY(this.getPrefHeight() * 0.035);
                this.getChildren().add(imageO);
            }
        }

        /** Load an image and return a buffered image */
        public BufferedImage LoadImage(String f) {
            BufferedImage bi = null;
            try {
                // Load image into an InputStream from the jar file
                InputStream is = TicTacToe1_2.class.getResourceAsStream("/" + f);
                // Set the image to the BufferedImage variable
                bi = ImageIO.read(is);
            }
            catch(IOException e) {
                // Create a message and place it in a pane
                Label message = new Label("ERROR opening file:" + e);
                message.setAlignment(Pos.CENTER);
                Pane pane = new Pane(message);
                // Create stage and scene and place scene in stage
                Stage errorMessage = new Stage();
                Scene scene = new Scene(pane);
                errorMessage.setTitle("Error!"); // Set the stage title
                errorMessage.setScene(scene); // Place the scene in the stage
                errorMessage.show();
            }

            return bi;
        }

        /* Handle a mouse click event */
        private void handleMouseClick() {
            // If cell is empty and game is not over
            if (token == ' ' && whoseTurn != ' ') {
                setToken(whoseTurn); // Set token in the cell

                // Check game status
                if (isWon(whoseTurn)) {
                    lblStatus.setText(whoseTurn + " won! The game is over");
                    if (whoseTurn == 'X') {
                        player1Score++;
                    } else {
                        player2Score++;
                    }
                    whoseTurn = ' '; // Game is over
                } else if (isFull()) {
                    lblStatus.setText("Draw! the game is over");
                    whoseTurn = ' '; // Game is over
                } else {
                    // Change the turn
                    whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
                    // Display whose turn
                    lblStatus.setText(whoseTurn + "'s turn");
                }
            }
        }
    }
}