我正在努力在一个简单的 javafx 应用程序中显示文本,并且我正在努力了解为什么会发生这种情况。这是我的代码:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
//Declarations
Pane root = new Pane();
Scene scene = new Scene(root, 960, 600);
javafx.scene.canvas.Canvas background = new Canvas(3840, 2160);
StackPane infoPane = new StackPane();
Text test = new Text("Hello");
test.setY(500);
test.setX(500);
root.getChildren().add(test);
//Stage Setting
primaryStage.setScene(scene);
primaryStage.setTitle("Test application");
primaryStage.setFullScreen(true);
primaryStage.setFullScreenExitHint("Press escape to exit fullscreen");
primaryStage.show();
//javafx.scene.image.Image icon = new Image("Sample/Test.png");
//primaryStage.getIcons().add(icon);
//Parent and child declarations
infoPane.getChildren().add(test);
//Styling
//Background
StackPane backgroundHolder = new StackPane();
backgroundHolder.setStyle("-fx-background-color: #0053A8");
backgroundHolder.getChildren().add(background);
root.getChildren().add(backgroundHolder);
}
这个想法是拥有一个蓝色背景的应用程序,上面有不同的文本字段。感谢您的帮助!
最佳答案
您将 test
节点添加到 root
Pane ,然后将其也添加到 infoPane
。由于节点只能在场景图中出现一次,因此您实际上是在看到它之前将其从根 Pane 中删除。
请注意,您永远不会将 infoPane
添加到场景图中。
我建议您阅读this tutorial ,也许可以尝试一些简单的布局 Scene Builder 。
关于java - 无法在简单的 javafx 应用程序中显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35667640/