未创建 javafx Pane

标签 java class javafx pane

我的 JavaFx 代码无法正常工作。我正在尝试创建填充有 1 或 0 的 10X10 文本矩阵,因此它看起来类似于填充有 1 和 0 的二维数组。当我将当前位于 MatrixPane 类中的代码放在 main 中时,它工作正常,但是使用此代码,它只是设置场景,但看起来没有添加或创建任何 Pane 。

如果有人可以帮助我,我将不胜感激。

我意识到我导入了一些未使用的东西,我将它们用于程序的其他部分。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.FlowPane;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javafx.scene.shape.Arc;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.geometry.Pos;
import javafx.collections.ObservableList;

public class Button1 extends Application
{
    public void start(Stage primaryStage)
    {
        GridPane pane = new GridPane();
        MatrixPane Matrix = new MatrixPane();
        pane.getChildren().add(Matrix);

        Scene scene = new Scene(pane, 700, 500);
        primaryStage.setTitle("1 window "); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }

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

}


class MatrixPane extends Pane
{
    double HEIGHT = 500;
    double WIDTH = 200;
    private GridPane pane1 = new GridPane();

    public MatrixPane()
    {
    }

    public void fillmatrix()
    {
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                TextField text = new TextField(Integer.toString((int)(Math.random() * 2)));
                text.setMinWidth(WIDTH / 8.0);
                text.setMaxWidth(WIDTH / 10.0);
                text.setMinHeight(HEIGHT / 8.0);
                text.setMaxHeight(HEIGHT / 10.0);
                this.pane1.add(text, j, i);
            }
        }
    }
}

最佳答案

Button1.start() 调用 fillMatrix(); 方法

在 MatrixPane 构造函数中将 GridPane 添加到 MatrixPane

private GridPane pane1 = new GridPane();

public MatrixPane() {
    getChildren().add(pane1);
}

这会起作用。

关于未创建 javafx Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36369038/

相关文章:

java - 如何使用 Preloader 重新启动 JavaFX 中的应用程序?

java - Ruby 中的斯坦福解析器不会创建预处理器

java - 如何在 Java 中将最大的 n 位无符号整数分配给 BigInteger

java - JPanel 和 Frame 之间的默认彩色间距

html - CSS 类 ID 问题

python - 在 Python 中调用父类中的子方法

javascript - ES6 : Applying function as class method

javafx - 减少 native 部署的 JavaFX 应用程序的大小

java - setOnEditCommit JavaFX 后禁用 TableCell

java - JPanel 上的组件不显示