4

我正在尝试围绕一个圆圈排列三角形按钮(如 3d 箭头键)。我尝试使用下面的代码通过 CSS 进行操作。但没有成功。它不会将设置应用于按钮。我检查了 Jfxtras 和 jfoenix,但找不到任何有用的东西。请问有什么想法吗?

#triangle-down {
    width: 0;
    height: 0;
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    border-top: 100px solid red;
}

在此处输入图像描述

4

1 回答 1

4

您可以使用 aStackPane来实现它。在里面添加一个圆形作为背景(或一个 ImageView),然后添加 4 个按钮。要对齐它们,您需要调用:

StackPane.setAlignment(topButton, Pos.TOP_CENTER);
StackPane.setAlignment(rightButton, Pos.CENTER_RIGHT);
StackPane.setAlignment(bottomButton, Pos.BOTTOM_CENTER);
StackPane.setAlignment(leftButton, Pos.CENTER_LEFT);

使用上面的代码,您将按钮放置到正确的位置(上、右、下和左),然后您需要更改按钮的形状,这可以使用 CSS 轻松完成-fx-shape,例如:

#arrow-button{
    -fx-background-color: -fx-mark-highlight-color, -fx-mark-color;
    -fx-background-insets: 0 0 -1 0, 0;
    -fx-padding: 0.25em;
    -fx-shape: "M 0 -3.5 v 7 l 4 -3.5 z"; 
}

现在,上面的 CSS 将创建一个指向右侧的箭头,因此您必须通过调用来旋转相应的按钮:

topButton.setRotate(270);
bottomButton.setRotate(90);
leftButton.setRotate(180);

最后,为了增加箭头的形状,您只需要设置按钮的首选大小。此外,如果您需要为按钮添加边距,您也可以通过调用:

StackPane.setMargin(topButton, new Insets(10, 0, 0, 0));
StackPane.setMargin(rightButton, new Insets(0, 10, 0, 0));
StackPane.setMargin(bottomButton, new Insets(0, 0, 10, 0));
StackPane.setMargin(leftButton, new Insets(0, 0, 0, 10));

编辑:

为了在按钮悬停或按下时应用不同的效果,您还应该添加这些 CSS 规则:

#arrow-button:hover{
    /* Example make the arrow yellow if buttos was hovered  */
    -fx-background-color: -fx-mark-highlight-color, yellow;

}

#arrow-button:pressed{
    /* And red if it was pressed, you can apply different effect 
     * like shadow , padding etc inside this rules.
     */
    -fx-background-color: -fx-mark-highlight-color, red;
}
于 2018-08-18T17:20:58.877 回答