[英]Application with BorderPane doesn't display simple button
JavaFX的新手,我在这里遵循一个简单的教程,我创建了一个新的JavaFX项目,但是默认情况下,它具有BorderPane作为默认值,而不是本教程所说的StackPane,所以我就把它留在那里了。 该应用程序上只有一个按钮,如果我使用BorderPane,则不会显示该按钮。 如果将其更改为StackPane,则会显示该按钮。 出于某种原因,我以为BorderPane裁剪了一些内容,因此使应用程序窗口变大,但仍然看不到该按钮。 这是BorderPane的代码,该代码不显示按钮:
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("This is a test!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
root.getChildren().add(btn);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
任何想法?
看看有关BorderPane
的文档:
BorderPane在顶部,左侧,右侧,底部和中心位置布置子项。
因此,您需要使用以下内容:
borderPane.setTop(toolbar);
borderPane.setCenter(appContent);
borderPane.setBottom(statusbar);
在你的情况下root.getChildren().add(btn);
例如应该是root.setCenter(btn);
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.