如何在javafx中创建弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22166610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a popup windows in javafx
提问by Maulik Patel
I want to create a popup windows in javafx application.give me some idea.
我想在 javafx 应用程序中创建一个弹出窗口。给我一些想法。
when i click on check button open popup windows. how to do?
当我点击检查按钮打开弹出窗口。怎么做?
回答by ItachiUchiha
You can either create a new Stage
, add your controls into it or if you require the POPUP as Dialog
box, then you may consider using DialogsFXor ControlsFX(Requires JavaFX8)
您可以创建一个新的Stage
,将您的控件添加到其中,或者如果您需要 POPUP 作为Dialog
框,那么您可以考虑使用DialogsFX或ControlsFX(需要 JavaFX8)
For creating a new Stage, you can use the following snippet
要创建新舞台,您可以使用以下代码段
@Override
public void start(final Stage primaryStage) {
Button btn = new Button();
btn.setText("Open Dialog");
btn.setOnAction(
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
final Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initOwner(primaryStage);
VBox dialogVbox = new VBox(20);
dialogVbox.getChildren().add(new Text("This is a Dialog"));
Scene dialogScene = new Scene(dialogVbox, 300, 200);
dialog.setScene(dialogScene);
dialog.show();
}
});
}
If you don't want it to be modal
(block other windows), use:
如果您不希望它成为modal
(阻止其他窗口),请使用:
dialog.initModality(Modality.NONE);
回答by broot02
Have you looked into ControlsFx Popover control.
你有没有研究过ControlsFx Popover control。
import org.controlsfx.control.PopOver;
import org.controlsfx.control.PopOver.ArrowLocation;
private PopOver item;
final Scene scene = addItemButton.getScene();
final Point2D windowCoord = new Point2D(scene.getWindow()
.getX(), scene.getWindow().getY());
final Point2D sceneCoord = new Point2D(scene.getX(), scene.
getY());
final Point2D nodeCoord = addItemButton.localToScene(0.0,
0.0);
final double clickX = Math.round(windowCoord.getX()
+ sceneCoord.getY() + nodeCoord.getX());
final double clickY = Math.round(windowCoord.getY()
+ sceneCoord.getY() + nodeCoord.getY());
item.setContentNode(addItemScreen);
item.setArrowLocation(ArrowLocation.BOTTOM_LEFT);
item.setCornerRadius(4);
item.setDetachedTitle("Add New Item");
item.show(addItemButton.getParent(), clickX, clickY);
This is only an example but a PopOver sounds like it could accomplish what you want. Check out the documentationfor more info.
这只是一个例子,但 PopOver 听起来可以完成你想要的。查看文档以获取更多信息。
Important note: ControlsFX will only work on JavaFX 8.0 b118 or later.
重要说明:ControlsFX 仅适用于 JavaFX 8.0 b118 或更高版本。
回答by Sudip Saha
Take a look at jfxmessagebox (http://en.sourceforge.jp/projects/jfxmessagebox/) if you are looking for very simple dialog popups.
如果您正在寻找非常简单的对话框弹出窗口,请查看 jfxmessagebox ( http://en.sourceforge.jp/projects/jfxmessagebox/)。
回答by RyanR
The Popup class might be better than the Stage class, depending on what you want. Stage is either modal (you can't click on anything else in your app) or it vanishes if you click elsewhere in your app (because it's a separate window). Popup stays on top but is not modal.
根据您的需要,Popup 类可能比 Stage 类更好。Stage 要么是模态的(您不能单击应用程序中的任何其他内容),要么如果您单击应用程序中的其他地方(因为它是一个单独的窗口),它就会消失。弹出窗口保持在顶部,但不是模态的。
See this Popup Windowexample.
请参阅此弹出窗口示例。