【问题标题】:dynamic expansion BorderPanel (Center element)动态扩展 BorderPanel(中心元素)
【发布时间】:2018-12-18 19:43:51
【问题描述】:

我想将 FXML 文件 (AnchorPane) 放在 FXML 文件 (BorderPane) 的中心,它可以工作,但它不沿着程序边界延伸,但有它的标准尺寸,我该如何解决这个问题?

谢谢。

public class Main extends Application {
    private Stage primaryStage;
    private BorderPane rootLayout;

    public void initMenuBar() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("../view/fxml/menu.fxml"));
            rootLayout = loader.load();

            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void initMainWindow() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("../view/fxml/mainWindow.fxml"));
            AnchorPane personOverview = loader.load();

            rootLayout.setCenter(personOverview);

            MainWindowController controller = loader.getController();
            controller.setMain(this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception{
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("AddressApp");

        initMenuBar();

        initMainWindow();
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

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

FXML 菜单 - 我在这里写 menuBar:

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

<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="325.0" prefWidth="750.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="model.MainWindowController">
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
   <center>
      <AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #ccccff;" BorderPane.alignment="CENTER" />
   </center>
</BorderPane>

FXML mainWindow——这里我写的是主程序

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane fx:id="mainWindowPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="750.0" style="-fx-background-color: #ccccff;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="model.MainWindowController">
   <children>
      <SplitPane fx:id="mainWindowPane2" dividerPositions="0.7591973244147158" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <TableView prefHeight="398.0" prefWidth="451.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <columns>
                      <TableColumn prefWidth="75.0" text="Сайт" />
                      <TableColumn prefWidth="75.0" text="Почта" />
                        <TableColumn prefWidth="75.0" text="Логин" />
                        <TableColumn prefWidth="75.0" text="Пароль" />
                    </columns>
                     <columnResizePolicy>
                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                     </columnResizePolicy>
                  </TableView>
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <Label layoutX="5.0" layoutY="6.0" text="Дополнительная информация" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="6.0" />
                  <Label layoutX="5.0" layoutY="33.0" text="Label" />
               </children>
            </AnchorPane>
        </items>
      </SplitPane>
   </children>
</AnchorPane>

【问题讨论】:

  • @charlesLgn 是的,我添加了
  • 我认为他不会被需要
  • 你能提供完整的 fxml - 带进口吗?友好总是很好,不要强迫人们自己添加导入。
  • @PrzemekKrysztofiak 已更新

标签: java javafx fxml


【解决方案1】:

这是怎么做到的?

在 mainMzenu.fxml 你有:

<AnchorPane fx:id="mainWindowPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="750.0" ...

这是什么意思?

minHeight="-Infinity" 的意思是minHeight=prefHeight

这样你就可以限制最低限度,这没有问题。问题是你还用maxHeight="-Infinity" maxWidth="-Infinity" 来阻止蜡,所以你需要做的是改变这个值。

maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"

如果您使用场景生成器,您可以选择MAX_VAL

结果:

<AnchorPane fx:id="mainWindowPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="750.0" style="-fx-background-color: #ccccff;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="model.MainWindowController">
  <children>
       .....
  </children>
</AnchorPane>

您也可以删除该值:

<AnchorPane fx:id="mainWindowPane" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="750.0" style="-fx-background-color: #ccccff;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="model.MainWindowController">
  <children>
       .....
  </children>
</AnchorPane>

【讨论】:

    猜你喜欢
    • 2016-04-20
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多