gpt4 book ai didi

java - 在 JavaFX 中设置背景颜色不起作用

转载 作者:行者123 更新时间:2023-11-30 05:52:34 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,该程序接受一个场景中的用户输入以设置第二个场景中的背景颜色。一切正常,除了背景不改变颜色,它保持白色。有谁知道为什么会发生这种情况?

public class Project extends Application {

Scene scene1, scene2;

final int WIDTH = 600;
final int HEIGHT = 600;

Label labelLightOrDark;

TextField tfLightOrDark;

private GridPane createTextFieldPane() {
GridPane gPane = new GridPane();
labelLightOrDark = new Label("Is it light or dark? ");

tfLightOrDark = new TextField();

gPane.add(labelLightOrDark, 0, 2);
gPane.add(tfLightOrDark, 1, 2);

return gPane;
}

@Override
public void start(Stage primaryStage) {
//first scene
Button btGenerate = new Button("Generate My Background!");

GridPane gPane = createTextFieldPane();
gPane.add(btGenerate, 0, 6);
Scene scene1 = new Scene(gPane, 600, 600);

//second scene
Button btReturn = new Button("Try Again!");
btReturn.setOnAction((ActionEvent event2) -> {
primaryStage.setScene(scene1);});

Pane root = new Pane();
String bColor = tfLightOrDark.getText();

if (bColor.equalsIgnoreCase("light")) {
root.setStyle("-fx-background-color: lightblue;");}
else if (bColor.equalsIgnoreCase("dark")) {
root.setStyle("-fx-background-color: darkblue;");}

root.getChildren().add(btReturn);
Scene scene2 = new Scene(root, 600, 600);
btGenerate.setOnAction((ActionEvent event) -> {
primaryStage.setScene(scene2);
});

primaryStage.setTitle("Create Your Own Background!");
primaryStage.setScene(scene1);
primaryStage.show();

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

我相信问题与我的 if、else if 语句有关,因为当我注释掉这些行时,它确实将第二个场景的背景设置为浅蓝色。

    //if (bColor.equalsIgnoreCase("light")) {
root.setStyle("-fx-background-color: lightblue;");}
//else if (bColor.equalsIgnoreCase("dark")) {
//root.setStyle("-fx-background-color: darkblue;");}

最佳答案

一切正常,但请保留对单击内容的引用。这是代码。

    Scene scene1, scene2;

final int WIDTH = 600;
final int HEIGHT = 600;

Label labelLightOrDark;

TextField tfLightOrDark;


private GridPane createTextFieldPane() {
GridPane gPane = new GridPane();
labelLightOrDark = new Label("Is it light or dark? ");

tfLightOrDark = new TextField();

gPane.add(labelLightOrDark, 0, 2);
gPane.add(tfLightOrDark, 1, 2);

return gPane;
}

@Override
public void start(Stage primaryStage) {
//first scene
Button btGenerate = new Button("Generate My Background!");

GridPane gPane = createTextFieldPane();
gPane.add(btGenerate, 0, 6);
Scene scene1 = new Scene(gPane, 600, 600);

//second scene
Button btReturn = new Button("Try Again!");
btReturn.setOnAction((ActionEvent event2) -> {
primaryStage.setScene(scene1);
});

Pane root = new Pane();


root.getChildren().add(btReturn);
Scene scene2 = new Scene(root, 600, 600);
btGenerate.setOnAction((ActionEvent event) -> {
primaryStage.setScene(scene2);
if (tfLightOrDark.getText().contains("light")) {
root.setStyle("-fx-background-color: lightblue;");
}
else if (tfLightOrDark.getText().contains("dark")) {
root.setStyle("-fx-background-color: darkblue;");
}
});

primaryStage.setTitle("Create Your Own Background!");
primaryStage.setScene(scene1);
primaryStage.show();

}

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

关于java - 在 JavaFX 中设置背景颜色不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53644508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com