2016-02-27 81 views
0

我正努力在简单的javafx应用程序中显示文本,而且我正在努力查明它为什么会发生。这里是我的代码:无法在简单的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); 
} 

这个想法是有一个蓝色背景的应用程序,它有不同的文本字段。感谢您的帮助!

回答

1

您将test节点添加到您的root窗格,但是您将其添加到infoPane。由于一个节点只能在场景图中出现一次,因此您必须先将其从根窗格中移除,然后才能看到它。

请注意,您绝不会将infoPane添加到场景图中。

我建议你阅读this tutorial,也许可以尝试几个简单的布局与Scene Builder

+0

啊,所以它只是凌乱的编码。非常感谢!我真的很挣扎了好一阵子。 –

相关问题