【发布时间】:2014-11-11 21:53:24
【问题描述】:
您可以像这样使用 JavaFx 发送桌面通知(需要 jdk 8u20 或更高版本):
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import org.controlsfx.control.Notifications;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
// Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Button notifyButton = new Button("Notify");
notifyButton.setOnAction(e -> {
Notifications.create().title("Test").text("Test Notification!").showInformation();
});
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(notifyButton, 100, 50));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
但是这样你必须创建一个主窗口(舞台),是否可以避免这种情况?我正在寻找一种发送通知的方法,例如在 bash 中使用 zenity:大部分代码都是非 gui,但使用一些 gui 元素以非常基本的方式通知用户或与用户交互。
【问题讨论】:
-
你的问题对我来说没有多大意义。舞台只是一个窗口。您无法在没有窗口的桌面窗口系统中显示任何类型的 UI。在这些zenity screenshots 中,所有的对话框都是窗口。您不需要第三方的 controlsfx 库来显示 UI 对话框(尽管它可能会有所帮助)。您只需 init the primary stage style 并填充舞台以显示对话框。
-
有了 zenity,你可以做任何你想做的事情,甚至不需要触摸 gui(在 bash 中),并且只在必要时弹出窗口。使用 Javafx,对我来说如何接收命令行参数并不明显。问题是你不能在 main() 中做很多事情,除了 launch(args)。
-
请参阅Application::getParameters,了解 JavaFX 应用程序中的命令行参数检索。请参阅Platform::setImplicitExit 以在未显示任何窗口时在后台运行 JavaFX 应用程序。请参阅stage::show, stage::hide and stage::showAndWait,了解按需显示和隐藏阶段的不同方式。
标签: java user-interface javafx notifications