java - 使用 JFXDecorator 时组件不会自动调整大小,如何解决?

标签 java javafx

我想使用 JFXDecorator 来设计我的应用程序窗口的外观。但是使用它会破坏组件的自动调整大小。这就是我的代码的样子

 @Override
 public void start(Stage stage) throws Exception {

    FXMLLoader loader = new FXMLLoader(getClass().getResource("ApplicationView.fxml"));
    Parent root = loader.load();

    JFXDecorator decorator = new JFXDecorator(stage, root);
    decorator.setCustomMaximize(true);

    stage.setTitle("Weather API");
    stage.setScene(new Scene(decorator));

    String cssURI = getClass().getResource("stylesheet/style.css").toExternalForm();
    decorator.getStylesheets().add(cssURI);

    stage.show();
}

这就是效果,看看拆分 Pane 。

window is broken

在没有 JFXDecorator 的情况下,可以正常使用如下代码。

@Override
public void start(Stage stage) throws Exception {

    FXMLLoader loader = new FXMLLoader(getClass().getResource("ApplicationView.fxml"));
    Parent root = loader.load();

    stage.setTitle("Weather API");
    stage.setScene(new Scene(root));

    String cssURI = getClass().getResource("stylesheet/style.css").toExternalForm();

    stage.show();
}

works

我尝试将主 Pane 的 AncorPane 属性设置为与子 Pane 类似,因此它可能会调整大小但失败了。我的 FXML 看起来像那样

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

<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="640.0" stylesheets="@stylesheet/style.css" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.161">
   <children>
     <SplitPane dividerPositions="0.29797979797979796" layoutX="220.0" layoutY="129.0" prefHeight="480.0" prefWidth="640.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" />
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
      </items>
    </SplitPane>
   </children>
</AnchorPane>

我该如何解决?谢谢。

最佳答案

摆脱 AnchorPane 并使 SplitPane 成为根节点。

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

<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>

<SplitPane dividerPositions="0.29797979797979796" prefHeight="480.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <items>
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
   </items>
</SplitPane>

关于java - 使用 JFXDecorator 时组件不会自动调整大小,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49578186/

相关文章:

Java Spring MVC & Jackson : get only selected variables from entities

java - 是否可以将 "Spring Boot like"环境抽象与普通 Spring 一起使用?

java - 如何使用 Apache Commons BeanUtils 设置日期类型值,

java - JBoss 和 ActiveMQ 之间挂起的 Socket.read() 线程

javafx - 使用 Quarkus 或 Springboot 和 JavaFx 进行 native 可执行文件

css - 样式表和内联样式有什么区别?

java - 打开 OBS 正在写入的文件进行读取

java - PDFBox PDFMergerUtility 在 JavaFX 应用程序上不稳定

java - 在 JavaFX 任务中创建 JavaFX 对话框

JavaFX TitledPane 更改的标题背景在鼠标输入时重置