BorderPane为何不显示任何内容

时间:2019-07-30 23:03:11

标签: javafx gluon borderpane

我创建了一个移动应用程序,现在我要使代码更简单,更精确。我想将页面之一从大量群集切换到边界窗格,因为这会使我的代码更整洁。出于某种原因,当我注释工作代码以使用边框窗格时,我的标签将不显示其他所有内容。我觉得好像很小,我看不到

我尝试制作场景,setLeft动作

public BookNow(){

BorderPane bookClub = new BorderPane();
Vbox labels = new VBox();
Label city = new Labels("City: ");
Label venue= new Labels("Venue: ");   
Label date = new Labels("Date: ");   
Label appArrivalTime = new Labels("Approxiamte Time of Arrival: ");

labels.getChildren().addAll(city, venue, date, appArrivalTime);
bookClub.setLeft(labels);

}

它应该只在BorderPane的左侧显示标签。

1 个答案:

答案 0 :(得分:0)

已更新

您将BorderPane元素插入哪里?您必须将组件插入到主FXML Scene中调用的Stage实例中,然后使用所有元素初始化BorderPane。另外,在FXML文件中编写代码信息比在Java类中更清晰:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>

<BorderPane xmlns:fx="http://javafx.com/fxml/1">
    <left>
        <VBox>
            <Label>City: </Label>
            <Label>Venue: </Label>
            <Label>Date: </Label>
            <Label>Approximate Time of Arrival: </Label>
        </VBox>
    </left>
</BorderPane>

您必须使用FXMLLoader.load()将FXML文件(例如'view.fxml')链接到JavaFX应用程序窗口:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(FXMLLoader.load(getClass().getResource("/package/path/to/the/fxml/file/view.fxml")));
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String... args) {
        launch(args);
    }
}

它回答了您的问题吗?