我试图每次使用buffer.setText("...")
或当用户键入某些内容时,每次TextBuffer的内容更改时都调用一个函数,但是在使用setText之后,该函数被调用两次(而不是一次)
package com.example;
import org.gnome.gtk.*;
public class Main {
public static void main(String[] args) {
Gtk.init(args);
Window w = new Window();
w.setDefaultSize(200, 200);
TextBuffer buffer = new TextBuffer();
buffer.setText("first text");
TextView textView = new TextView(buffer);
buffer.connect(new TextBuffer.Changed() {
@Override
public void onChanged(TextBuffer textBuffer) {
System.out.println("onChange called with " + textBuffer.getText());
}
});
buffer.setText("second text");
buffer.setText("third text");
w.add(textView);
w.connect((Window.DeleteEvent) (source, event) -> {
Gtk.mainQuit();
return false;
});
w.showAll();
Gtk.main();
}
}
此示例打印
onChange called with
onChange called with second text
onChange called with
onChange called with third text
知道为什么会这样吗?我正在使用java-gnome 4.1和Java 1.8。
答案 0 :(得分:0)
gtk_text_buffer_set_text
是gtk_text_buffer_delete
和gtk_text_buffer_insert
的组合。此函数发出"delete-range"
和"insert-text"
信号。这些信号具有default handlers。这些处理程序会发出"changed"
。
实际上,对于单个gtk_buffer_set_text
,呼叫信号按以下顺序发出:
"delete-range"
"changed"
"insert-text"
"changed"