在 JavaFX 中设置背景颜色不起作用
Setting background color in JavaFX not working
我正在尝试编写一个程序,该程序在一个场景中接受用户输入以设置第二个场景中的背景颜色。一切正常,除了背景不会改变颜色,它保持白色。有谁知道为什么会这样?
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;");}
代码的问题是您在渲染场景图之前检查文本字段中的值。您需要将 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);
});
一切正常,但请保留对所点击内容的引用。这是代码。
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);
}
}
我正在尝试编写一个程序,该程序在一个场景中接受用户输入以设置第二个场景中的背景颜色。一切正常,除了背景不会改变颜色,它保持白色。有谁知道为什么会这样?
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;");}
代码的问题是您在渲染场景图之前检查文本字段中的值。您需要将 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);
});
一切正常,但请保留对所点击内容的引用。这是代码。
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);
}
}