gpt4 book ai didi

java - 在 JavaFX 中向 GridPane 添加标签时遇到问题

转载 作者:行者123 更新时间:2023-12-02 05:23:13 24 4
gpt4 key购买 nike

我在 FXML 文档中有一个 GridPane,其 id 为矩阵。我想为矩阵中的每个单元格添加标签。这是我的 Controller :

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);
}
}
}

}

这是我的 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。我想在每个单元格中添加一个标签。标签中显然会有一个数字。唯一给我带来错误的是matrix.add()。它说无法转换为节点。请帮忙。谢谢你!

最佳答案

就像user4132613所说,导入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);
}
}
}

关于java - 在 JavaFX 中向 GridPane 添加标签时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26322735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com