JavaFX:将textField和Labels添加到场景中

时间:2016-04-28 02:51:32

标签: java javafx

我试图获得两个标签,每个标签都有自己的小标签,包含标签和文本字段/ textareas。我以为我做了一个" Master"应该显示所有内容的VBox除了选项卡以外什么都没有显示,我也没有收到任何错误。 如何将它们添加到场景中?

    // Label and Button variables
private Label fname,lname, appt, AppointmentInfo;
protected String input;

@Override
public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("Person Appointments");
    primaryStage.setWidth(500);
    primaryStage.setHeight(500);

    //Create and set Tab Names
    Tab InfoTab = new Tab();
    Tab ApptTab = new Tab();
    InfoTab.setText("PersonInfo");
    ApptTab.setText("Appointments");

    // VBoxes
    VBox personInfoBox = new VBox();
    VBox FirstBox = new VBox();
    VBox appointmentBox = new VBox();

    // Setup labels and fields
    fname = new Label();
    lname = new Label();
    appt = new Label();
    AppointmentInfo = new Label();
    fname.setText("First Name");
    lname.setText("Last Name");
    appt.setText("Appt Count");
    AppointmentInfo.setText("Appt Info");

    // Text Fields
    TextField firstNameText = new TextField();
    TextField lastNameText = new TextField();
    TextField appointmentText = new TextField();
    TextArea apptInfo = new TextArea();
    firstNameText.setText("First Name Here");
    lastNameText.setText("Last Name Here");
    appointmentText.setText("Appointment Here");
    personInfoBox.getChildren().add(fname);
    personInfoBox.getChildren().addAll(firstNameText);
    personInfoBox.getChildren().add(lname);
    personInfoBox.getChildren().addAll(lastNameText);
    appointmentBox.getChildren().add(AppointmentInfo);
    appointmentBox.getChildren().addAll(apptInfo);


    // Grid for Tabs
    GridPane grid = new GridPane();
    GridPane grid2 = new GridPane();
    grid.add(fname, 0, 0);
    grid.add(firstNameText, 0, 1);
    grid.add(lname, 1, 0);
    grid.add(lastNameText, 1, 1);
    grid.add(appt, 2, 0);
    grid.add(appointmentText, 2, 1);
    grid2.add(AppointmentInfo, 0, 0);
    grid2.add(apptInfo, 0, 1);
    InfoTab.setContent(grid);
    ApptTab.setContent(grid2);

    // TabPane
    TabPane tabPane = new TabPane();
    tabPane.getTabs().add(InfoTab);
    tabPane.getTabs().add(ApptTab);
    InfoTab.setClosable(false);
    ApptTab.setClosable(false);


    InfoTab.setContent(personInfoBox);
    ApptTab.setContent(appointmentBox);
    VBox one = new VBox(tabPane, personInfoBox);
    Scene scene = new Scene(one, 500, 500);
    primaryStage.setScene(scene);
    primaryStage.show();`

1 个答案:

答案 0 :(得分:3)

您无法多次为Node分配SceneGraph

  

在场景图中的任何位置最多可以出现一次节点。具体而言,节点在以下所有方面中必须出现不超过一次:作为Scene的根节点,Parent的子ObservableList,或作为Node的剪辑。

http://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html

您已分配,例如lnamepersonInfoBoxgrid。因此,最终lname将仅分配给网格。然后,您将grid设置为Tab的内容,之后将personInfoBox设置为内容,现在为空