编辑:我确定是以下原因:
primaryStage.initStyle(StageStyle.UNIFIED);
能解释为什么会导致此问题吗?
JDK:13.0.1
FX:13
Eclipse:2019-09 R(4.13.0)
也许我快要疯了,因为我感觉我对JavaFX的表现还不错。但是由于某些原因,这些按钮没有出现,但是我可以按下它们,并且记录器说这些按钮被按下了。我可以按退出按钮(虽然不可见),但它按预期退出了...那么为什么按钮不可见?
我已经尝试将可见性设置为true,但这也没做。按钮大小似乎也已正确设置。
public class test extends Application {
private Button gameButton = new Button("Game");
private Button mapEditorButton = new Button("Map Editor");
private Button exitButton = new Button("Exit");
private static final int buttonHeight = 25;
private static final int buttonWidth = 100;
private static final int screenwidth = 350;
private static final int screenHeight = 150;
private Scene theScene;
private Stage primaryStage;
private VBox root = new VBox(30);
private HBox top = new HBox(10);
public void start(Stage stage) {
primaryStage = stage;
setButtonSizes();
root.setPrefSize(screenwidth, screenHeight);
root.resize(screenwidth, screenHeight);
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(top, exitButton);
top.getChildren().addAll(gameButton, mapEditorButton);
top.setAlignment(Pos.CENTER);
theScene = new Scene(root);
primaryStage.setTitle("Menu Selector");
primaryStage.setWidth(screenwidth);
primaryStage.setHeight(screenHeight);
primaryStage.setResizable(false);
primaryStage.initStyle(StageStyle.UNIFIED);
primaryStage.setScene(theScene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private void setButtonSizes() {
gameButton.setMinSize(buttonWidth, buttonHeight);
gameButton.setMaxSize(buttonWidth, buttonHeight);
gameButton.setPrefSize(buttonWidth, buttonHeight);
mapEditorButton.setMinSize(buttonWidth, buttonHeight);
mapEditorButton.setMaxSize(buttonWidth, buttonHeight);
mapEditorButton.setPrefSize(buttonWidth, buttonHeight);
exitButton.setMinSize(buttonWidth, buttonHeight);
exitButton.setMaxSize(buttonWidth, buttonHeight);
exitButton.setPrefSize(buttonWidth, buttonHeight);
}
}
这是问题的图像:
注释掉您的代码行以尝试找出导致此问题的原因之后,我发现正是这一行...
primaryStage.initStyle(StageStyle.UNIFIED);
然后我查看了类StageStyle
的javadoc。它指出...
指定此阶段的样式。必须在使阶段可见之前完成此操作。样式是以下之一:
StageStyle.DECORATED
,StageStyle.UNDECORATED
,StageStyle.TRANSPARENT
或StageStyle.UTILITY
嗯,UNIFIED
在这里没有提到。尽管如此,您的代码仍可以编译并正确运行,因此UNIFIED
必须为有效值。因此,我查看了UNIFIED
的javadoc并阅读了此...
这是一个有条件的功能,要检查它是否受支持,请参阅
javafx.application.Platform.isSupported
正在运行此方法...
Platform.isSupported(ConditionalFeature.UNIFIED_WINDOW)
在运行[Oracle] JDK 13.0.1的Windows 10 64位计算机上返回true
尽管如此,使用StageStyle.UNIFIED
仍会引起问题。我的猜测可能是错误。无论如何,如果您删除此行(或将其注释掉),您将看到您的按钮。