【问题标题】:JavaFx and Floating buttonJavaFx 和浮动按钮
【发布时间】:2017-07-05 16:47:26
【问题描述】:

我在使用 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,我可以与卡片进行交互,但我无法点击按钮;否则我可以点击按钮但不能与卡片交互。

如何避免这种不良行为?

【问题讨论】:

    标签: java css javafx material-design pane


    【解决方案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);
    

    【讨论】:

    • 哇,真是个好技巧!但我希望在调整按钮大小的情况下可以越过卡片,这可能吗?
    • 我的猜测是您的 HomePane 使用的是硬编码尺寸,因此无法缩小以适应更小的窗口。与其在 HomePane 中强制每个部分的大小,不如让它们成为它们的自然大小并将它们放置在垂直的 FlowPane 中,这样窗口的大小调整将得到妥善处理。
    • 主面板是一个包含一些VBox的HBox
    猜你喜欢
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 2015-01-08
    相关资源
    最近更新 更多