Javafx将FXML加载到TitledPane模糊不清

时间:2015-12-17 08:45:43

标签: javafx javafx-8

我正在尝试将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>

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

enter image description here

我如何解决问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

很遗憾,这是一个尚未解决的问题。 (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());

如果您认为它有点超出范围,则可以减去width属性

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