引用下面的代码片段:
public class Application extends javafx.application.Application implements ActionListener {
private java.awt.SystemTray tray;
private java.awt.TrayIcon trayIcon;
private java.awt.PopupMenu popupMenu = new PopupMenu();
private java.awt.MenuItem menuItem = new MenuItem("My Item");
@Override
public void start(Stage primaryStage) throws Exception {
if (!SystemTray.isSupported())
return;
menuItem.addActionListener(this);
popupMenu.add(menuItem);
trayIcon = new TrayIcon(image, "Title", popupMenu);
tray = SystemTray.getSystemTray();
tray.add(trayIcon);
}
@Override
public void actionPerformed(ActionEvent e){
Platform.runLater(() -> {
Optional<String> result = new TextInputDialog().showAndWait();
if(result.isPresent() && !result.get().isEmpty()){
...
}
})
}
}
发生的情况是该对话框仅显示一次。第二次或多次触发 actionPerformed()
时,它不会弹出,也不会抛出任何异常。
我尝试在该Task
上使用Task
、setOnSucceeded()
,并启动一个基于Thread
执行该任务
。更糟糕的是,该对话框根本不会显示,而且也没有产生任何错误。
最佳答案
您需要设置以下内容:
Platform.setImplicitExit(false);
否则,当您关闭最后一个窗口时,JavaFX 运行时将关闭,因此 Platform.runLater()
只能为您工作一次
来自Platform的片段:
Sets the implicitExit attribute to the specified value. If this attribute is true, the JavaFX runtime will implicitly shutdown when the last window is closed; the JavaFX launcher will call the Application.stop() method and terminate the JavaFX application thread. If this attribute is false, the application will continue to run normally even after the last window is closed, until the application calls exit(). The default value is true.
关于java - TextInputDialog 仅第一次显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37930333/