您好我有应用程序提醒,在用户没有回复后我想刷新提醒/再次显示它,由于某种原因,我没有看到第二个警告它是空的,如:
我正在关闭警报,如果它显示,所以我不知道为什么下一个警报是空的。 包裹申请;
//imports
public class Main extends Application{
int number = 50;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
alertMethod();
}
private void alertMethod() {
number += number + 10;
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Title");
alert.setHeaderText("Number " + number);
alert.setContentText("Choose your option.");
ButtonType buttonTypeOne = new ButtonType("Yes");
ButtonType buttonTypeCancel = new ButtonType("No", ButtonBar.ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);
Thread newThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
Platform.runLater(new Runnable() {
@Override
public void run() {
if(alert.isShowing()) {
alert.close();
}
alertMethod();
}
});
}
});
newThread.start();
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne){
System.out.println("Pressed Yes");
} else if (result.get() == buttonTypeCancel) {
System.out.println("Pressed No");
}
}
}
答案 0 :(得分:0)
这只是猜测我想你想要什么。你的问题不是很清楚。
此应用使用来自here的@Sergey Grinev代码。
它每两秒更新一次Alert
。
//imports
import java.util.Optional;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application
{
int number = 50;
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception
{
alertMethod();
}
private void alertMethod()
{
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Title");
alert.setHeaderText("Number " + number);
alert.setContentText("Choose your option.");
ButtonType buttonTypeOne = new ButtonType("Yes");
ButtonType buttonTypeCancel = new ButtonType("No", ButtonBar.ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);
Timeline twoSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(2), new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
number = number + 10;
alert.setAlertType(Alert.AlertType.CONFIRMATION);
alert.setTitle("Title -> " + number);
alert.setHeaderText("Number " + number);
alert.setContentText("Choose your option.");
}
}));
twoSecondsWonder.setCycleCount(Timeline.INDEFINITE);
twoSecondsWonder.play();
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne) {
System.out.println("Pressed Yes");
}
else if (result.get() == buttonTypeCancel) {
System.out.println("Pressed No");
}
}
}