我的程序中有三个场景。当我开始它时,我看到所有按钮的第一个场景。然后,当我进入第二个场景时,我只看到标签,按钮没有显示出来。在第三个场景一切正常,我看到标签和按钮。
所以问题是,为什么我的按钮没有显示在我的第二个场景上?
我试图切换设置按钮和场景的顺序,使场景2(音量)是第三个场景,第三个场景(分辨率)是第二个场景。每次第二个场景都没有显示按钮。
package view.options;
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Options extends Application {
Scene optionsMenu, volumeMenu, resolutionMenu;
// Create the labels
Label optionslabel= new Label("This is the options scene");
Label volumelabel= new Label("This is the volume scene");
Label resolutionlabel= new Label("This is the resolution scene");
Label labl_message = new Label("Volume settings");
Label labl_generalvolume = new Label("General volume");
Label labl_effectvolume = new Label("Effect volume");
Label labl_musicvolume = new Label("Music volume");
// Create the buttons
Button optionsButton= new Button("Go to options");
Button volumeButton= new Button("Go to volume settings");
Button resolutionButton= new Button("Go to resolution settings");
Button saveButton = new Button("save");
Button exitButton = new Button("exit");
// Create the sliders
Slider slider_generalvolume;
Slider slider_effectvolume;
Slider slider_musicvolume;
@Override
public void start(Stage optionsStage) {
// Setup the sliders
slider_generalvolume = new Slider();
slider_generalvolume.setMin(0);
slider_generalvolume.setMax(100);
slider_generalvolume.setValue(50);
slider_generalvolume.setShowTickLabels(true);
slider_generalvolume.setShowTickMarks(true);
slider_generalvolume.setBlockIncrement(10);
slider_effectvolume = new Slider();
slider_effectvolume.setMin(0);
slider_effectvolume.setMax(100);
slider_effectvolume.setValue(50);
slider_effectvolume.setShowTickLabels(true);
slider_effectvolume.setShowTickMarks(true);
slider_effectvolume.setBlockIncrement(10);
slider_musicvolume = new Slider();
slider_musicvolume.setMin(0);
slider_musicvolume.setMax(100);
slider_musicvolume.setValue(50);
slider_musicvolume.setShowTickLabels(true);
slider_musicvolume.setShowTickMarks(true);
slider_musicvolume.setBlockIncrement(10);
// Setup the main button
optionsButton.setOnAction(e -> optionsStage.setScene(optionsMenu));
volumeButton.setOnAction(e -> optionsStage.setScene(volumeMenu));
resolutionButton.setOnAction(e -> optionsStage.setScene(resolutionMenu));
exitButton.setOnAction(e -> Platform.exit());
saveButton.setOnAction(e -> Platform.exit());
// Setup the layout and add elements to it
VBox optionsLayout = new VBox(20);
optionsLayout.getChildren().addAll(optionslabel, volumeButton, resolutionButton, exitButton);
optionsLayout.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
optionsMenu = new Scene(optionsLayout, 300, 250);
VBox volumeLayout = new VBox(20);
volumeLayout.getChildren().addAll(volumelabel, saveButton, optionsButton);
volumeLayout.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
volumeMenu = new Scene(volumeLayout, 300, 250);
VBox resolutionLayout = new VBox(20);
resolutionLayout.getChildren().addAll(resolutionlabel, saveButton, optionsButton);
resolutionLayout.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
resolutionMenu = new Scene(resolutionLayout, 300, 250);
// Setup stage and start it
optionsStage.setTitle("Options");
optionsStage.setScene(optionsMenu);
optionsStage.show();
}
public static void main(String[] args) {
launch(args);
System.out.println("exited successfully!");
}
}
那该死的虫子在哪里?我不明白。它是某种Java或JavaFX伏都教,你看不到第二个场景的按钮?
问题来自saveButton
和来自optionButton
的layout.getChildren().addAll(..., saveButton, optionButton)
。 Java FX节点最多限制为1个父节点,最多1个场景。尝试在多个位置使用它要么从旧父节点中删除它,要么导致异常。在您的情况下,旧父项将被删除并替换为新父项。要可视化问题,您可以更改布局初始化的顺序,因此,您将看到卷布局将起作用,而不是另一个。
因此,如果为每个布局创建saveButton和optionsButton的唯一实例,它将起作用。
例:
Button saveButtonVolume = new Button("save");
Button saveButtonResolution = new Button("save");
// ...
saveButtonVolume.setOnAction(e -> Platform.exit());
saveButtonResolution.setOnAction(e -> Platform.exit());
// ...
resolutionLayout.getChildren().addAll(resolutionlabel, saveButtonResolution, optionsButtonResolution);
volumeLayout.getChildren().addAll(volumelabel, saveButtonVolume, optionsButtonVolume);
遗憾的是,我没有找到任何方法来复制或克隆Java FX节点,这会简化问题(例如:saveButton.clone()
)。
所以,这是一个只有一个场景,舞台和布局的工作示例。所有按钮都可以通过这种方式重复使用。这不是我通常喜欢的方式,但它的工作原理使代码更短。
package view.options;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Options extends Application {
// Create the labels
Label optionslabel= new Label("This is the options scene");
Label volumelabel= new Label("This is the volume scene");
Label resolutionlabel= new Label("This is the resolution scene");
Label labl_message = new Label("Volume settings");
Label labl_generalvolume = new Label("General volume");
Label labl_effectvolume = new Label("Effect volume");
Label labl_musicvolume = new Label("Music volume");
// Create the buttons
Button optionsButton= new Button("Go to options");
Button volumeButton= new Button("Go to volume settings");
Button resolutionButton= new Button("Go to resolution settings");
Button saveButton = new Button("save");
Button exitButton = new Button("exit");
VBox theOneAndOnlyLayout = new VBox(20);
Scene theOneAndOnlyScene;
@Override
public void start(Stage theOneAndOnlyStage) {
theOneAndOnlyLayout.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
theOneAndOnlyScene = new Scene(theOneAndOnlyLayout, 300, 250);
theOneAndOnlyLayout.getChildren().setAll(optionslabel, volumeButton, resolutionButton, exitButton);
optionsButton.setOnAction(e -> {
theOneAndOnlyStage.setTitle("Options");
theOneAndOnlyLayout.getChildren().setAll(optionslabel, volumeButton, resolutionButton, exitButton);
});
volumeButton.setOnAction(e -> {
theOneAndOnlyStage.setTitle("Volume");
theOneAndOnlyLayout.getChildren().setAll(volumelabel, saveButton, optionsButton);
});
resolutionButton.setOnAction(e -> {
theOneAndOnlyStage.setTitle("Resolution");
theOneAndOnlyLayout.getChildren().setAll(resolutionlabel, saveButton, optionsButton);
});
exitButton.setOnAction(e -> Platform.exit());
saveButton.setOnAction(e -> Platform.exit());
// Setup stage and start it
theOneAndOnlyStage.setTitle("Options");
theOneAndOnlyStage.setScene(theOneAndOnlyScene);
theOneAndOnlyStage.show();
}
public static void main(String[] args) {
launch(args);
System.out.println("exited successfully!");
}
}