标签不会在 javafx 中显示

Label does not get displayed in javafx

我的应用程序中有一个边框。在底部,我想设置一个标签来显示程序的当前状态,例如:"ready"、"reading data" 等等。

当我启动应用程序时,底部部分没有显示。只有在我用鼠标手动更改 window 的高度后,它才会突然弹出。有谁知道为什么会这样?我想这与我的听众有关。

主要

package Sample;

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

public class main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("View/MainWindow.fxml"));
        primaryStage.setTitle("Tool-Name");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

fxml

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

<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane fx:id="root" prefHeight="453.0" prefWidth="604.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller.MainWindow_Controller">
   <center>
      <TabPane fx:id="tpTabs" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="402.0" prefWidth="604.0" tabClosingPolicy="UNAVAILABLE">
         <tabs>
            <Tab fx:id="tDatabase" text="XXXXXXX">
               <content>
                  <AnchorPane fx:id="apDatabase" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <Button fx:id="bSetDBPath" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" text="XXXXXXX..." />
                        <Button fx:id="bSetHeadFilePath" layoutX="14.0" layoutY="66.0" mnemonicParsing="false"  text="XXXXXXX..." />
                        <TextField fx:id="tfDBPath" layoutX="138.0" layoutY="14.0" prefHeight="25.0" prefWidth="255.0" promptText="XXXXXXX" />
                        <TextField fx:id="tfHeadFilePath" layoutX="138.0" layoutY="66.0" prefHeight="25.0" prefWidth="255.0" promptText="XXXXXXX" />
                     </children>
                  </AnchorPane>
               </content>
            </Tab>
            <Tab text="XXXXXXX">
               <content>
                  <Accordion>
                    <panes>
                      <TitledPane text="XXXXXXX">
                        <content>
                          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                                 <children>
                                    <Button fx:id="bGetNewMembers" layoutX="14.0" layoutY="14.0" mnemonicParsing="false"  text="XXXXXXX" />
                                    <Button fx:id="bGetStatusChange" layoutX="14.0" layoutY="58.0" mnemonicParsing="false"  text="XXXXXXX" />
                                    <Button fx:id="bRegulation" layoutX="14.0" layoutY="101.0" mnemonicParsing="false"  text="XXXXXXX" />
                                 </children>
                              </AnchorPane>
                        </content>
                      </TitledPane>
                      <TitledPane text="XXXXXXX">
                        <content>
                          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                                 <children>
                                    <Button fx:id="bAdministrationData" layoutX="14.0" layoutY="57.0" mnemonicParsing="false"  text="XXXXXXX" />
                                    <Button fx:id="bCompareStatus" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" text="XXXXXXX" />
                                 </children>
                              </AnchorPane>
                        </content>
                      </TitledPane>
                      <TitledPane text="XXXXXXX">
                        <content>
                          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                                 <children>
                                    <Button fx:id="bOrganisation" layoutX="14.0" layoutY="103.0" mnemonicParsing="false"  text="XXXXXXX" />
                                    <Button fx:id="bDeferredCompensation" layoutX="14.0" layoutY="14.0" mnemonicParsing="false"  text="XXXXXXX" />
                                    <Button fx:id="bCompanyMatching" layoutX="14.0" layoutY="60.0" mnemonicParsing="false"  text="XXXXXXX" />
                                 </children>
                              </AnchorPane>
                        </content>
                      </TitledPane>
                    </panes>
                  </Accordion>
               </content>
            </Tab>
            <Tab fx:id="tTestCases" text="XXXXXXX">
               <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TableView fx:id="tvTestCases" prefHeight="310.0" prefWidth="600.0">
                           <columns>
                              <TableColumn fx:id="tcTestCaseId" prefWidth="132.0" text="XXXXXXX">
                                  <cellValueFactory><PropertyValueFactory property="testCaseId" />
                                  </cellValueFactory>
                              </TableColumn>
                              <TableColumn fx:id="tcPaymentId" prefWidth="150.0" text="XXXXXXX">
                                  <cellValueFactory><PropertyValueFactory property="paymentId" />
                                  </cellValueFactory>
                              </TableColumn>
                           </columns>
                        </TableView>
                     </children>
                  </AnchorPane>
               </content>
            </Tab>
            <Tab fx:id="tReports" text="XXXXXXX">
               <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TableView fx:id="tvReports" prefHeight="310.0" prefWidth="604.0">
                           <columns>
                              <TableColumn fx:id="tcReportId" editable="false" prefWidth="75.0" text="XXXXXXX" />
                              <TableColumn fx:id="tcReportType" editable="false" prefWidth="75.0" text="XXXXXXX" />
                              <TableColumn fx:id="tcCreationDate" editable="false" prefWidth="75.0" text="XXXXXXX" />
                           </columns>
                        </TableView>
                     </children>
                  </AnchorPane>
               </content>
            </Tab>
         </tabs>
      </TabPane>
   </center>
   <top>
      <MenuBar fx:id="mbMenuBar" prefHeight="30.0" prefWidth="604.0" BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="XXXXXXX">
            <items>
                  <MenuItem mnemonicParsing="false" text="XXXXXXX" />
                  <MenuItem mnemonicParsing="false" text="XXXXXXX" />
                  <SeparatorMenuItem mnemonicParsing="false" />
              <MenuItem mnemonicParsing="false" text="XXXXXXX" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="XXXXXXX">
            <items>
                  <Menu mnemonicParsing="false" text="XXXXXXX">
                    <items>
                      <MenuItem fx:id="miNewVoucher" mnemonicParsing="false" text="XXXXXXX" />
                        <MenuItem fx:id="miNewAssociation" mnemonicParsing="false" text="XXXXXXX" />
                        <MenuItem fx:id="miNewPensioner" mnemonicParsing="false" text="XXXXXXX" />
                        <MenuItem fx:id="miNewRegulation" mnemonicParsing="false" text="XXXXXXX" />
                        <MenuItem fx:id="miNewAdministrationCosts" mnemonicParsing="false" text="XXXXXXX" />
                        <MenuItem fx:id="miNewPayment" mnemonicParsing="false" text="XXXXXXX" />
                    </items>
                  </Menu>
                  <SeparatorMenuItem mnemonicParsing="false" />
              <MenuItem fx:id="miOpenDB" mnemonicParsing="false" text="XXXXXXX" />
                  <MenuItem fx:id="miImportDatasets" mnemonicParsing="false" text="XXXXXXX" />
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem fx:id="miSetDBPath" mnemonicParsing="false" text="XXXXXXX" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="XXXXXXX">
            <items>
              <MenuItem fx:id="miOpenManual" mnemonicParsing="false" text="XXXXXXX" />
                  <MenuItem mnemonicParsing="false" text="XXXXXXX" />
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem fx:id="miSetManualPath" mnemonicParsing="false" text="XXXXXXX" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
   <bottom>
      <Label fx:id="lInfo" prefHeight="21.0" prefWidth="604.0" text="Bereit" BorderPane.alignment="CENTER" />
   </bottom>
