我试图阻止Label与父级重叠。当父级的大小调整为低于标签的大小时,会发生这种情况。在这种情况下,我希望Label开始被隐藏,但它只是显示了父母的界限。
您可以使用以下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>
请注意,向Label添加底部锚点将解决此问题,但我不想使用它。是否有任何解决方法或解决此问题?感谢。
答案 0 :(得分: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());
}
});
}
}