JavaFX - 垂直手风琴
JavaFX - Vertical Accordion
您好,我有一个关于 JavaFX 的问题。我担心答案是剥皮,我对此一无所知,但这里有。
我想在 JavaFX 中进行 Accordion/TabPane 交叉。我会尝试用文字来解释我自己,但在下面我已经包含了我正在尝试制作的图像。
所以,我想制作一个 JavaFX 容器,左侧有一个按钮,单击该按钮时,会将不同的相关容器移到该初始容器上。再次按下时,顶部容器将移回左侧。我希望按钮和顶部容器一起从左向右移动,然后再向后移动,就好像它们彼此相连一样。
需要说明的是,两个不同的容器最好像 Accordion 那样以平滑的方式进行转换。
将 "standard" 内容和滑块的内容(包含按钮和滑块内容的 HBox
)放在 StackPane
中,并使用动画设置 translateX
以滑块移入和移出视图的方式:
@Override
public void start(Stage primaryStage) {
Button someButton = new Button("Sample content");
StackPane stackPane = new StackPane(someButton);
stackPane.setPrefSize(500, 500);
stackPane.setStyle("-fx-background-color: blue;");
Region sliderContent = new Region();
sliderContent.setPrefWidth(200);
sliderContent.setStyle("-fx-background-color: red; -fx-border-color: orange; -fx-border-width: 5;");
Button expandButton = new Button(">");
HBox slider = new HBox(sliderContent, expandButton);
slider.setAlignment(Pos.CENTER);
slider.setPrefWidth(Region.USE_COMPUTED_SIZE);
slider.setMaxWidth(Region.USE_PREF_SIZE);
// start out of view
slider.setTranslateX(-sliderContent.getPrefWidth());
StackPane.setAlignment(slider, Pos.CENTER_LEFT);
// animation for moving the slider
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(slider.translateXProperty(), -sliderContent.getPrefWidth())),
new KeyFrame(Duration.millis(500), new KeyValue(slider.translateXProperty(), 0d))
);
expandButton.setOnAction(evt -> {
// adjust the direction of play and start playing, if not already done
String text = expandButton.getText();
boolean playing = timeline.getStatus() == Animation.Status.RUNNING;
if (">".equals(text)) {
timeline.setRate(1);
if (!playing) {
timeline.playFromStart();
}
expandButton.setText("<");
} else {
timeline.setRate(-1);
if (!playing) {
timeline.playFrom("end");
}
expandButton.setText(">");
}
});
stackPane.getChildren().add(slider);
final Scene scene = new Scene(stackPane);
primaryStage.setScene(scene);
primaryStage.show();
}
您好,我有一个关于 JavaFX 的问题。我担心答案是剥皮,我对此一无所知,但这里有。
我想在 JavaFX 中进行 Accordion/TabPane 交叉。我会尝试用文字来解释我自己,但在下面我已经包含了我正在尝试制作的图像。
所以,我想制作一个 JavaFX 容器,左侧有一个按钮,单击该按钮时,会将不同的相关容器移到该初始容器上。再次按下时,顶部容器将移回左侧。我希望按钮和顶部容器一起从左向右移动,然后再向后移动,就好像它们彼此相连一样。
需要说明的是,两个不同的容器最好像 Accordion 那样以平滑的方式进行转换。
将 "standard" 内容和滑块的内容(包含按钮和滑块内容的 HBox
)放在 StackPane
中,并使用动画设置 translateX
以滑块移入和移出视图的方式:
@Override
public void start(Stage primaryStage) {
Button someButton = new Button("Sample content");
StackPane stackPane = new StackPane(someButton);
stackPane.setPrefSize(500, 500);
stackPane.setStyle("-fx-background-color: blue;");
Region sliderContent = new Region();
sliderContent.setPrefWidth(200);
sliderContent.setStyle("-fx-background-color: red; -fx-border-color: orange; -fx-border-width: 5;");
Button expandButton = new Button(">");
HBox slider = new HBox(sliderContent, expandButton);
slider.setAlignment(Pos.CENTER);
slider.setPrefWidth(Region.USE_COMPUTED_SIZE);
slider.setMaxWidth(Region.USE_PREF_SIZE);
// start out of view
slider.setTranslateX(-sliderContent.getPrefWidth());
StackPane.setAlignment(slider, Pos.CENTER_LEFT);
// animation for moving the slider
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(slider.translateXProperty(), -sliderContent.getPrefWidth())),
new KeyFrame(Duration.millis(500), new KeyValue(slider.translateXProperty(), 0d))
);
expandButton.setOnAction(evt -> {
// adjust the direction of play and start playing, if not already done
String text = expandButton.getText();
boolean playing = timeline.getStatus() == Animation.Status.RUNNING;
if (">".equals(text)) {
timeline.setRate(1);
if (!playing) {
timeline.playFromStart();
}
expandButton.setText("<");
} else {
timeline.setRate(-1);
if (!playing) {
timeline.playFrom("end");
}
expandButton.setText(">");
}
});
stackPane.getChildren().add(slider);
final Scene scene = new Scene(stackPane);
primaryStage.setScene(scene);
primaryStage.show();
}