</BorderPane>

控制器

package Controller;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
public class MainWindow_Controller {

    @FXML
    private BorderPane root;

    @FXML
    private TableView tvTestCases;

    @FXML
    private TableView<?> tvReports;

    @FXML
    MenuBar mbMenuBar;
    @FXML
    private TabPane tpTabs;

    @FXML
    private Label lInfo;

    /**
     * Initialize method to read filepaths and set listeners.
     */
    public void initialize() {
        tpTabs.setBackground(new Background(new BackgroundFill(Color.BISQUE, null, null)));
        loadListener();
        lInfo.setText("Bereit");
    }

    public void loadListener() {
        root.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                tpTabs.setPrefWidth(root.getWidth());
                tvTestCases.setPrefWidth(root.getWidth());
                tvReports.setPrefWidth(root.getWidth());
            }
        });

        root.heightProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {

                tpTabs.setPrefHeight(root.getHeight() - mbMenuBar.getHeight()-lInfo.getHeight());
                tvTestCases.setPrefHeight(root.getHeight() - mbMenuBar.getHeight()-lInfo.getHeight());
                tvReports.setPrefHeight(root.getHeight()-mbMenuBar.getHeight()-lInfo.getHeight());
            }
        });
    }
}

感谢您的帮助

您不应在布局期间调整节点的首选大小。这里发生的事情是,在设置根尺寸之前,通过计算preferred/min/max尺寸来确定场景尺寸。设置根大小时,loadListener 中添加的侦听器会更改首选大小,不再为所有节点留出足够的空间。由于 Scene 强制其大小小于其根现在的实际最小大小,布局以某种方式适应这一点,在本例中通过将 Label 移出视图。

不要添加这些侦听器并将调整大小留给 BorderPane。为此,您需要将最大尺寸设置为 Region.USE_PREF_SIZE (= -1d/0d = Double.NEGATIVE_INFINITY).

以外的尺寸

以下 <TabPane> 的开始标记仅使用默认值 (Region.USE_COMPUTED_SIZE)。这样 TabPane 可以增长任意大:

<TabPane fx:id="tpTabs" minHeight="-Infinity" minWidth="-Infinity" prefHeight="402.0" prefWidth="604.0" tabClosingPolicy="UNAVAILABLE">