我正在尝试编写一个程序,该程序在一个场景中接受用户输入,以在第二个场景中设置背景色。一切正常,但背景不会改变颜色,而是保持白色。有谁知道为什么会这样?
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;");}
答案 0 :(得分:1)
一切正常,但请保留对单击内容的引用。这是代码。
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);
}
}
答案 1 :(得分:0)
代码的问题是,您正在渲染场景图形之前检查文本字段中的值。您需要将if-else逻辑移到onAction按钮上。
btGenerate.setOnAction((ActionEvent event) -> {
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;");
}
primaryStage.setScene(scene2);
});