Javafx 有没有更好的方法将 myObjects 添加到 GridPane?

Javafx Is there a better way to add myObjects to GridPane?

我正在寻找答案并尝试了很多选项。最后我找到了将我自己的 javaxf 对象传递给 GridPane 的方法。但我仍然认为有比我现在做的更好的方法。 所以这是我的代码: 主要:

package sample;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.layout.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import static java.lang.String.valueOf;

public class Main extends Application {

    private My_Rectangle selectedShelf;

    Stage window;
    GridPane grid;

    @Override
    public void start(Stage primaryStage) throws Exception {
    window = primaryStage;

        BackgroundFill background_fill = new BackgroundFill(Color.PINK,
                CornerRadii.EMPTY, Insets.EMPTY);
        Background background = new Background(background_fill);

    grid = new GridPane();
    grid.setBackground(background);
    grid.setPadding(new Insets(10,10,10,10));
    grid.setVgap(10);
    grid.setHgap(10);
    grid.setAlignment(Pos.CENTER);


    for (int i = 0; i<10;i++){
        for(int y = 0; y<10;y++){
            My_Rectangle rect = new My_Rectangle(grid,i,y,new Text( valueOf(i+y)) );
        }
    }
    Scene scene = new Scene(grid, 400, 400);
    window.setScene(scene);
    window.show();

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

package sample;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;

public class My_Rectangle {
    private float wide;
    private float height;
    private final Rectangle rect;
    private boolean selected;

    public  My_Rectangle( GridPane cheff, int x, int y, Text text) {
        wide = 20;
        height = 30;
        selected = false;
        rect = new Rectangle(wide,height);
        rect.setFill(Color.GREEN);
        text.setTextAlignment(TextAlignment.CENTER);

        EventHandler<MouseEvent> clickedHandler = ev -> {
            selected = !selected;
            this.selected();
            ev.consume();
        };
        rect.setOnMouseClicked(clickedHandler);
        cheff.add(rect, x ,y );
        cheff.add(text, x ,y );
    }
    public void selected() {
        if (selected) {
            rect.setFill(Color.GREEN);
            ShelfShowUp.display("KOKOS", 7);
        } else {
            rect.setFill(Color.HOTPINK);
        }
    }
}

我的意思是,通过更好的方式,除了传递 GridPane 和 int x, int y 索引之外,可能还有其他方式。我还是想用 GridPane。

感谢您的帮助 我是新手

正如@JimD 已经评论过的,将对节点父节点的引用嵌入到节点中是不好的做法。除了切换颜色的事件处理程序之外,矩形没有什么特别之处。假设您不想访问矩形的“选定”状态,您可以完全放弃自定义 class。

除了调用 [not included] "ShelfShowUp.display("KOKOS", 7)" :

之外,以下代码执行您的示例所做的所有操作
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import static java.lang.String.valueOf;

public class GridPaneCustom extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        GridPane grid = new GridPane();
        grid.setBackground(new Background(new BackgroundFill(Color.PINK, CornerRadii.EMPTY, Insets.EMPTY)));
        grid.setPadding(new Insets(10));
        grid.setVgap(10);
        grid.setHgap(10);
        grid.setAlignment(Pos.CENTER);

        for (int column = 0; column < 10; column++) {
            for (int row = 0; row < 10; row++) {
                Rectangle rectangle = new Rectangle(20, 30, Color.HOTPINK);
                rectangle.setOnMouseClicked(ev -> rectangle.setFill(
                        rectangle.getFill().equals(Color.HOTPINK) ? Color.GREEN : Color.HOTPINK));
                grid.add(rectangle, column, row);
                grid.add(new Text(valueOf(column + row)), column, row);
            }
        }
        primaryStage.setScene(new Scene(grid, 400, 400));
        primaryStage.show();

    }

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