2 回答
TA贡献1827条经验 获得超8个赞
问题来自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()。
TA贡献1848条经验 获得超6个赞
因此,这是一个只有一个场景、舞台和布局的工作示例。所有按钮都可以这样重复使用。这不是我通常喜欢的方式,但它可以工作并且使代码也更短。
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!");
}
}
添加回答
举报