如何在JavaFX 2.0中创建和显示常见对话框(错误、警告、确认)?我找不到任何像Dialog
、DialogBox
、Message
之类的“标准”类。
发布于 2012-10-04 07:04:03
Sergey是正确的,但是如果您需要在调用它的相同代码块中从您的本地对话框中获取响应以进行评估,则应该使用.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();
}
}
发布于 2015-06-10 22:07:31
改编自这里的答案:https://stackoverflow.com/a/7505528/921224
javafx.scene.control.Alert
有关如何使用JavaFX对话框的深入描述,请参阅:JavaFX Dialogs (official) by code.makery。它们比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是一个单线程图形用户界面工具包,这意味着应该直接从JavaFX应用程序线程调用此方法。如果你有另一个线程在工作,它需要一个对话框,那么可以看看下面这些:JavaFX2: Can I pause a background Task / Service?和Platform.Runlater and Task Javafx。
要使用此方法调用:
ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE");
或
ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE", "HEADER MESSAGE");
发布于 2016-04-29 20:34:37
从java 8u40开始:
Alert alert = new Alert(AlertType.INFORMATION, "Content here", ButtonType.OK);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.show();
https://stackoverflow.com/questions/8309981
复制相似问题