简体   繁体   English

如何在Java中创建gnome gtk通知?

[英]How to create gnome gtk notifications in java?

I'm learning javafx and want my application to be able to show gnome shell notifications. 我正在学习javafx,希望我的应用程序能够显示gnome shell通知。 I'm using the standard gnome libraries for java. 我正在使用Java的标准gnome库。 Here is a simple app that I created using javafx in which when a button is clicked, a notification will be created. 这是我使用javafx创建的一个简单应用,其中单击按钮时将创建通知。

Main class 主班

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 file 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 Fxml控制器

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
    }    

}

The error I'm getting while clicking the button is 单击按钮时出现的错误是

Caused by: org.freedesktop.bindings.FatalError: 

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

and

Caused by: java.lang.reflect.InvocationTargetException

Why are these errors coming and how to get rid of them? 为什么会出现这些错误,以及如何消除这些错误?

EDIT As the comments mentioned and the answer instructed I ran the code with adding the Gtk.init(new String[0]); 编辑正如提到的评论和答案所指示的,我通过添加Gtk.init(new String[0]);运行了代码Gtk.init(new String[0]); in the start method of the main class but I got the following errors. 在主类的启动方法中,但出现以下错误。

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

After showing these, the window doesn't load and no stack traces are shown. 显示这些内容后,该窗口将不会加载,也不会显示任何堆栈跟踪。 The build also doesn't stop and needs to be force closed. 构建也不会停止,需要强制关闭。

失误

As per the error message you are supposed to call the org.gnome.gtk.Gtk.init(String[]) method somewhere early in your application lifecycle, before you attempt to use any GTK features. 根据错误消息,您应该在尝试使用任何GTK功能之前,在应用程序生命周期的早期调用org.gnome.gtk.Gtk.init(String[])方法。 As per the method javadoc: 按照方法javadoc:

Initialize the GTK libraries. 初始化GTK库。 This must be called before any other org.gnome.* classes are used. 必须在使用任何其他org.gnome。*类之前调用​​此方法。

You could try the below: 您可以尝试以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM