JavaFx之显示一个国际象棋棋盘

Wenjian96 2017-08-17 11:48:40
我的想法是先设计 白矩形 和黑矩形,然后用循环将矩形填到对应的位置,但无法运行,显示Exception in Application start method,后面还有一大堆,百度也看不懂。
求解:国际象棋棋盘——偶数格为白,奇数格为黑,怎么写?
package demo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class NumberSix extends Application
{
@Override
public void start(Stage primaryStage)
{
GridPane pane = new GridPane();

Rectangle rectangle1 = new Rectangle();
rectangle1.setWidth(10);
rectangle1.setHeight(10);
rectangle1.setFill(Color.WHITE);

Rectangle rectangle2 = new Rectangle();
rectangle2.setWidth(10);
rectangle2.setHeight(10);
rectangle2.setFill(Color.BLACK);

for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
if( (i + j) % 2 == 0)
pane.add(rectangle1, i, j);
else
pane.add(rectangle2, i, j);
}
}
Scene scene = new Scene(pane);
primaryStage.setTitle("14.6");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args)
{
Application.launch(args);
}
}
...全文
640 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
技养 2019-08-02
  • 打赏
  • 举报
回复
意思大概就是你不能重复把同一个引用加到pane中去吧,像下面这样就可以了。 package javaFX; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.GridPane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class Exercise499_14_6 extends Application{ @Override public void start(Stage primaryStage) throws Exception { Rectangle[][] rectangle = new Rectangle[8][8]; GridPane gridPane = new GridPane(); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { rectangle[i][j] = new Rectangle(i, j, 40, 40); if ((i + j) % 2 == 0) { rectangle[i][j].setFill(Color.WHITE); System.out.println("WHITE:" +rectangle[i][j]); } else { rectangle[i][j].setFill(Color.BLACK); System.out.println("BLACK:" + rectangle[i][j]); } gridPane.add(rectangle[i][j], i, j); } System.out.println(i + "==========" + gridPane); } Scene scene = new Scene(gridPane); primaryStage.setTitle("ShowChessboard"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { Application.launch(args); } }
qq_41707602 2018-12-27
  • 打赏
  • 举报
回复 1
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class Test01 extends Application {

public void start(Stage primaryStage) throws Exception {
GridPane pane=new GridPane();
for (int i=0;i<8;i++){
for (int j=0;j<8;j++){
Rectangle rectangle=new Rectangle(80,80);
rectangle.setFill(Color.BLACK);
if ((i+j)%2==0){
pane.add(rectangle,i,j);
}
}
}
Scene scene=new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
天涯若风 2017-08-17
  • 打赏
  • 举报
回复
这个是怎么玩的呀

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