JavaFX中按钮颜色如何更改

5

这是我绘制公交车座位的代码。每个Button表示在GridPane中绘制的一个座位。当有人点击座位时,我想将座位颜色从绿色更改为黄色。到目前为止,我已经这样做了。如果我单击按钮,它会在输出窗口中打印"hellow world"。但是按钮的颜色在UI中没有改变。以下是我的代码:

public static GridPane drawBus(int rows, int col, String ss){
    GridPane table = new GridPane();
    table.setHgap(5);
    table.setVgap(5);
    table.setAlignment(Pos.CENTER);
    String seatName;

    if(ss.equals("ROW WISE")||ss.equals("Row Wise")||ss.equals("row wise")){
    for(int i=0; i<rows; i++){

        for(int j=0;j<col; j++)
        {
        seat=new Button();
        seat.setAlignment(Pos.CENTER);
        seat.setPrefSize(80, 31);

        seatName=numToString(i+1)+(j+1);
        seat.setText(seatName);
        seat.setStyle("-fx-background-color: MediumSeaGreen");


        seat.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {

            seat.setStyle("-fx-background-color: Yellow");
            System.out.println("Hello World!");

        }
        });

        busSeatList.put(seatName, 0);

        //add them to the GridPane
        table.add(seat, j, i); //  (child, columnIndex, rowIndex)

     }


    }
    }
    else
    {
      for(int i=0; i<rows; i++){

        for(int j=0;j<col; j++)
        {
        seat=new Button();
        seat.setAlignment(Pos.CENTER);
        seat.setPrefSize(80, 31);

        seatName=(i+1)+numToString(j+1);
        seat.setText(seatName);
        seat.setStyle("-fx-background-color: MediumSeaGreen");


        seat.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            System.out.println("Hello World!");
            //seat.setStyle("-fx-background-color: Yellow");

        }
        });



        busSeatList.put(seatName, 0);
        //add them to the GridPane
        table.add(seat, j, i); //  (child, columnIndex, rowIndex)


       }


    }


    }


    return table;
}

不建议使用“魔术字符串”,最好使用枚举。这样编译器会在出现拼写错误时发出警告,使代码更易于维护... - fabian
2个回答

5

使用已经实现该功能的类并使用样式表更容易。 ToggleButton 是一个适合您需要的类:

ToggleButton btn = new ToggleButton("Say 'Hello World'");
btn.setOnAction((ActionEvent event) -> {
    System.out.println("Hello World!");
});

...
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

style.css

.toggle-button {
    -fx-background-color: green;
}

.toggle-button:selected {
    -fx-background-color: yellow;
}

顺便提一下:你代码中的问题可能是使用了一个字段(seat)来存储按钮。这样,如果你按下任何按钮,最后创建的按钮将始终被修改。如果你想继续使用你的实现,请在内部循环中声明一个final局部变量。


非常感谢。对于我的实现,使用final局部变量对我很有帮助。这个函数在我的testControllerClass中用于test fxml文件。我需要从Style.css类中获取资源的位置在哪里? - Waliur Rahman
@WaliurRahman 你需要确保在运行时将其包含在类路径中,并将其放置在包含代码的类文件的同一目录中。 - fabian

3

我建议在动态样式中使用自定义的伪类和 CSS:

代码中的伪类:

public static final PseudoClass PSEUDO_CLASS_FOO = PseudoClass.getPseudoClass("foo");

// ... then in your creation method
// Note using java8 lambda is more concise:
seat.setOnAction(event->{
        System.out.println("Hello World!");
       seat.pseudoClassStateChanged(PSEUDO_CLASS_FOO, true);

    });

在您的CSS中:
Button:foo {
    -fx-background-color: yellow;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接