2016-09-21 85 views
0

在JavaFX我试图阻止标签重叠的父母。这发生在父级调整为比标签更低的大小时。在这种情况下,我期望标签开始隐藏起来,但它只是稍微显示了父项的边界。JavaFX停止标签重叠父项?

您可以使用以下FXML测试了这一点:

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

<?import javafx.scene.control.Label?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.text.Font?> 


<AnchorPane minHeight="500.0" minWidth="500.0" prefHeight="500.0" prefWidth="558.0" style="-fx-background-color: black;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65"> 
    <children> 
     <AnchorPane layoutX="110.0" layoutY="102.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" AnchorPane.bottomAnchor="15.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="15.0"> 
     <children> 
      <Label alignment="CENTER" layoutX="238.0" layoutY="201.0" style="-fx-background-color: gray;" text="Testing to see if this overlaps" textFill="WHITE" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="5.0"> 
       <font> 
        <Font size="37.0" /> 
       </font> 
      </Label> 
     </children> 
     </AnchorPane> 
    </children> 
</AnchorPane> 

注意添加底部锚标签会解决这个问题,但我想不使用。有没有解决这个问题?谢谢。

回答

0

可以提供一些拐杖。添加标签MinHeight和MaxHeight并添加到窗格heightProperty侦听器。

Main.fxml:

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

<?import javafx.scene.control.Label?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.text.Font?> 

<AnchorPane minHeight="500.0" minWidth="500.0" prefHeight="500.0" prefWidth="558.0" style="-fx-background-color: black;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller"> 
    <children> 
     <AnchorPane fx:id="pane" layoutX="16.0" layoutY="15.0" style="-fx-background-color: red;" AnchorPane.bottomAnchor="15.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="15.0"> 
      <children> 
       <Label fx:id="label" alignment="CENTER" maxHeight="54.0" minHeight="0.0" style="-fx-background-color: gray;" text="Testing to see if this overlaps" textFill="WHITE" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="5.0"> 
        <font> 
         <Font size="37.0" /> 
        </font> 
       </Label> 
      </children> 
     </AnchorPane> 
    </children> 
</AnchorPane> 

Controller.java:

import javafx.fxml.FXML; 
import javafx.scene.control.Label; 
import javafx.scene.layout.AnchorPane; 

public class Controller { 

    @FXML 
    Label label; 
    @FXML 
    AnchorPane pane; 

    @FXML 
    private void initialize() { 

     pane.heightProperty().addListener((observableValue, oldSceneHeight, newSceneHeight) -> { 
      double top_margin = 5; 
      double bottom_margin = 5; 
      double bound_value = label.getMaxHeight() + top_margin + bottom_margin; 
      if (newSceneHeight.doubleValue() < bound_value) { 
       if ((newSceneHeight.doubleValue() - top_margin - bottom_margin) >= 0) { 
        label.setPrefHeight(newSceneHeight.doubleValue() - top_margin - bottom_margin); 
       } 
      } else { 
       label.setPrefHeight(label.getMaxHeight()); 
      } 
     }); 
    } 

}