如何在 JavaFX 2.0 中创建和显示常见对话框(错误、警告、确认)?我找不到任何“标准”类Dialog
,例如DialogBox
,Message
或其他东西。
10 回答
最近发布的 JDK 1.8.0_40 添加了对 JavaFX 对话框、警报等的支持。例如,要显示确认对话框,可以使用 Alert 类:
Alert alert = new Alert(AlertType.CONFIRMATION, "Delete " + selection + " ?", ButtonType.YES, ButtonType.NO, ButtonType.CANCEL);
alert.showAndWait();
if (alert.getResult() == ButtonType.YES) {
//do stuff
}
这是此版本中添加的类的列表:
编辑:对话框支持已添加到 JavaFX,请参阅https://stackoverflow.com/a/28887273/1054140
2011 年没有通用对话框支持。您必须自己编写new Stage()
:
Stage dialogStage = new Stage();
dialogStage.initModality(Modality.WINDOW_MODAL);
VBox vbox = new VBox(new Text("Hi"), new Button("Ok."));
vbox.setAlignment(Pos.CENTER);
vbox.setPadding(new Insets(15));
dialogStage.setScene(new Scene(vbox));
dialogStage.show();
更新
作为RT-12643实现的一部分,官方标准对话框将在 8u40 版中加入 JavaFX 。这些应该在 2015 年 3 月左右以最终版本形式提供,现在在 JavaFX 开发存储库中以源代码形式提供。
同时,您可以使用下面的 ControlsFX 解决方案...
ControlsFX是事实上的标准第 3 方库,用于 JavaFX 中的常见对话框支持(错误、警告、确认等)。
如其他答案中所指出的,还有许多其他 3rd 方库可提供通用对话框支持,您可以使用 Sergey 答案中的示例代码轻松创建自己的对话框。
但是,我相信ControlsFX很容易提供目前可用的最佳质量标准 JavaFX 对话框。以下是ControlsFX 文档中的一些示例。
Sergey 是正确的,但是如果你需要从你的 home-spun 对话框中获得响应以在调用它的同一代码块中进行评估,你应该使用 .showAndWait(),而不是 .show()。这是我对 Swing 的 OptionPane 中提供的几种对话框类型的演绎:
public class FXOptionPane {
public enum Response { NO, YES, CANCEL };
private static Response buttonSelected = Response.CANCEL;
private static ImageView icon = new ImageView();
static class Dialog extends Stage {
public Dialog( String title, Stage owner, Scene scene, String iconFile ) {
setTitle( title );
initStyle( StageStyle.UTILITY );
initModality( Modality.APPLICATION_MODAL );
initOwner( owner );
setResizable( false );
setScene( scene );
icon.setImage( new Image( getClass().getResourceAsStream( iconFile ) ) );
}
public void showDialog() {
sizeToScene();
centerOnScreen();
showAndWait();
}
}
static class Message extends Text {
public Message( String msg ) {
super( msg );
setWrappingWidth( 250 );
}
}
public static Response showConfirmDialog( Stage owner, String message, String title ) {
VBox vb = new VBox();
Scene scene = new Scene( vb );
final Dialog dial = new Dialog( title, owner, scene, "res/Confirm.png" );
vb.setPadding( new Inset(10,10,10,10) );
vb.setSpacing( 10 );
Button yesButton = new Button( "Yes" );
yesButton.setOnAction( new EventHandler<ActionEvent>() {
@Override public void handle( ActionEvent e ) {
dial.close();
buttonSelected = Response.YES;
}
} );
Button noButton = new Button( "No" );
noButton.setOnAction( new EventHandler<ActionEvent>() {
@Override public void handle( ActionEvent e ) {
dial.close();
buttonSelected = Response.NO;
}
} );
BorderPane bp = new BorderPane();
HBox buttons = new HBox();
buttons.setAlignment( Pos.CENTER );
buttons.setSpacing( 10 );
buttons.getChildren().addAll( yesButton, noButton );
bp.setCenter( buttons );
HBox msg = new HBox();
msg.setSpacing( 5 );
msg.getChildren().addAll( icon, new Message( message ) );
vb.getChildren().addAll( msg, bp );
dial.showDialog();
return buttonSelected;
}
public static void showMessageDialog( Stage owner, String message, String title ) {
showMessageDialog( owner, new Message( message ), title );
}
public static void showMessageDialog( Stage owner, Node message, String title ) {
VBox vb = new VBox();
Scene scene = new Scene( vb );
final Dialog dial = new Dialog( title, owner, scene, "res/Info.png" );
vb.setPadding( new Inset(10,10,10,10) );
vb.setSpacing( 10 );
Button okButton = new Button( "OK" );
okButton.setAlignment( Pos.CENTER );
okButton.setOnAction( new EventHandler<ActionEvent>() {
@Override public void handle( ActionEvent e ) {
dial.close();
}
} );
BorderPane bp = new BorderPane();
bp.setCenter( okButton );
HBox msg = new HBox();
msg.setSpacing( 5 );
msg.getChildren().addAll( icon, message );
vb.getChildren().addAll( msg, bp );
dial.showDialog();
}
}
改编自这里的答案:https ://stackoverflow.com/a/7505528/921224
javafx.scene.control.Alert
有关如何使用 JavaFX 对话框的深入描述,请参阅: code.makery 的JavaFX 对话框(官方)。它们比 Swing 对话框更强大、更灵活,而且功能远不止弹出消息。
import javafx.scene.control.Alert
import javafx.scene.control.Alert.AlertType;
import javafx.application.Platform;
public class ClassNameHere
{
public static void infoBox(String infoMessage, String titleBar)
{
/* By specifying a null headerMessage String, we cause the dialog to
not have a header */
infoBox(infoMessage, titleBar, null);
}
public static void infoBox(String infoMessage, String titleBar, String headerMessage)
{
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(titleBar);
alert.setHeaderText(headerMessage);
alert.setContentText(infoMessage);
alert.showAndWait();
}
}
要记住的一件事是 JavaFX 是一个单线程 GUI 工具包,这意味着应该直接从 JavaFX 应用程序线程调用此方法。如果您有另一个线程在工作,需要一个对话框,请查看这些 SO Q&As:JavaFX2:我可以暂停后台任务/服务吗?和Platform.Runlater 和 Task Javafx。
要使用此方法调用:
ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE");
或者
ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE", "HEADER MESSAGE");
这从 java 8u40 开始有效:
Alert alert = new Alert(AlertType.INFORMATION, "Content here", ButtonType.OK);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.show();
更新: JavaFX 8u40 包括简单的对话框和警报!查看这篇博客文章,它解释了如何使用官方 JavaFX 对话框!
您可以给出由JavaFX UI Controls Project给出的对话框。我想它会帮助你
Dialogs.showErrorDialog(Stage object, errorMessage, "Main line", "Name of Dialog box");
Dialogs.showWarningDialog(Stage object, errorMessage, "Main line", "Name of Dialog box");
public myClass{
private Stage dialogStage;
public void msgBox(String title){
dialogStage = new Stage();
GridPane grd_pan = new GridPane();
grd_pan.setAlignment(Pos.CENTER);
grd_pan.setHgap(10);
grd_pan.setVgap(10);//pading
Scene scene =new Scene(grd_pan,300,150);
dialogStage.setScene(scene);
dialogStage.setTitle("alert");
dialogStage.initModality(Modality.WINDOW_MODAL);
Label lab_alert= new Label(title);
grd_pan.add(lab_alert, 0, 1);
Button btn_ok = new Button("fermer");
btn_ok.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
// TODO Auto-generated method stub
dialogStage.hide();
}
});
grd_pan.add(btn_ok, 0, 2);
dialogStage.show();
}
}
要使Clairton Luz的示例 正常工作,您需要在代码片段中运行FXApplicationThread
and insert into方法:Platform.runLater
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error Dialog");
alert.setHeaderText("No information.");
alert.showAndWait();
}
);
否则,你会得到: java.lang.IllegalStateException: Not on FX application thread