【问题标题】:Javafx loading FXML into TitledPane is blurredJavafx 将 FXML 加载到 TitledPane 中是模糊的
【发布时间】:2015-12-17 08:45:43
【问题描述】:

我正在尝试将 FXML 加载到 TitledPane 中。但它看起来很模糊。 不知道是什么原因。

我的代码包括 2 个文件,如下所示:

MainTitlePane.java

public class MainTitlePane extends Application {

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

  @Override 
  public void start(Stage stage) throws IOException {
    stage.setTitle("TitledPane");
    Scene scene = new Scene(new Group(), 500, 800);
    scene.setFill(Color.GHOSTWHITE);

    //Load AnchorPane from FXML file
    FXMLLoader loader1 = new FXMLLoader(getClass().getResource("ChildPane.fxml"));
    AnchorPane childPane = (AnchorPane) loader1.load();

    //Add pane to TitledPane
    TitledPane tps1 = new TitledPane("First",childPane);         
    Accordion acc = new Accordion (tps1);   

    acc.setExpandedPane(tps1);

    //Add button outside of TitledPane
    Button btnTest = new Button("Button Direct");
    btnTest.setLayoutX(250.0);
    btnTest.setLayoutY(250.0);


    Group root = (Group)scene.getRoot();
    root.getChildren().addAll(acc, btnTest);
    stage.setScene(scene);
    stage.show();
  }
}

和 ChildPane.fxml:

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

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

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<children>
  <ScrollPane layoutX="673.0" prefHeight="380.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
    <content>
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="300.0" prefWidth="189.0">
           <children>
              <Label layoutX="31.0" layoutY="22.0" text="Hello world" />
              <Button layoutX="49.0" layoutY="159.0" mnemonicParsing="false" text="Submit" />
           </children>
        </AnchorPane>
    </content>
  </ScrollPane>

 </children>
</AnchorPane>

当我运行我的代码时,应用程序如下图所示:

如何解决我的问题?

谢谢!

【问题讨论】:

    标签: javafx javafx-8


    【解决方案1】:

    很遗憾,您不太清楚您想通过这种布局层次结构实现什么。无论如何,只需删除 AnchorPane 锚点或将它们设置为 0.5,如下所示。

    AnchorPane.bottomAnchor="0.5" AnchorPane.rightAnchor="0.5" AnchorPane.topAnchor="0.5"
    

    【讨论】:

      【解决方案2】:

      我找到了原因,我在 ChildPane.fxml 中使用了 ScrollPane,滚动条会缩放屏幕,我认为这就是原因。 如果我删除 ScrollPane ,它会起作用。谢谢

      【讨论】:

        【解决方案3】:

        不幸的是,这是一个未解决的问题 afaik。 (https://bugs.openjdk.java.net/browse/JDK-8089499)

        ScrollPane 中的文本内容变得模糊。有几种解决方法;

        1. 正如@wzberger 提到的,将锚约束设置为 0.5 可以解决这个问题(我不知道为什么,但使用 0.0 或 1.0 会再次变得模糊)

        2. 第二种解决方案是通过将 ScrollPane 宽度绑定到 AnchorPane

        scrollPane.prefWidthProperty().bind(anchorPane.widthProperty());
        scrollPane.prefHeightProperty().bind(anchorPane.heightProperty());
        

        如果你觉得有点过头了,可以从宽度属性中减去

        scrollPane.prefWidthProperty().bind(anchorPane.widthProperty().subtract(100));
        scrollPane.prefHeightProperty().bind(anchorPane.heightProperty().subtract(100));
        

        【讨论】:

          猜你喜欢
          • 2017-02-04
          • 1970-01-01
          • 1970-01-01
          • 2019-02-26
          • 1970-01-01
          • 2017-02-08
          • 1970-01-01
          • 1970-01-01
          • 2019-07-14
          相关资源
          最近更新 更多