JavaFX创建没有外部库的popover [英] JavaFX create popover without external libraries
本文介绍了JavaFX创建没有外部库的popover的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从 javafx.scene.control.DatePicker
中创建一个类似于 DatePicker
类的弹出窗口这里:
How can I create a popover like in the DatePicker
class from javafx.scene.control.DatePicker
as seen here:
弹出窗口应显示为在这里看到的所有其他组件之上(popover在 TextField
之上):
The popover should when displayed be on top of all the other components as seen here (the popover is above the TextField
):
推荐答案
找到一个非常简单的解决方案来解决我的问题,这里有一个代码片段,以防万一人们遇到同样的问题
Found a pretty simple solution to my problem, here is a code snippet in case people encounter the same problem
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();
}
}
我刚放了窗格
我的所有内容都在 CustomMenuItem
中,并将 CustomMenuItem
添加到我的 MenuButton
。
I just placed a Pane
with all of my content inside of a CustomMenuItem
and added that CustomMenuItem
to my MenuButton
.
这篇关于JavaFX创建没有外部库的popover的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文