我正在用JavaFX测试一些东西,遇到了一个问题。我有3个具有操作事件的单选按钮。这些单选按钮每个都代表一种颜色(红色、蓝色和黄色),如果你按下它,正方形应该会改变颜色。
下面是我的带有按钮的窗格:公共类ButtonsPane扩展VBox{私有最终ControlPane控制窗格;
public ButtonsPane(ControlPane controlPane) {
this.controlPane = controlPane;
this.setPrefHeight(150);
this.setPrefWidth(120);
RadioButton radioButton1 = new RadioButton("Red");
radioButton1.setSelected(true);
radioButton1.setOnAction(e -> controlPane.setColor("Red"));
RadioButton radioButton2 = new RadioButton("Blue");
radioButton2.setOnAction(e -> controlPane.setColor("Blue"));
RadioButton radioButton3 = new RadioButton("Yellow");
radioButton3.setOnAction(e -> controlPane.setColor("Yellow"));
ToggleGroup toggleGroup = new ToggleGroup();
toggleGroup.getToggles().addAll(radioButton1, radioButton2, radioButton3);
this.getChildren().addAll(radioButton1, radioButton2, radioButton3);
this.setPadding(new Insets(20, 0, 0, 15));
}
}
下面是我的控制面板。动作事件调用DrawPane中的一个方法,该方法尝试将String颜色转换为真实的Color对象值。
public class ControlPane extends VBox {
private Color color;
private final DrawPane drawPane;
public ControlPane(boolean buttons) {
ButtonsPane buttonsPane = new ButtonsPane(this);
ListViewPane listViewPane = new ListViewPane();
drawPane = new DrawPane();
SliderPane sliderPane = new SliderPane();
if(buttons) {
this.getChildren().add(buttonsPane);
} else {
this.getChildren().add(listViewPane);
}
this.setPrefHeight(350);
this.setPrefWidth(120);
this.getChildren().add(sliderPane);
this.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, null)));
}
public void setColor(String color) {
try {
drawPane.setColor(Color.valueOf(color));
} catch (Exception e) {
drawPane.setColor(Color.WHITE);
}
}
}
如果颜色失败或不会有一个方法调用我的窗格,应该改变颜色。
public class DrawPane extends BorderPane {
private final Rectangle rectangle;
public DrawPane() {
this.rectangle = new Rectangle();
this.rectangle.setFill(Color.RED);
this.rectangle.setHeight(180);
this.rectangle.setWidth(180);
this.rectangle.setStroke(Color.BLACK);
this.setCenter(this.rectangle);
this.setPadding(new Insets(10));
this.setPrefHeight(350);
this.setPrefWidth(380);
}
public void setColor(Color color) {
System.out.println("true");
this.rectangle.setFill(Color.BLACK);
this.setBackground(new Background(new BackgroundFill(Color.YELLOW, null, null)));
}
}
场景:
public class MyScene extends Scene {
public MyScene(HBox root) {
super(root);
root.getChildren().addAll(new DrawPane(), new ControlPane(true));
}
}
该方法被调用,因为我已经用System. out测试过了,但是颜色没有更新。我遗漏了什么或做错了什么?
工作样品
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class RectangleApp extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Rectangle rectangle = new Rectangle(100, 100, Color.BLACK);
StackPane stackPane = new StackPane(rectangle);
RadioButton redButton = new RadioButton("Red");
RadioButton blueButton = new RadioButton("Blue");
//It's not good style to get the color using valueOf() but I'm staying in context of the sample
redButton.setOnAction(event -> rectangle.setFill(Color.valueOf("red")));
blueButton.setOnAction(event -> rectangle.setFill(Color.valueOf("blue")));
ToggleGroup toggleGroup = new ToggleGroup();
toggleGroup.getToggles().setAll(redButton, blueButton);
HBox hBox = new HBox(redButton, blueButton);
VBox vBox = new VBox(hBox, stackPane);
Scene scene = new Scene(vBox, 400, 400);
stage.setScene(scene);
stage.show();
}
}