201.md 6.7 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
# Java Gnome 中的事件

> 原文: [http://zetcode.com/gui/javagnome/events/](http://zetcode.com/gui/javagnome/events/)

在 Java Gnome 编程教程的这一部分中,我们将讨论事件。

Java Gnome 库是事件驱动的系统。 所有 GUI 应用程序都是事件驱动的。 应用程序启动一个主循环,该循环不断检查新生成的事件。 如果没有事件,则应用程序将等待并且不执行任何操作。

## 简单事件示例

下一个示例显示了我们如何应对两个基本事件。

`quitbutton.java`

```
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.Button;
import org.gnome.gtk.Fixed;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * Java Gnome tutorial
 *
 * This program demonstrates two 
 * basic events. 
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */

public class GButton extends Window  {

    public GButton() {

        setTitle("Button");

        initUI();

        setPosition(WindowPosition.CENTER);
        showAll();
    }

    public void initUI() {

        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });

        Fixed fixed = new Fixed();

        Button quit = new Button("Quit");
        quit.connect(new Button.Clicked() {

            public void onClicked(Button button) {
                Gtk.mainQuit();
            }
        });

        quit.setSizeRequest(80, 35);
        fixed.put(quit, 50, 50);
        add(fixed);        

        setSizeRequest(250, 200);
    }

    public static void main(String[] args) {

        Gtk.init(args);
        new GButton();
        Gtk.main();
    }
}

```

在我们的代码示例中,我们对两个事件做出反应。 删除事件和单击事件。 当我们关闭窗口时,将触发删除事件。 默认情况下,该窗口被销毁,但应用程序不退出。 我们必须明确退出该程序。

```
connect(new Window.DeleteEvent() {
    public boolean onDeleteEvent(Widget source, Event event) {
        Gtk.mainQuit();
        return false;
    }
});

```

如果单击窗口的关闭按钮,则窗口将被破坏。 但是应用程序并未完全销毁。 我们必须调用`Gtk.mainQuit()`结束应用程序。 我们使用`connect()`方法将回调方法连接到特定事件类型。 在我们的例子中是`DeleteEvent`

```
quit.connect(new Button.Clicked() {
    public void onClicked(Button button) {
        Gtk.mainQuit();
    }
});

```

当我们单击按钮小部件时,将触发`onClicked()`方法。 我们对按钮单击做出反应,退出了应用程序。

## 移动窗

下一个示例显示了我们如何对移动窗口事件做出反应。 我们在标题栏中显示窗口左上角的当前位置。

`move.java`

```
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gdk.EventConfigure;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * ZetCode Java Gnome tutorial
 *
 * This program demonstrates the 
 * configure event.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */

public class GMoveWindow extends Window 
        implements Window.ConfigureEvent {

    public GMoveWindow()  {

        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });

        connect(this);

        setPosition(WindowPosition.CENTER);

        setTitle("");
        resize(250, 200);
        showAll();
    }

    public boolean onConfigureEvent(Widget widget,
        EventConfigure eventConfigure) {

        int x = eventConfigure.getX();
        int y = eventConfigure.getY();

        setTitle(x + ", " + y);
        return false;
    }

    public static void main(String[] args) {
        Gtk.init(args);
        new GMoveWindow();
        Gtk.main();
    }
}

```

调整大小和移动窗口最终导致创建`ConfigureEvent`

```
public boolean onConfigureEvent(Widget widget,
    EventConfigure eventConfigure) {

    int x = eventConfigure.getX();
    int y = eventConfigure.getY();

    setTitle(x + ", " + y);
    return false;
}

```

我们将窗口的 x,y 坐标设置为窗口的标题栏。

![Move event](img/4d8d83f7d16a6fe847d5f3455555dfc6.jpg)

Figure: Move event

## EnterNotifyEvent

当我们使用鼠标指针进入小部件的区域时,会发出`EnterNotifyEvent`

`enter.java`

```
package com.zetcode;

import org.gnome.gdk.Color;
import org.gnome.gdk.Event;
import org.gnome.gdk.EventCrossing;
import org.gnome.gtk.Button;
import org.gnome.gtk.Fixed;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.StateType;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * ZetCode Java Gnome tutorial
 *
 * This program demonstrates the 
 * EnterNotifyEvent.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */

public class GEnterNotifyEvent extends Window {

    private Color lightGray = new Color(55000, 55000, 55000);

    public GEnterNotifyEvent() {

        setTitle("EnterNotifyEvent");

        initUI();

        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });

        setDefaultSize(250, 150);
        setPosition(WindowPosition.CENTER);
        showAll();
    }

    public void initUI() {

        Button button = new Button("Button");
        button.setSizeRequest(80, 30);

        button.connect(new Button.EnterNotifyEvent() {
            public boolean onEnterNotifyEvent(Widget widget,
                        EventCrossing eventCrossing) {

                widget.modifyBackground(StateType.PRELIGHT, lightGray);

                return false;
            }
        });

        Fixed fix = new Fixed();
        fix.put(button, 20, 20);
        add(fix);
    }

    public static void main(String[] args) {
        Gtk.init(args);
        new GEnterNotifyEvent();
        Gtk.main();
    }
}

```

一旦将鼠标指针悬停在按钮小部件上,我们将更改其背景颜色。

```
button.connect(new Button.EnterNotifyEvent() {
    public boolean onEnterNotifyEvent(Widget widget,
        EventCrossing eventCrossing) {
        widget.modifyBackground(StateType.PRELIGHT, lightGray);
        return false;
    }
});

```

在这里,我们对`EnterNotifyEvent`做出反应。

```
widget.modifyBackground(StateType.PRELIGHT, lightGray);

```

我们修改按钮背景的颜色。

本章介绍 Java Gnome 中的事件。