JavaFX显示节点周围的选项

时间:2016-07-06 15:43:14

标签: javafx contextmenu

我试图实现一个自定义上下文菜单,菜单项将出现在节点周围,其思路如下,在目标节点周围绘制一个虚拟圆圈,并计算圆周长,第一个菜单项将是在目标节点的顶部,其余将分布在每个菜单项之间相等的距离,,,,我没有写任何代码行,因为我不知道如何开始至少,,, PLZ帮助?

也许这张图片会更好地解释

enter image description here

1 个答案:

答案 0 :(得分:0)

如果有人感兴趣,我会这样做,但代码有点脏:

import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
     * @author Asendar
 *
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class RadialMenuFactory {
    public static final RadialMenuFactory instance = new RadialMenuFactory();

    public StackPane construct(int itemsCount, int radius) {
        StackPane pane = new StackPane();

        double pheta = 270;

        int counter = 0;

        while (counter != itemsCount) {
            double x = radius * Math.cos(Math.toRadians(pheta));
            double y = radius * Math.sin(Math.toRadians(pheta));

            Button btn = new Button("btn");

            btn.setTranslateX(x);
            btn.setTranslateY(y);

            pane.getChildren().addAll(btn);

            pheta += 360 / itemsCount;
            pheta %= 360;

            counter++;
        }

        pane.setMinHeight(300);
        pane.setMinWidth(300);

        return pane;
    }

}