Java fxml从表单返回值

时间:2018-12-06 00:28:07

标签: java javafx netbeans fxml

我遇到一个问题,我似乎无法使用fxml从javafx中的新表单返回值。

我正在尝试一种具有4个与Connect 4游戏相关的功能的主要形式 开始游戏,查看玩家,查看游戏,查看玩家游戏

当我启动程序时,弹出主菜单,然后我想单击“开始游戏”按钮。 打开后,它将提示您输入用户1的信息。 然后弹出一个有关玩家信息的新表格。

我遇到的问题是,单击接受按钮后,类中的变量变为空 我尝试通过实例get方法从主菜单类进行访问,并且还返回到主类。

在ShowAndWait行之后,变量似乎被清空了。 在winodow之后,我如何以c#方式检索变量信息

package Homework_5;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.util.ArrayList;

public class Connect4Window extends Application {
    ArrayList<Connect4Game> games = new ArrayList<>();

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

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("FXML_Files/MainMenu.fxml"));
        primaryStage.setTitle("Connect 4");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

MainMenu类

package Homework_5.FXML_Java_Classes;

import Homework_5.DataClasses.PersonData;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;

public class MainMenu {

    @FXML
    private Button btnViewGames;

    @FXML
    private Button btnViewPlayers;

    @FXML
    private Button btnStartGame;

    @FXML
    private Button btnViewPlayerGames;

    @FXML
    void startGame(ActionEvent event) {
        Alert alert = new Alert(Alert.AlertType.NONE, "Please enter player 1 info");
        alert.setHeaderText(null);
        alert.setTitle("Player 1 info");
        alert.showAndWait();

        PlayerInfo playerInfo = new PlayerInfo();
        playerInfo.Show();
        System.out.println(playerInfo.getTxtName()); //Shows null
        System.out.println(playerInfo.isInfoAccepted()); //Shows null
        System.out.println(playerInfo.getChkComputer()); //Shows null
        System.out.println(playerInfo.getColor()); //Shows null
    }

    @FXML
    void viewPlayers(ActionEvent event) {
        System.out.println("view players");
    }

    @FXML
    void viewPlayerGames(ActionEvent event) {
        System.out.println("view playergames");
    }

    @FXML
    void viewGames(ActionEvent event) {
        System.out.println("view games");
    }
}

玩家信息类

package Homework_5.FXML_Java_Classes;

