首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX:当用户在面板中按下鼠标时,它的颜色应该随机改变吗?

JavaFX:当用户在面板中按下鼠标时,它的颜色应该随机改变吗?
EN

Stack Overflow用户
提问于 2016-04-10 05:15:49
回答 1查看 189关注 0票数 2

我的代码有问题。我不得不为之前的一个问题做了一个棋盘。现在我必须使用相同的代码来显示8X8网格,这些网格最初是白色的。一旦点击,他们应该,他们应该随机改变颜色。谁能帮帮忙,这是我的代码副本。

代码语言:javascript
复制
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Control;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Rectangle;
import javafx.event.EventHandler;
import javafx.scene.paint.Paint;
import javafx.scene.paint.Color;

public class ChessBoardColor extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane pane = new GridPane();
        int size = 8 ;

        for (int row = 0; row < size; row++) {

            for (int color = 0; color < size; color ++) {


                StackPane panel = new StackPane();
                String boxcolor ;
                if ((row + color) % 2 == 0) {
                    boxcolor = "red";
                } else {
                    boxcolor = "black";
                }
                panel.setStyle("-fx-background-color:#FFFFFF;");
                pane.add(panel, color, row);



                pane.setOnMouseClicked(e -> {
                pane.setStroke();
                });
               }

            }
        }



        for (int i = 0; i < size; i++) {
            pane.getColumnConstraints().add(new ColumnConstraints(5, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, HPos.CENTER, true));
            pane.getRowConstraints().add(new RowConstraints(5, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, VPos.CENTER, true));
        }

        primaryStage.setScene(new Scene(pane, 500, 500));
        primaryStage.show();


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

回答 1

Stack Overflow用户

发布于 2016-04-10 22:23:23

看看这段代码,只需找到用户单击的节点并应用样式,就像你所知道的:

代码语言:javascript
复制
public class ChessBoardColor extends Application {

@Override
public void start(Stage primaryStage) {
    GridPane pane = new GridPane();
    int size = 8;
    for (int row = 0; row < size; row++) {

        for (int color = 0; color < size; color++) {

            StackPane panel = new StackPane();
            String boxcolor; //initial box color = "white" if you want it white initialy

            if ((row + color) % 2 == 0) {//I suppose it's your chessboard color
                boxcolor = "red";
            } else {
                boxcolor = "black";

            }
            panel.setStyle("-fx-background-color:" + boxcolor + ";");
            pane.add(panel, color, row);

            pane.setOnMouseClicked(e -> { 

                Node target = (Node) e.getTarget(); // you find where the user click
                if (target instanceof StackPane) {

                    String radomColor = getRandomColor(); // choose a random color
                    ((StackPane) target).setStyle("-fx-background-color:" + radomColor + ";"); // apply it like you already know
                }

            });
        }

    }

    for (int i = 0; i < size; i++) {
        pane.getColumnConstraints().add(new ColumnConstraints(5, Control.USE_COMPUTED_SIZE,
                Double.POSITIVE_INFINITY, Priority.ALWAYS, HPos.CENTER, true));
        pane.getRowConstraints().add(new RowConstraints(5, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY,
                Priority.ALWAYS, VPos.CENTER, true));
    }

    primaryStage.setScene(new Scene(pane, 500, 500));
    primaryStage.show();

}

private String getRandomColor() { // simple random color generator

    String colors[] = new String[] {"blue", "yellow", "green", "purple"};
    Random ran = new Random();
    int randomColourIndex = ran.nextInt(4);     
    return colors[randomColourIndex];
}

public static void main(String[] args) {
    launch(args);
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36522810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档