在JavaFX中添加到HBox时如何滑动节点?

佩德罗·科雷亚(Pedro Correia)

我正在创建一个HBox包含2个或3个孩子的面板第一个孩子是VBox带有图标的,并停留在屏幕的左侧,当我将鼠标悬停在VBox(第一个孩子)上时,我想添加第二个VBox带有按钮的孩子我的第三个孩子是AnchorPane支持我的内容的。

我的问题是,如何将我的第二次补充VBoxHBox与过渡幻灯片(从左至右)?

隐藏我的第二个孩子(VBox带按钮)的最终目的是增加我的第三个孩子的宽度内容(content AnchorPane);

范例程式码

public class Test extends Application {

    @Override
    public void start(Stage primaryStage) {

        HBox root = new HBox();

        Scene scene = new Scene(root, 300, 250);
        VBox c1 = new VBox();
        ImageView i1 = new ImageView(new Image(getClass().getResourceAsStream("home/home.png")));
        ImageView i2 = new ImageView(new Image(getClass().getResourceAsStream("home/contactos.png")));
        ImageView i3 = new ImageView(new Image(getClass().getResourceAsStream("home/info.png")));
        c1.getChildren().addAll(i1, i2, i3);
        VBox c2 = new VBox();
        Button b1 = new Button("home opção1");
        Button b2 = new Button("home opção2");
        c2.getChildren().addAll(b1, b2);
        AnchorPane c3 = new AnchorPane();

        // Set backgrounds
        c1.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY)));
        c2.setBackground(new Background(new BackgroundFill(Color.GRAY, CornerRadii.EMPTY, Insets.EMPTY)));
        c3.setBackground(new Background(new BackgroundFill(Color.rgb(255,255,148), CornerRadii.EMPTY, Insets.EMPTY)));
        root.getChildren().addAll(c1, c3);
        c1.setOnMouseEntered(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                root.getChildren().add(1, c2);
                // Fault transation slide
            }
        });

        c1.setOnMouseExited(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                root.getChildren().remove(1);
            }
        });

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
西丹丹

不幸的是,没有预定义的方法可以在JavaFX中获得滑动效果。您可以通过了解/实现裁剪的概念来获得这种效果。在内部,相同的概念用于需要裁剪内容的ScrollPane和其他控件。

这个想法是,我们裁剪要滑动的布局,并使用时间轴逐渐更改其值。关于此的更详细的解释可以在我的博客中找到(我在2012年初写了这个博客,所以代码可能看起来有些过时了;)但是您应该明白这个概念)

我迅速按照您的要求制作了以下演示。这应该可以帮助您了解一些想法。

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.stream.Stream;


public class SlideMenuDemo extends Application {
    /* THE ONLY DRAWBACK OF THIS APPROACH IS YOU HAVE TO KNOW THE WIDTH OF THE SUBMENU AHEAD*/
    double subMenuWidth = 140;

    HBox root;
    VBox subMenu;
    VBox menuPane;
    StackPane subMenuPane;

    private Rectangle clipRect;
    private Timeline timelineHide;
    private Timeline timelineShow;

    private final Color[] colors = {Color.RED, Color.BLUE, Color.GREEN, Color.LAVENDER, Color.PINK};
    private final String[] shapes = {"cirlce", "triangle", "square", "rectangle"};

    @Override
    public void start(Stage primaryStage) {
        root = new HBox();
        Scene scene = new Scene(root, 400, 300);
        primaryStage.setTitle("Sliding Demo");
        primaryStage.setScene(scene);
        primaryStage.show();

        // Main menu pane
        menuPane = new VBox();
        menuPane.setStyle("-fx-background-color:#DDDDDD;");
        Stream.of(shapes).forEach(shape -> menuPane.getChildren().addAll(buildMenuButton(shape, Color.BLACK, null)));

        // Sub menu pane
        subMenu = new VBox();
        subMenuPane = new StackPane(subMenu);
        subMenuPane.setMinWidth(0);
        subMenuPane.setPrefWidth(0);

        StackPane subMenuContainer = new StackPane(subMenuPane);
        subMenuContainer.setStyle("-fx-background-color:#AAAAAA");
        HBox menuBox = new HBox(menuPane, subMenuContainer);
        menuBox.setOnMouseExited(e -> hideSubMenu());

        // Content Pane
        StackPane contentPane = new StackPane(new Text("Hello Slide Checking"));
        contentPane.setAlignment(Pos.TOP_LEFT);
        contentPane.setPadding(new Insets(15));
        HBox.setHgrow(contentPane, Priority.ALWAYS);
        contentPane.setStyle("-fx-background-color:#0000FF70,#FFFFFF;-fx-background-insets:0,1;");

        root.getChildren().addAll(menuBox, contentPane);
        setAnimation();
    }

    private void hideSubMenu() {
        timelineHide.play();
    }

    private void showSubMenu() {
        timelineShow.play();
    }

