76.md 8.4 KB
Newer Older
W
wizardforcel 已提交
1
# JavaFX 首个程序
W
init  
wizardforcel 已提交
2 3 4 5 6 7 8

> 原文: [http://zetcode.com/gui/javafx/firstprograms/](http://zetcode.com/gui/javafx/firstprograms/)

在本章中,我们将创建一些基本的 JavaFX 程序。

## 退出按钮

W
wizardforcel 已提交
9
在下面的示例中,我们有一个`Button`控件。 当我们单击按钮时,应用终止。 按下并释放按钮时,将发送`ActionEvent`
W
init  
wizardforcel 已提交
10 11 12

`QuitButtonEx.java`

W
wizardforcel 已提交
13
```java
W
init  
wizardforcel 已提交
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
package com.zetcode;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * ZetCode JavaFX tutorial
 *
 * This program has a Quit button. Clicking
 * on the button terminates the application.
 *
 * Author: Jan Bodnar
 * Website: zetcode.com
 * Last modified: June 2015
 */

public class QuitButtonEx extends Application {

    @Override
    public void start(Stage stage) {

        initUI(stage);
    }

    private void initUI(Stage stage) {

        Button btn = new Button();
        btn.setText("Quit");
        btn.setOnAction((ActionEvent event) -> {
            Platform.exit();
        });

        HBox root = new HBox();
        root.setPadding(new Insets(25));
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 280, 200);

        stage.setTitle("Quit button");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

```

W
wizardforcel 已提交
70
`Button`控件位于窗口的左上角。 事件处理器将添加到按钮。
W
init  
wizardforcel 已提交
71

W
wizardforcel 已提交
72
```java
W
init  
wizardforcel 已提交
73 74 75 76 77 78 79
Button btn = new Button();
btn.setText("Quit");

```

实例化`Button`控件。 `setText()`方法设置按钮的标签。

W
wizardforcel 已提交
80
```java
W
init  
wizardforcel 已提交
81 82 83 84 85 86
btn.setOnAction((ActionEvent event) -> {
    Platform.exit();
});

```

W
wizardforcel 已提交
87
`setOnAction()`方法设置按钮的动作,每当触发按钮时都会调用该动作。 上面的代码创建了一个匿名事件处理器。 `Platform.exit()`终止应用。
W
init  
wizardforcel 已提交
88

W
wizardforcel 已提交
89
```java
W
init  
wizardforcel 已提交
90 91 92 93 94 95 96
HBox root = new HBox();
root.setPadding(new Insets(25));

```

`HBox`是将其子级布置在单个水平行中的窗格。 `setPadding()`方法在窗格内容周围创建填充。 (默认填充为`Insets.EMPTY`。)这样,按钮和窗口边框的边缘之间会留有一些空间。

W
wizardforcel 已提交
97
```java
W
init  
wizardforcel 已提交
98 99 100 101 102 103 104 105
root.getChildren().add(btn);

```

该按钮将添加到`HBox`窗格。

![Quit button](img/9b501a59485c542238359a21e520526f.jpg)

W
wizardforcel 已提交
106
图:退出按钮
W
init  
wizardforcel 已提交
107 108 109 110 111 112 113

## 工具提示

任何节点都可以显示工具提示。 `Tooltip`是常见的 UI 元素,通常用于显示有关场景图中节点的其他信息。 当我们将鼠标指针悬停在节点上时,将显示该图标。

`TooltipEx.java`

W
wizardforcel 已提交
114
```java
W
init  
wizardforcel 已提交
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
package com.zetcode;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * ZetCode JavaFX tutorial
 *
 * This program creates a tooltip for 
 * a button control.
 *
 * Author: Jan Bodnar
 * Website: zetcode.com
 * Last modified: June 2015
 */

public class TooltipEx extends Application {

    @Override
    public void start(Stage stage) {

        initUI(stage);
    }

    private void initUI(Stage stage) {

        HBox root = new HBox();
        root.setPadding(new Insets(20));

        Button btn = new Button("Button");
        Tooltip tooltip = new Tooltip("Button control");
        Tooltip.install(btn, tooltip);

        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        stage.setTitle("Tooltip");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

```

在示例中,我们将工具提示设置为按钮控件。

W
wizardforcel 已提交
171
```java
W
init  
wizardforcel 已提交
172 173 174 175 176 177
Button btn = new Button("Button");

```

实例化`Button`控件。

W
wizardforcel 已提交
178
```java
W
init  
wizardforcel 已提交
179 180 181 182 183
Tooltip tooltip = new Tooltip("Button control");
Tooltip.install(btn, tooltip);

```

W
wizardforcel 已提交
184
创建一个`Tooltip`并将其设置为通过`Tooltip``install()`方法设置的按钮。
W
init  
wizardforcel 已提交
185 186 187

![Tooltip](img/b96ebcb443f5e34d715e9e137c3324cd.jpg)

W
wizardforcel 已提交
188
图:工具提示
W
init  
wizardforcel 已提交
189 190 191 192 193

## 助记符

助记符是激活支持助记符的控件的快捷键。 例如,它们可以与标签,按钮或菜单项一起使用。

W
wizardforcel 已提交
194
助记符是通过在控件的标签上添加`_`字符来创建的。 它使下一个字符成为助记符。 字符与无鼠标修饰符(通常为 `Alt` )结合在一起。 选择的字符带有下划线,但是可以以平台特定的方式强调。 在某些平台上,仅在按下无鼠标修饰符后才对字符加下划线。
W
init  
wizardforcel 已提交
195 196 197

`MnemonicEx.java`

W
wizardforcel 已提交
198
```java
W
init  
wizardforcel 已提交
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
package com.zetcode;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * ZetCode JavaFX tutorial
 *
 * This program creates a mnemonic for 
 * a button control.
 *
 * Author: Jan Bodnar
 * Website: zetcode.com
 * Last modified: June 2015
 */

public class MnemonicEx extends Application {

    @Override
    public void start(Stage stage) {

        initUI(stage);
    }

    private void initUI(Stage stage) {

        HBox root = new HBox();
        root.setPadding(new Insets(20));

        Button btn = new Button("_Button");
        btn.setOnAction((ActionEvent event) -> {
            System.out.println("Button fired");
        });

        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        stage.setTitle("Mnemonic");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

```

W
wizardforcel 已提交
254
我们为按钮控件设置了一个助记符。 可以使用 `Alt + B` 键盘快捷键激活。
W
init  
wizardforcel 已提交
255

W
wizardforcel 已提交
256
```java
W
init  
wizardforcel 已提交
257 258 259 260
Button btn = new Button("_Button");

```

W
wizardforcel 已提交
261
在按钮的标签中,`_`字符位于`B`字符之前; 因此,`B`字符带有下划线,并包含在键盘快捷键中。
W
init  
wizardforcel 已提交
262

W
wizardforcel 已提交
263
```java
W
init  
wizardforcel 已提交
264 265 266 267 268 269 270 271
btn.setOnAction((ActionEvent event) -> {
    System.out.println("Button fired");
});

```

触发按钮后,它将消息发送到控制台。

W
wizardforcel 已提交
272
目前,有三种激活按钮的方式:单击鼠标左键, `Alt + B` 快捷方式以及`空格键` 按钮具有焦点)。
W
init  
wizardforcel 已提交
273 274 275 276 277 278 279

