![](/img/trans.png)
[英]Javafx Is there a better way to add myObjects to GridPane?
[英]Is there a way to add rectangles in gridpane and change colors of them
编写一个程序,显示棋盘 7. 每个黑色或白色的单元格都是一个具有相应颜色的 Rectangle 对象。
提示:要创建复选框,请定义 GridPane 类的窗格,并在两个循环内使用 pane.add () 在 GridPane 上放置 Rectangle
这就是我的老师想要的。
我试试这个:
@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
for(int i=0; i<8; i++){
for(int j=0; j<8; j++){
pane.add(new Rectangle(30, 30), i, j);
}
}
Scene scene = new Scene(pane, 300, 300);
primaryStage.setTitle("Exercise_3_7");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
而且我只有黑色,如何将其中一些更改为白色?
在您的循环中执行以下操作:
Rectangle rect = new Rectangle(30, 30);
<change the relevant attributes here>
pane.add(rect, i, j);
提示:矩形是一种形状,因此阅读有关形状https://openjfx.io/javadoc/11/javafx.graphics/javafx/scene/shape/Shape.html的文档以找出答案听起来是个好主意什么相关属性适合您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.