gpt4 book ai didi

java - 如何为 "drawer"菜单制作自动调整大小的边框 Pane

转载 作者:行者123 更新时间:2023-11-29 04:12:11 25 4
gpt4 key购买 nike

我将 BorderPane 作为我的根。左侧包含滑出式菜单,中间有容器来存放组件。当滑出式菜单隐藏并相对时,我想将主容器的长度拉伸(stretch)到舞台的宽度。

我试图通过 ScaleTransition 缩放我的 BorderPane mainContent,但它出错了。ScaleTransition 只是拉伸(stretch),不设置宽度...

enter image description here enter image description here enter image description here

@Override
public void start(Stage primaryStage) throws Exception{
Label label = new Label("->");
label.setStyle("-fx-text-fill: orange; -fx-background-color: black");

VBox content = new VBox();
content.setStyle("-fx-background-color: purple");

BorderPane.setAlignment(label, Pos.CENTER);
BorderPane menu = new BorderPane(content,null,label,null,null);

menu.setMaxWidth(250);
menu.setPrefWidth(250);
menu.setTranslateX(-230);
menu.setStyle("-fx-background-color: yellow");

TranslateTransition menuTranslation = new TranslateTransition(Duration.millis(500), menu);
menuTranslation.setFromX(-230);
menuTranslation.setToX(0);

BorderPane mainContent = new BorderPane(new Button("OKEY"),new Button("OKEY"),new Button("OKEY"),new Button("OKEY"),new Button("OKEY"));
mainContent.setStyle("-fx-background-color: cyan");

ScaleTransition mainContenTransition = new ScaleTransition(Duration.millis(500), mainContent);
//TranslateTransition mainContenTranslation = new TranslateTransition(Duration.millis(500), mainContent);

ParallelTransition parallelTransition = new ParallelTransition();
parallelTransition.getChildren().addAll(menuTranslation, mainContenTransition);

menu.setOnMouseEntered(evt -> {
mainContenTransition.setFromX(mainContent.getWidth());
mainContenTransition.setToX(primaryStage.getWidth());
parallelTransition.setRate(1);
parallelTransition.play();
});
menu.setOnMouseExited(evt -> {
mainContenTransition.setFromX(mainContent.getWidth());
mainContenTransition.setToX(primaryStage.getWidth());
parallelTransition.setRate(-1);
parallelTransition.play();
});

mainContent.setStyle("-fx-background-color: cyan");
BorderPane root = new BorderPane(mainContent, null,null,null, menu);
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 800, 600));



primaryStage.show();
//mainContent.setScaleX(primaryStage.getWidth());
}

最佳答案

建议的解决方案使用 Timeline 来插入内容的首选宽度,让布局完成其余的工作:

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class SideDrawerAnimation extends Application {

private static final int MENU_CONTENT_WIDTH = 230, MENU_MIN_WIDTH = 20, H = 400, W = 600;
private static final double SPEED = 2;
private VBox content;
private Timeline openMenu, closeMenu;

@Override
public void start(Stage primaryStage) throws Exception{

Label label = new Label("->");
label.setMinWidth(MENU_MIN_WIDTH);
label.setPrefWidth(MENU_MIN_WIDTH);
label.setStyle("-fx-text-fill: orange; -fx-background-color: black");

content = new VBox();
content.setPrefWidth(0);
content.setStyle("-fx-background-color: purple");

HBox menu = new HBox(content,label);
menu.setAlignment(Pos.CENTER);
menu.setMaxWidth(MENU_CONTENT_WIDTH+ MENU_MIN_WIDTH);
menu.prefWidthProperty().bind(content.prefWidthProperty().add(label.prefWidthProperty()));
menu.setStyle("-fx-background-color: yellow");

BorderPane mainContent = new BorderPane(new Label("CENTER"),new Label("TOP"),new Label("RIGHT"),
new Label("BOTTOM"),new Label("LEFT"));
mainContent.setStyle("-fx-background-color: cyan");

openMenu = new Timeline(
new KeyFrame(Duration.millis(SPEED), event -> setMenuContentPrefSize(openMenu,1))
);
openMenu.setCycleCount(Timeline.INDEFINITE);

closeMenu = new Timeline(
new KeyFrame(Duration.millis(SPEED), event -> setMenuContentPrefSize(closeMenu, -1))
);
closeMenu.setCycleCount(Timeline.INDEFINITE);

menu.setOnMouseEntered(evt -> {
closeMenu.stop(); openMenu.play();
});
menu.setOnMouseExited(evt -> {
openMenu.stop(); closeMenu.play();
});

HBox root = new HBox(menu, mainContent);
HBox.setHgrow(mainContent, Priority.ALWAYS);

Scene scene = new Scene(root, W, H);
primaryStage.setScene(scene);
primaryStage.show();
}

private void setMenuContentPrefSize(Timeline timeline, int i) {

double width = content.getPrefWidth();

if(width > MENU_CONTENT_WIDTH){
timeline.stop();
content.setPrefWidth(MENU_CONTENT_WIDTH);

} else if (width < 0){
timeline.stop();
content.setPrefWidth(0);
} else {
content.setPrefWidth(width +i);
}
}

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

enter image description here

关于java - 如何为 "drawer"菜单制作自动调整大小的边框 Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54489721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com