简体   繁体   English

如何在java-gnome中创建自定义信号?

[英]How to create custom signal in java-gnome?

I am trying to create custom signal in java-gnome in my GTK+3 application but I can't. 我试图在我的GTK+3应用程序中创建java-gnome中的自定义信号,但我不能。

import org.gnome.gtk.CheckButton;

public class MyCheckButton extends CheckButton {

    // how can I add a cutom signal for this class
    // fos example: the signal that has "my-signal" name.
}

How can I do it? 我该怎么做?

I can do it in python 我可以在python中完成它

class MyCheckButton(Gtk.CheckButton):
    __gsignals__ = {
        'my_signal': (GObject.SIGNAL_RUN_FIRST, None,
                      (int,))}
abc = MyCheckButton()
abc.connect("my-signal", print)
abc.emit("my-signal", 543)

I've searched on the internet but I couldn't find anything. 我在网上搜索过,但找不到任何东西。

Thanks. 谢谢。

I think there is no similar way in Java (based on the fact that there is no similar structure in the source code). 我认为在Java中没有类似的方法(基于源代码中没有类似结构的事实)。

But you can implement your own handler by: 但您可以通过以下方式实现自己的处理程

  • Define the handler interface (#1) 定义处理程序接口(#1)
  • Add a connect method (#2, #3) 添加连接方法(#2,#3)
  • Add an emit method (#4) 添加emit方法(#4)

Sample code: 示例代码:

public class MyCheckButton extends CheckButton {

    // ...

    private MySignalHandler handler; // #2

    public interface MySignalHandler {  // #1
        public void onMySignal(int value);
    }

    public void connect(MySignalHandler handler) {  // #3
        this.handler = handler;
    }

    public void emitMySignal(int value) {  // #4
        if (handler == null) return;
        handler.onMySignal(value);
    }

}

And then you can connect and emit in the same way as the other events. 然后,您可以以与其他事件相同的方式连接和发射。

MyCheckButton a = new MyCheckButton();

a.connect(new MyCheckButton.MySignalHandler() {
  public void onMySignal(int value) {
    System.out.println(value);
  }
});

a.emitMySignal(10);

The built-in Button uses something similar to define the click event by adding the connect(Clicked handler) and emitClicked() ( source code ) 内置Button使用类似的东西来定义click事件,方法是添加connect(Clicked handler)emitClicked()源代码

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

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