JavaFx和浮动按钮

时间:2017-07-05 16:47:26

标签: java css javafx material-design pane

我在使用JavaFX制作的GUI时遇到问题,你可以看到gui here

正如你所看到的那样,在GUI的右下角有一个带卡片的中心窗格和一个动作按钮,我用这段代码实现了这个效果

public class MainFrame extends BorderPane{         

private Header header   = new Header();
private Sidebar sidebar = new Sidebar();   
private GhostAbsolutePane ghostPaneWithPlay = new GhostAbsolutePane();   
private HomePane homePane                      = new HomePane();

private final StackPane mainPane = new StackPane();

   public MainFrame() {         
        mainPane.getStyleClass().add("main-stack-pane");
        mainPane.getChildren().addAll(homePane, ghostPaneWithPlay);            

        this.setTop(header);
        this.setLeft(sidebar);
        this.setCenter(mainPane);                
    }                                  
}

我用一个StackPane填充了MainFrame的中心,其中第一个子窗口包含一个带有GridBagLayout中的卡片的窗格,第二个子窗口是一个像这样的通用窗格

public class GhostAbsolutePane extends Region{

    private Button run = new Button("");

    private double diameter = 56;

    public GhostAbsolutePane() {

        run.setMinWidth(diameter);
        run.setMaxWidth(diameter);

        run.setMinHeight(diameter);
        run.setMaxHeight(diameter);

        this.getChildren().add(run);

        this.setMouseTransparent(true);

        this.widthProperty().addListener((obs, oldVal, newVal)->{
            onResize();
        });

        this.heightProperty().addListener((obs, oldVal, newVal)->{
            onResize();
        }); 

    }

    private void onResize(){
        double h = getHeight();
        double w = getWidth();

        run.setTranslateX(w-diameter);
        run.setTranslateY(h-diameter);
    }        
}

问题很简单。如果我将ghostPaneWithPlay setMouseTransparent属性设置为true,我可以与卡片进行互动,但我无法点按此按钮;否则我可以按下按钮但不能与卡片互动。

如何避免这种不受欢迎的行为?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您正试图在右下角放置“播放”按钮(带有▶的圆形按钮)。

您不应该使用StackPane。只需使用BorderPane,让它处理定位:

private final BorderPane mainPane = new BorderPane();

// ...

    Button playButton = new Button("\u25b6");
    playButton.getStyleClass().add("play");

    mainPane.setCenter(homePane);
    HBox buttonPane = new HBox(12, playButton);
    buttonPane.setAlignment(Pos.CENTER_RIGHT);
    mainPane.setBottom(buttonPane);