import Homework_5.DataClasses.PersonData;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class PlayerInfo {

    @FXML
    private Button btnAccept;

    @FXML
    private ColorPicker colorColorPicker;

    @FXML
    private CheckBox chkComputer;

    @FXML
    private TextField txtName;

    @FXML
    private Button btnClear;

    private boolean infoAccepted = false;

    public PlayerInfo(){

    }

    public PersonData Show()
    {
        try{
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../FXML_Files/PlayerInfo.fxml"));
            Parent root1 = fxmlLoader.load();
            Stage stage = new Stage();
            stage.initModality(Modality.WINDOW_MODAL);
            stage.setTitle("Enter Player Information");
            stage.setScene(new Scene(root1));
            stage.showAndWait();
            return new PersonData(txtName.getText(), colorColorPicker.getValue(), chkComputer.isSelected());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @FXML
    void Accept(ActionEvent event) {
        if (txtName.getText().isEmpty()) {
            Alert alert = new Alert(Alert.AlertType.WARNING, "The name field is blank!\n\nPlease enter a name.");
            alert.setHeaderText(null);
            alert.setTitle("Name Blank");
            alert.showAndWait();
        } else {
            infoAccepted = true;
            Stage stage = (Stage) btnAccept.getScene().getWindow();
            stage.close();
        }
    }

    @FXML
    void Clear(ActionEvent event) {
        txtName.clear();
        colorColorPicker.setValue(Color.WHITE);
        chkComputer.setSelected(false);
    }

    public Color getColor() {
        return colorColorPicker.getValue();
    }

    public boolean getChkComputer() {
        return chkComputer.isSelected();
    }

    public String getTxtName() {
        return txtName.getText();
    }

    public boolean isInfoAccepted() {
        return infoAccepted;
    }
}

MainMenu fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane prefHeight="81.0" prefWidth="442.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Homework_5.FXML_Java_Classes.MainMenu">
   <children>
      <Button fx:id="btnStartGame" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" onAction="#startGame" prefHeight="55.0" prefWidth="86.0" text="Start Game" />
      <Button fx:id="btnViewPlayers" layoutX="110.0" layoutY="14.0" mnemonicParsing="false" onAction="#viewPlayers" prefHeight="55.0" prefWidth="86.0" text="View Players" />
      <Button fx:id="btnViewPlayerGames" layoutX="302.0" layoutY="14.0" mnemonicParsing="false" onAction="#viewPlayerGames" prefHeight="55.0" prefWidth="127.0" text="View Player Games" />
      <Button fx:id="btnViewGames" layoutX="206.0" layoutY="14.0" mnemonicParsing="false" onAction="#viewGames" prefHeight="55.0" prefWidth="86.0" text="View Games" />
   </children>
</AnchorPane>

玩家信息fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane prefHeight="201.0" prefWidth="213.0" rotate="-0.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Homework_5.FXML_Java_Classes.PlayerInfo">
   <children>
      <Button fx:id="btnAccept" cancelButton="true" layoutX="14.0" layoutY="165.0" mnemonicParsing="false" onAction="#Accept" text="Accept" />
      <Button fx:id="btnClear" layoutX="156.0" layoutY="165.0" mnemonicParsing="false" onAction="#Clear" text="Clear" />
      <CheckBox fx:id="chkComputer" layoutX="57.0" layoutY="126.0" mnemonicParsing="false" text="Computer" />
      <ColorPicker fx:id="colorColorPicker" layoutX="57.0" layoutY="81.0" />
      <TextField fx:id="txtName" layoutX="57.0" layoutY="41.0" />
      <Label contentDisplay="CENTER" layoutX="14.0" layoutY="14.0" text="Please Enter User Information below" />
      <Label layoutX="14.0" layoutY="45.0" text="Name:" />
      <Label layoutX="14.0" layoutY="85.0" text="Color:" />
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:0)

您调用The honour roll students are: ---------------------------------------------------------------------- Devin Agorhom 83.75 Jevon Ahn 84.125 Chandler Akahira 82.5 Stas Al-Turki 84.25 ... ----------------------------------------------------------------------- The subject award winners are: ----------------------------------------------------------------------- English: Josiah Gower with 99 Math: Carson Whicher with 99 Geography: Ismaila LeBlanc with 100 Science: Jonathan Emes with 100 Gym: Woo Taek (James) Irvine with 100 History: Tami Easterbrook with 100 Art: Nathan Bryson with 100 Music: Jamie Bates with 100 的{​​{1}}实例不是与PersonInfo一起使用的实例。如果提供了Show()属性,则PlayerInfo.fxml将创建控制器类的新实例。要访问此实例,请在调用FXMLLoader之后使用fx:controller的{​​{1}}方法。

您可以例如创建一个FXMLLoader方法来加载并使用它来创建控制器:

getController
load()
static

或者,您可以使用public class PlayerInfo { @FXML private Parent root1; public static PlayerInfo create() throws Exception { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../FXML_Files/PlayerInfo.fxml")); fxmlLoader.load(); return fxmlLoader.getController(); } public PersonData Show() { Stage stage = new Stage(); stage.initModality(Modality.WINDOW_MODAL); stage.setTitle("Enter Player Information"); stage.setScene(new Scene(root1)); stage.showAndWait(); return new PersonData(txtName.getText(), colorColorPicker.getValue(), chkComputer.isSelected()); } ... } 并从“控制器”的构造函数中加载fxml,请参见custom component approach in Introduction to FXML