    private void setAnimation() {
        clipRect = new Rectangle();
        clipRect.setWidth(0);
        clipRect.heightProperty().bind(root.heightProperty());
        clipRect.translateXProperty().set(subMenuWidth);
        subMenuPane.setClip(clipRect);
        subMenuPane.translateXProperty().set(-subMenuWidth);

        /* Event handler hide is finished. */
        EventHandler<ActionEvent> onFinished = e -> {
            menuPane.getChildren().stream().forEach(n -> n.setStyle(null));
            subMenu.getChildren().clear();
        };

        timelineShow = new Timeline();
        timelineHide = new Timeline();

        /* Animation for show. */
        timelineShow.setCycleCount(1);
        final KeyValue kvDwn1a = new KeyValue(clipRect.widthProperty(), subMenuWidth);
        final KeyValue kvDwn1b = new KeyValue(subMenuPane.prefWidthProperty(), subMenuWidth);
        final KeyValue kvDwn1c = new KeyValue(subMenuPane.minWidthProperty(), subMenuWidth);
        final KeyValue kvDwn2 = new KeyValue(clipRect.translateXProperty(), 0);
        final KeyValue kvDwn3 = new KeyValue(subMenuPane.translateXProperty(), 0);
        final KeyFrame kfDwn = new KeyFrame(Duration.millis(200), kvDwn1a, kvDwn1b, kvDwn1c, kvDwn2, kvDwn3);
        timelineShow.getKeyFrames().add(kfDwn);

        /* Animation for hide. */
        timelineHide.setCycleCount(1);
        final KeyValue kvUp1a = new KeyValue(clipRect.widthProperty(), 0);
        final KeyValue kvUp1b = new KeyValue(subMenuPane.prefWidthProperty(), 0);
        final KeyValue kvUp1c = new KeyValue(subMenuPane.minWidthProperty(), 0);
        final KeyValue kvUp2 = new KeyValue(clipRect.translateXProperty(), subMenuWidth);
        final KeyValue kvUp3 = new KeyValue(subMenuPane.translateXProperty(), -subMenuWidth);
        final KeyFrame kfUp = new KeyFrame(Duration.millis(200), onFinished, kvUp1a, kvUp1b, kvUp1c, kvUp2, kvUp3);
        timelineHide.getKeyFrames().add(kfUp);
    }

    private StackPane buildMenuButton(String type, Color color, String text) {
        double size = 50;
        double sSize = (size / 5) * 4;
        double hSize = sSize / 2;
        StackPane menuButton = new StackPane();
        menuButton.setPadding(new Insets(0, 5, 0, 5));
        menuButton.setMaxHeight(size);
        menuButton.setMinHeight(size);

        Node shape = null;
        switch (type) {
            case "triangle":
                StackPane s = new StackPane();
                s.setPrefSize(sSize, sSize);
                s.setMaxSize(sSize, sSize);
                s.setBackground(new Background(new BackgroundFill(color, CornerRadii.EMPTY, Insets.EMPTY)));
                s.setStyle("-fx-shape:\"M0 1 L1 1 L.5 0 Z\";");
                s.setPadding(new Insets(hSize));
                shape = s;
                break;
            case "square":
                shape = new Rectangle(sSize, sSize, color);
                break;
            case "rectangle":
                shape = new Rectangle(sSize, hSize, color);
                break;
            default:
                shape = new Circle(hSize, color);
                break;
        }
        HBox hb = new HBox(shape);
        hb.setAlignment(Pos.CENTER_LEFT);
        if (text != null) {

            hb.setSpacing(10);
            hb.getChildren().add(new Label(text));
        }
        menuButton.getChildren().add(hb);

        if (text == null) {
            // Main menu button
            menuButton.setOnMouseEntered(e -> {
                menuPane.getChildren().stream().forEach(n -> n.setStyle(null));
                subMenu.getChildren().clear();
                menuButton.setStyle("-fx-background-color:#AAAAAA;");
                Stream.of(colors).forEach(c -> subMenu.getChildren().addAll(buildMenuButton(type, c, c.toString())));
                if (subMenuPane.getWidth() == 0) {
                    showSubMenu();
                }
            });
        } else {
            // Sub menu button
            menuButton.setPrefWidth(subMenuWidth);
            menuButton.setMinWidth(subMenuWidth);
            menuButton.setOnMouseEntered(e -> menuButton.setStyle("-fx-background-color:#777777;"));
            menuButton.setOnMouseExited(e -> menuButton.setStyle(null));
        }
        return menuButton;
    }

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将常规JavaFX节点添加到FXML中

将元素添加到树中时如何自动扩展Webix树节点

如何约束代码中添加到GridPane的节点?

如何JavaFX运行添加到Eclipse的Java中11?

节点可以在运行时添加到 HBox 吗?

将图像添加到按钮时,如何禁用UIButton的标题向右滑动?

查找TreeItem并将其添加到JavaFX中的现有节点

如何将选项卡添加到ActionBar中以进行滑动选项卡?

如何将上下滑动动画添加到可变高度div中?

如何在Swift 3中将滑动手势添加到AVPlayer

JavaFX使用SceneBuilder将节点添加到ScrollPane

在JavaFX中将BlendMode添加到裁剪的节点

如何通过节点js将子节点添加到现有json文件中?

如何使用python将嵌套的子节点添加到xml文档中的父节点?

无法将ImageButton添加到“滑动菜单抽屉”中

将节点等级添加到networkx中的节点标签

如何在 angularfire 2 版本 5 中向添加到节点的对象添加键

尝试将节点添加到 Kubernetes 集群时如何解决超时问题?

将新节点添加到群集时,如何避免查询花费很长时间

将节点添加到C中的链表时,EXC_BAD访问

在SpriteKit中添加到父级时未显示未归档节点

不使用构建工具时如何将 JavaFX javadocs 添加到 vscode

在JavaFX中创建一个自定义节点并将其添加到布局中

在Python中将节点添加到AST时,函数未添加到新行

如何通过在结构中的名称上使用 strcmp 将节点添加到列表中?

在 GoJS 中如何获取添加到图表中的节点的键?

如何打印已添加到队列中的二叉树中节点的值?

将节点添加到链表时出现分段错误

如何在Java 1.4中将属性添加到XML节点