在Java FX中添加菜单到按钮顶部

时间:2017-09-10 18:38:05

标签: java button javafx

我试图在JavaFX按钮中获得具有任意内容的下拉菜单的效果。以下是我到目前为止的情况:

// I'm using a popup, but that isn't owned by the main window
button1.setOnMouseClicked(e -> {
    final Stage dialog = new Stage();
    dialog.setAlwaysOnTop(true);
    dialog.initModality(Modality.NONE);
    dialog.initStyle(StageStyle.UNDECORATED);
    try {
        VBox popup = (VBox) FXMLLoader.load(Popup.class.getResource("Popup.fxml"));
        Scene scene = new Scene(popup, 50, 100);
        Bounds boundsInScreen = button1.localToScreen(button1.getBoundsInLocal());
        dialog.setX(boundsInScreen.getMinX());
        dialog.setY(boundsInScreen.getMinY() - 100); // This isn't dynamic, so it's bad
        dialog.setScene(scene);
        dialog.show();
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
});

我希望有一种很好的方式来捆绑按钮及其相关的弹出窗口(基本上是任意形式),我不必跟踪弹出窗口的大小。< / p>

0 个答案:

没有答案