Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaFX中将标签添加到GridPane时遇到问题_Java_Javafx_Fxml - Fatal编程技术网

在JavaFX中将标签添加到GridPane时遇到问题

在JavaFX中将标签添加到GridPane时遇到问题,java,javafx,fxml,Java,Javafx,Fxml,我在FXML文档中有一个id为matrix的GridPane。我想给矩阵中的每个单元格添加标签。 这是我的控制器: package application.view; import java.awt.Button; import java.awt.Insets; import java.awt.Label; import javafx.fxml.FXML; import javafx.scene.Node; import javafx.scene.layout.GridPane; publ

我在FXML文档中有一个id为matrix的GridPane。我想给矩阵中的每个单元格添加标签。 这是我的控制器:

package application.view;

import java.awt.Button;
import java.awt.Insets;
import java.awt.Label;

import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.GridPane;

public class viewController {

    @FXML
    private GridPane matrix;
    private Label[][] label = new Label[10][10];

    public viewController() {
    }

    @FXML
    private void initialize() {

    }

    @FXML
    private void setMatrix() {
        for (int i = 0; i < label.length; i++) {
            for (int j = 0; j < label[i].length; j++) {
                label[i][j].setText("1");
                matrix.add(label[i][j], i, j);
            }
        }
    }

}
packapplication.view;
导入java.awt.Button;
导入java.awt.Insets;
导入java.awt.Label;
导入javafx.fxml.fxml;
导入javafx.scene.Node;
导入javafx.scene.layout.GridPane;
公共类视图控制器{
@FXML
私有网格窗格矩阵;
私有标签[][]标签=新标签[10][10];
公共视图控制器(){
}
@FXML
私有void初始化(){
}
@FXML
私有void setMatrix(){
对于(int i=0;i
这是我的FXML文档:

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

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

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.view.viewController">
   <children>
      <SplitPane dividerPositions="0.8467336683417085" orientation="VERTICAL" prefHeight="200.0" prefWidth="160.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
               <children>
                  <GridPane fx:id="matrix" alignment="CENTER" disable="true" gridLinesVisible="true" layoutX="73.0" layoutY="125.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <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 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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                  </GridPane>
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
               <children>
                  <HBox alignment="CENTER" prefHeight="65.0" prefWidth="200.0" spacing="10.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                     <children>
                        <Button fx:id="refresh" mnemonicParsing="false" text="Refresh" />
                        <Button fx:id="findBlock" mnemonicParsing="false" text="Find Largest Block" />
                     </children>
                  </HBox>
               </children>
            </AnchorPane>
        </items>
      </SplitPane>
   </children>
</AnchorPane>


正如你所看到的,我得到了id矩阵的GridPane。我想在每个单元格中添加一个标签。标签中显然有一个数字。唯一让我出错的是
矩阵。add()
。它表示无法转换为节点。请帮忙。谢谢大家!

尝试使用javafx.scene.control.Label类。

尝试使用javafx.scene.control.Label类。

尝试使用javafx.scene.control.Label类。

就像用户4132613说的那样,导入
javafx.scene.control.Label
而不是
java.awt.Label
。实际上,不要将AWT类与JavaFX一起使用

您已经创建了标签数组,但是还没有实例化数组中的对象。方法setMatrix()应为:

@FXML
private void setMatrix() {
    for (int i = 0; i < label.length; i++) {
      for (int j = 0; j < label[i].length; j++) {
            label[i][j] = new Label(); // This is missing in the original code
            label[i][j].setText("1");
            matrix.add(label[i][j], i, j);
        }
    }
}
@FXML
私有void setMatrix(){
对于(int i=0;i
就像用户4132613所说的,导入
javafx.scene.control.Label
而不是
java.awt.Label
。实际上,不要将AWT类与JavaFX一起使用

您已经创建了标签数组,但是还没有实例化数组中的对象。方法setMatrix()应为:

@FXML
private void setMatrix() {
    for (int i = 0; i < label.length; i++) {
      for (int j = 0; j < label[i].length; j++) {
            label[i][j] = new Label(); // This is missing in the original code
            label[i][j].setText("1");
            matrix.add(label[i][j], i, j);
        }
    }
}
@FXML
私有void setMatrix(){
对于(int i=0;i
就像用户4132613所说的,导入
javafx.scene.control.Label
而不是
java.awt.Label
。实际上,不要将AWT类与JavaFX一起使用

您已经创建了标签数组,但是还没有实例化数组中的对象。方法setMatrix()应为:

@FXML
private void setMatrix() {
    for (int i = 0; i < label.length; i++) {
      for (int j = 0; j < label[i].length; j++) {
            label[i][j] = new Label(); // This is missing in the original code
            label[i][j].setText("1");
            matrix.add(label[i][j], i, j);
        }
    }
}
@FXML
私有void setMatrix(){
对于(int i=0;i
就像用户4132613所说的,导入
javafx.scene.control.Label
而不是
java.awt.Label
。实际上,不要将AWT类与JavaFX一起使用

您已经创建了标签数组,但是还没有实例化数组中的对象。方法setMatrix()应为:

@FXML
private void setMatrix() {
    for (int i = 0; i < label.length; i++) {
      for (int j = 0; j < label[i].length; j++) {
            label[i][j] = new Label(); // This is missing in the original code
            label[i][j].setText("1");
            matrix.add(label[i][j], i, j);
        }
    }
}
@FXML
私有void setMatrix(){
对于(int i=0;i
非常感谢您!我已经花了两个小时试图解决这个问题,而且解决起来也很容易。哇!我想这是一个学习过程,AWT和JavaFX共享许多控件名是一个不幸的决定,就像Label一样。但是,当代码甚至不编译时,检查导入的类型是一种很好的做法。更好的方法是在IDE中设置从自动导入中排除完整路径(如java.awt.*path)。Eclipse和IntelliJ IDEA都支持这个选项。非常感谢!我已经花了两个小时试图解决这个问题,而且解决起来也很容易。哇!我想这是一个学习过程,AWT和JavaFX共享许多控件名是一个不幸的决定,就像Label一样。但是,当代码甚至不编译时,检查导入的类型是一种很好的做法。更好的方法是在IDE中设置从自动导入中排除完整路径(如java.awt.*path)。Eclipse和IntelliJ IDEA都支持这个选项。非常感谢!我已经花了两个小时试图解决这个问题,而且解决起来也很容易。哇!我想这是一个学习过程,AWT和JavaFX共享许多控件名是一个不幸的决定,就像Label一样。但当代码不可用时