在JavaFX中使用FXML和Controller创建场景 [英] Creating a scene using both FXML and the Controller in JavaFX

查看:283
本文介绍了在JavaFX中使用FXML和Controller创建场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SceneBuilder中创建了一个9x9 GridPane ,我想为每个单元格添加单独的 TextFields 。我确定还有其他方法可以创建像这样的大表,但我不是在寻找一种不同的方法(这是我学习经历的一部分)。我不想在FXML / SceneBuilder中添加 TextFields ;我想在数组中跟踪它们,以便我可以访问它们各自的值,所以我想在Controller中一次创建一个,然后将它们添加到数组以及<$ c $的每个单元格中。 c> GridPane 。

I've created a 9x9 GridPane in SceneBuilder, and I want to add individual TextFields to each cell. I'm certain there are other methods for creating a large table like this, but I'm not looking for a different way to do this (this is part of my learning experience). I don't want to add TextFields in FXML/SceneBuilder; I want to keep track of them in an array so I can access their individual values, so I want to create them one at a time in the Controller and then add them to the array as well as to each cell of the GridPane.

这是我的控制器尝试添加 TextFields (我尝试在将它们添加到数组之前创建它们):

Here is the part of my controller that attempts to add TextFields (I tried creating them before adding them to the array):

@FXML
private GridPane gridPane;
private TextField myTextField[][] = new TextField[9][9];
.
.
.
@Override
public void initialize(URL url, ResourceBundle rb) {
  for (int i = 0; i < 9; ++i){
        for (int j = 0; j < 9; ++j){

            TextField tempTextField = new TextField();
            Font myFont = new Font("System",38);
            tempTextField.setFont(myFont);
            tempTextField.setText(i + ", " + j);
            tempTextField.setPrefSize(70, 70);
            myTextField[i][j] = tempTextField;
            gridPane.add(tempTextField,i,j);
            System.out.println("TextField " +i+j+" Created!");
        }
    } 
}

我没有得到运行前错误且场景未更新。

I don't get an error before runtime and the scene is not updated.

编辑:我查看了 StackTrace 并注意到我得到了一个空指针

I have looked at the StackTrace and noticed that I'm getting a Null Pointer at

gridPane.add(tempTextField,i,j);

FXML文件

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

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

<AnchorPane id="AnchorPane" prefHeight="654.0" prefWidth="747.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="sudokusolver.FXMLDocumentController">
    <children>
      <BorderPane prefHeight="654.0" prefWidth="747.0">
         <center>
            <GridPane fx:id="gridPane" gridLinesVisible="true" prefHeight="198.0" prefWidth="200.0" BorderPane.alignment="CENTER">
               <columnConstraints>
              <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
               </columnConstraints>
               <rowConstraints>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
               </rowConstraints>
            </GridPane>
         </center>
         <bottom>
            <HBox alignment="CENTER" prefHeight="40.0" prefWidth="747.0" BorderPane.alignment="CENTER">
               <children>
                  <FlowPane prefHeight="200.0" prefWidth="200.0">
                     <children>
                        <Button fx:id="loadButton" alignment="CENTER" contentDisplay="CENTER" minHeight="-Infinity" minWidth="-Infinity" onAction="#setGrid" prefHeight="35.0" prefWidth="100.0" text="Load Board" />
                          <Button fx:id="solveButton" alignment="CENTER" contentDisplay="CENTER" minHeight="-Infinity" minWidth="-Infinity" onAction="#sudokuSolve" prefHeight="35.0" prefWidth="100.0" text="Solve" />
                     </children>
                  </FlowPane>
               </children>
            </HBox>
         </bottom>
      </BorderPane>
    </children>
</AnchorPane>


推荐答案

尝试实施 javafx。控制器中的fxml.Initializable 接口,并将 setGrid()逻辑移动到随附的方法。

Try implementing the javafx.fxml.Initializable interface in your controller and move the setGrid() logic to the method that comes with it.

这篇关于在JavaFX中使用FXML和Controller创建场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