【发布时间】:2017-07-03 09:55:43
【问题描述】:
我正在尝试使用左侧的抽屉制作可调整大小的 BorderPane。我进行了将“左 AnchorPane”移到外部的过渡 - 但不幸的是,BorderPane 的左占位符不会调整大小/移位/或折叠。我希望它会做一个。
无论如何,这里有一些图片可以更好地描述正在发生的事情。注意第一张图片中的空白。这就是问题所在。
Java
import java.io.IOException;
import com.jfoenix.controls.JFXHamburger;
import com.jfoenix.transitions.hamburger.HamburgerBasicCloseTransition;
import javafx.animation.TranslateTransition;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class ResizeableBorderPane extends BorderPane {
@FXML
private BorderPane root;
@FXML
private AnchorPane topAnchor;
@FXML
private AnchorPane leftAnchor;
@FXML
private AnchorPane centerAnchor;
@FXML
private AnchorPane rightAnchor;
@FXML
private AnchorPane bottomAnchor;
@FXML
private JFXHamburger hamburger;
private Stage stage;
public ResizeableBorderPane(){
try {
FXMLLoader loader = new FXMLLoader(getClass()
.getResource("/application/prototypes/custom/resizableborderpane/ResizableBorderPaneView.fxml"));
loader.setController(this);
loader.setRoot(this);
loader.load();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
root.setPrefWidth(1000);
root.setPrefHeight(800);
setStage();
}
@FXML
public void initialize() {
leftAnchor.setPrefWidth(200);
topAnchor.setPrefHeight(50);
bottomAnchor.setPrefHeight(50);
final TranslateTransition translateLeftAnchor =
new TranslateTransition(Duration.millis(1000), leftAnchor);
translateLeftAnchor.setFromX(50);
translateLeftAnchor.setToX(-200 + 50);
translateLeftAnchor.play();
HamburgerBasicCloseTransition burgerTask = new HamburgerBasicCloseTransition(hamburger);
burgerTask.setRate(-1);
hamburger.addEventHandler(MouseEvent.MOUSE_PRESSED,(e)->{
if (burgerTask.getRate() == -1){
burgerTask.setRate(burgerTask.getRate()*-1);
burgerTask.play();
translateLeftAnchor.setFromX(-200 + 50);
translateLeftAnchor.setToX(0);
translateLeftAnchor.play();
} else {
burgerTask.setRate(burgerTask.getRate()*-1);
burgerTask.play();
translateLeftAnchor.setFromX(0);
translateLeftAnchor.setToX(-200 + 50);
translateLeftAnchor.play();
}
});
translateLeftAnchor.currentTimeProperty().addListener( e -> {
System.out.println("Layout X: " + leftAnchor.getTranslateX());
});
}
// =========== GETTERS AND SETTERS ===========
public Stage getStage(){
return (Stage) root.getScene().getWindow();
}
public void setStage(){
try{
this.stage = (Stage) root.getScene().getWindow();
} catch (NullPointerException n){
System.out.println("The stage is null!");
}
}
public BorderPane getRoot() {
return root;
}
public void setRoot(BorderPane root) {
this.root = root;
}
public AnchorPane getTopAnchor() {
return topAnchor;
}
public void setTopAnchor(AnchorPane topAnchor) {
this.topAnchor = topAnchor;
}
public AnchorPane getLeftAnchor() {
return leftAnchor;
}
public void setLeftAnchor(AnchorPane leftAnchor) {
this.leftAnchor = leftAnchor;
}
public AnchorPane getCenterAnchor() {
return centerAnchor;
}
public void setCenterAnchor(AnchorPane centerAnchor) {
this.centerAnchor = centerAnchor;
}
public AnchorPane getRightAnchor() {
return rightAnchor;
}
public void setRightAnchor(AnchorPane rightAnchor) {
this.rightAnchor = rightAnchor;
}
public AnchorPane getBottomAnchor() {
return bottomAnchor;
}
public void setBottomAnchor(AnchorPane bottomAnchor) {
this.bottomAnchor = bottomAnchor;
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXHamburger?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<fx:root fx:id="root" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" type="BorderPane" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<top>
<AnchorPane fx:id="topAnchor" maxWidth="1.7976931348623157E308" style="-fx-background-color: #999999;" BorderPane.alignment="CENTER">
<children>
<JFXHamburger fx:id="hamburger" alignment="CENTER_LEFT" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="5.0" />
</children></AnchorPane>
</top>
<left>
<AnchorPane fx:id="leftAnchor" maxHeight="1.7976931348623157E308" style="-fx-background-color: #FF0000;" BorderPane.alignment="CENTER" />
</left>
<center>
<AnchorPane fx:id="centerAnchor" focusTraversable="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="200.0" minWidth="200.0" style="-fx-background-color: #454545;" BorderPane.alignment="CENTER_LEFT" />
</center>
<right>
<AnchorPane fx:id="rightAnchor" maxHeight="1.7976931348623157E308" style="-fx-background-color: #00FF00;" BorderPane.alignment="CENTER" />
</right>
<bottom>
<AnchorPane fx:id="bottomAnchor" maxWidth="1.7976931348623157E308" style="-fx-background-color: #0000FF;" BorderPane.alignment="CENTER" />
</bottom>
</fx:root>
关于如何修复空白区域有什么建议吗?我有点卡住了。
更新
通过在时间属性监听器中添加这一行,我几乎可以正常工作:
translateLeftAnchor.currentTimeProperty().addListener( e -> {
setMargin(leftAnchor, new Insets(0, leftAnchor.translateXProperty().doubleValue(), 0, 0));
});
但是,当抽屉进入时 - 它的移动速度比“中心”展开的速度要快一些;因此,可以看到中间有一条白线。
更新 2
使用翻译属性监听器,它工作正常
leftAnchor.translateXProperty().addListener( e -> {
setMargin(leftAnchor, new Insets(0, leftAnchor.translateXProperty().doubleValue(), 0, 0));
});
【问题讨论】:
-
在不使用时尝试在侧面菜单上使用
setManaged(false),这可能会将另一个块重新绘制到该空间中。 docs.oracle.com/javase/8/javafx/api/javafx/scene/… -
@Chris 感谢您的建议,但我无法让它正常工作。但是,我添加了一个有一点问题的更新,但它几乎是正确的行为。中心随着左边扩大和缩小。
-
我注意到您的最后一次编辑似乎已经解决了您的问题,我建议您将其发布为答案并为未来的用户接受它:) 弄清楚它也很好
-
@Chris 谢谢!让我先清理一下,如果一切正常,我会发布它。