## 设置控件样式

JavaFX 中的控件可以使用 CSS 设置样式。

`text.css`

W
wizardforcel 已提交
280
```java
W
init  
wizardforcel 已提交
281 282 283 284 285 286 287 288 289
#root {-fx-background-color: linear-gradient(gray, darkgray); }
#text {-fx-fill:linear-gradient(orange, orangered); }  

```

此 CSS 文件为根节点和`Text`节点创建样式。

`StylingTextEx.java`

W
wizardforcel 已提交
290
```java
W
init  
wizardforcel 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
package com.zetcode;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * ZetCode JavaFX tutorial
 *
 * This program styles a Text control with CSS.
 *
 * Author: Jan Bodnar
 * Website: zetcode.com
 * Last modified: June 2015
 */

public class StylingTextEx extends Application {

    @Override
    public void start(Stage stage) {

        initUI(stage);
    }

    private void initUI(Stage stage) {

        HBox root = new HBox();
        root.setPadding(new Insets(20));

        Text text = new Text("ZetCode");
        text.setFont(Font.font("Serif", FontWeight.BOLD, 76));

        text.setId("text");
        root.setId("root");

        root.getChildren().addAll(text);

        Scene scene = new Scene(root);
        scene.getStylesheets().add(this.getClass().getResource("text.css")
                .toExternalForm());

        stage.setTitle("Styling text");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

```

该示例为根节点创建背景渐变颜色,并为`Text`控件创建线性渐变填充。

W
wizardforcel 已提交
351
```java
W
init  
wizardforcel 已提交
352 353 354 355 356 357 358
Text text = new Text("ZetCode");
text.setFont(Font.font("Serif", FontWeight.BOLD, 76));

```

创建一个`Text`控件。 较大的粗体衬线字体设置为控件。

W
wizardforcel 已提交
359
```java
W
init  
wizardforcel 已提交
360 361 362 363 364 365 366
text.setId("text");
root.setId("root");

```

节点由其 ID 标识,该 ID 由`setId()`方法设置。

W
wizardforcel 已提交
367
```java
W
init  
wizardforcel 已提交
368 369 370 371 372 373 374 375 376
scene.getStylesheets().add(this.getClass().getResource("text.css")
        .toExternalForm());

```

样式表已添加到`Scene`中。

![Styled Text control](img/db572a0ae13e35d1088e1286367f7004.jpg)

W
wizardforcel 已提交
377
图:样式文本控件
W
init  
wizardforcel 已提交
378 379

在本章中,我们创建了一些简单的 JavaFX 程序。