簡體   English   中英

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

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

我正在學習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控制器

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]);運行了代碼Gtk.init(new String[0]); 在主類的啟動方法中,但出現以下錯誤。

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

顯示這些內容后,該窗口將不會加載,也不會顯示任何堆棧跟蹤。 構建也不會停止,需要強制關閉。

失誤

根據錯誤消息,您應該在嘗試使用任何GTK功能之前,在應用程序生命周期的早期調用org.gnome.gtk.Gtk.init(String[])方法。 按照方法javadoc:

初始化GTK庫。 必須在使用任何其他org.gnome。*類之前調用​​此方法。

您可以嘗試以下方法:

@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