java - 如何在 java 中创建 gnome gtk 通知?

标签 java javafx gtk gnome

我正在学习 javafx,并希望我的应用程序能够显示 gnome shell 通知。我正在使用 java 的标准 gnome 库。这是我使用 javafx 创建的一个简单应用程序,其中单击按钮时,将创建一个通知。

主类

package gtknotifications;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.gnome.gtk.Gtk;

/**
 *
 * @author jyotiproy
 */
public class GtkNotifications extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }

}

FXML 文件

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gtknotifications.FXMLDocumentController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>

Fxml Controller

package gtknotifications;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import org.gnome.notify.*;
import org.gnome.gtk.*;

/**
 *
 * @author jyotiproy
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        Notification notify = new Notification("Test", "This is a Test Gtk Notification","");
        notify.setTimeout(1000);
        notify.show();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

点击按钮时出现的错误是

Caused by: org.freedesktop.bindings.FatalError: 

You *must* call Gtk.init() before using anything else in java-gnome!

Caused by: java.lang.reflect.InvocationTargetException

为什么会出现这些错误以及如何消除它们?

编辑 正如所提到的评论和答案所指示的那样,我运行了代码并添加了 Gtk.init(new String[0]);在主类的 start 方法中,但出现以下错误。

DANGER: GLib-GObject-WARNING, cannot register existing type 'GdkDisplayManager'
DANGER: GLib-CRITICAL, g_once_init_leave: assertion 'result != 0' failed
DANGER: GLib-GObject-CRITICAL, g_object_new_with_properties: assertion 'G_TYPE_IS_OBJECT (object_type)' failed

显示这些后,窗口不会加载,也不会显示堆栈跟踪。构建也不会停止,需要强制关闭。

Errors

最佳答案

根据错误消息,您应该调用 org.gnome.gtk.Gtk.init(String[])在您尝试使用任何 GTK 功能之前,在应用程序生命周期的早期某个地方使用该方法。根据 javadoc 方法:

Initialize the GTK libraries. This must be called before any other org.gnome.* classes are used.

您可以尝试以下方法:

@Override
public void start(Stage stage) throws Exception {
    Gtk.init(new String[0]);
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    ...

关于java - 如何在 java 中创建 gnome gtk 通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57275417/

相关文章:

java - 给定一个字符串数组,如何找出特定字符串的索引?

java - ScalaFX/JavaFX 8 获取最近的节点

c - 无法在 GTK+ 窗口中嵌入 Gstreamer 视频

gtk - 瓦拉 Gtk 模板 : UI Resource not found

gtk - 使用 PyGObject 正确构造和突出显示 GtkPopoverMenu

java - 如何使其实时数据观察器基于用户点击事件工作并接受一些用户输入,但应该从 onCreate 开始观察?

java.lang.IllegalStateException : Failed to load ApplicationContext when running unit test with HSQL embedded database

java - 在 Eclipse 中调试时更改编译的代码

JavaFX - TreeTableView sortPolicyProperty()

maven - 如何让 javafx 类进入运行时?