簡體   English   中英

JavaFX 在沒有外部庫的情況下創建彈出窗口

[英]JavaFX create popover without external libraries

如何從javafx.scene.control.DatePicker創建一個像DatePicker類中的彈出javafx.scene.control.DatePicker如下所示:

在此處輸入圖片說明

彈出框在顯示時應位於所有其他組件的頂部,如此處所示(彈出框位於TextField上方):

在此處輸入圖片說明

為我的問題找到了一個非常簡單的解決方案,這是一個代碼片段,以防人們遇到同樣的問題

package main;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Label;
import javafx.scene.control.MenuButton;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        BorderPane rootPane = new BorderPane();
        MenuButton openButton = new MenuButton("Open Context Menu");
        BorderPane contentPane = new BorderPane();
        CustomMenuItem item = new CustomMenuItem(contentPane);
        openButton.setStyle("-fx-selection-bar: transparent;"); //this is optional. it makes the blue background that appears when something is focused transparent
        contentPane.setPrefSize(300, 300);
        Label text = new Label("The ContextMenu will only close when you click the\nbutton below OR click outside of the ContextMenu.\nHow neat is that?");
        text.setStyle(" -fx-text-fill: -fx-text-base-color;"); //needs to bet set if you want the selection-bar to be transparent. if not set the text will become invisible
        contentPane.setTop(text);
        Button closeButton = new Button("Close this popover");
        closeButton.setOnAction(x -> {
            openButton.hide();
        });
        contentPane.setBottom(closeButton);
        item.setHideOnClick(false); // this will stop the ContextMenu from being hidden when clicking inside of it.
        openButton.getItems().add(item);
        rootPane.setCenter(openButton);
        Scene scene = new Scene(rootPane, 550, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

我只是放在Pane與所有我的一個內容里面的CustomMenuItem並補充說, CustomMenuItem到我的MenuButton

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM