如何使用 JavaFX 在警报中播放视频?
How to play a video in a alert using JavaFX?
我正在尝试使用 JavaFX 在警告对话框中播放视频。问题是我找不到如何显示视频或更多如何将其插入警报?
这是我的警报代码
MediaPlayer player = new MediaPlayer( new Media(getClass().getResource("video.mp4").toExternalForm()));
MediaView mediaView = new MediaView(player);
private void alert(){
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Don't be a fool");
alert.setHeaderText("");
alert.setContentText("Do you really think your time is correct ?");
Optional<ButtonType> result = alert.showAndWait();
}
一个Alert
extends from Dialog
, which means you can customize its DialogPane
。如果您想在警报中添加视频,最好的位置可能是对话框窗格的 content
。但请注意,设置 content
将替换 contentText
(您在示例代码中设置):
In addition to the header and content properties, there exists header text and content text properties. The way the *Text properties work is that they are a lower precedence compared to the Node properties, but they are far more convenient for developers in the common case, as it is likely the case that a developer more often than not simply wants to set a string value into the header or content areas of the DialogPane.
这意味着,如果您仍想显示 "Do you really think your time is correct?"
,您还必须将自己的 Label
添加到内容中。例如:
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Don't be a fool");
alert.setHeaderText("");
Label label = new Label("Do you really think your time is correct?");
VBox content = new VBox(10, label, mediaView);
content.setAlignment(Pos.CENTER);
alert.getDialogPane().setContent(content);
alert.setOnShowing(e -> player.play());
alert.showAndWait();
我正在尝试使用 JavaFX 在警告对话框中播放视频。问题是我找不到如何显示视频或更多如何将其插入警报?
这是我的警报代码
MediaPlayer player = new MediaPlayer( new Media(getClass().getResource("video.mp4").toExternalForm()));
MediaView mediaView = new MediaView(player);
private void alert(){
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Don't be a fool");
alert.setHeaderText("");
alert.setContentText("Do you really think your time is correct ?");
Optional<ButtonType> result = alert.showAndWait();
}
一个Alert
extends from Dialog
, which means you can customize its DialogPane
。如果您想在警报中添加视频,最好的位置可能是对话框窗格的 content
。但请注意,设置 content
将替换 contentText
(您在示例代码中设置):
In addition to the header and content properties, there exists header text and content text properties. The way the *Text properties work is that they are a lower precedence compared to the Node properties, but they are far more convenient for developers in the common case, as it is likely the case that a developer more often than not simply wants to set a string value into the header or content areas of the DialogPane.
这意味着,如果您仍想显示 "Do you really think your time is correct?"
,您还必须将自己的 Label
添加到内容中。例如:
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Don't be a fool");
alert.setHeaderText("");
Label label = new Label("Do you really think your time is correct?");
VBox content = new VBox(10, label, mediaView);
content.setAlignment(Pos.CENTER);
alert.getDialogPane().setContent(content);
alert.setOnShowing(e -> player.play());
alert.showAndWait();