<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # JavaFX 首個程序 > 原文: [http://zetcode.com/gui/javafx/firstprograms/](http://zetcode.com/gui/javafx/firstprograms/) 在本章中,我們將創建一些基本的 JavaFX 程序。 ## 退出按鈕 在下面的示例中,我們有一個`Button`控件。 當我們單擊按鈕時,應用終止。 按下并釋放按鈕時,將發送`ActionEvent`。 `QuitButtonEx.java` ```java 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); } } ``` `Button`控件位于窗口的左上角。 事件處理器將添加到按鈕。 ```java Button btn = new Button(); btn.setText("Quit"); ``` 實例化`Button`控件。 `setText()`方法設置按鈕的標簽。 ```java btn.setOnAction((ActionEvent event) -> { Platform.exit(); }); ``` `setOnAction()`方法設置按鈕的動作,每當觸發按鈕時都會調用該動作。 上面的代碼創建了一個匿名事件處理器。 `Platform.exit()`終止應用。 ```java HBox root = new HBox(); root.setPadding(new Insets(25)); ``` `HBox`是將其子級布置在單個水平行中的窗格。 `setPadding()`方法在窗格內容周圍創建填充。 (默認填充為`Insets.EMPTY`。)這樣,按鈕和窗口邊框的邊緣之間會留有一些空間。 ```java root.getChildren().add(btn); ``` 該按鈕將添加到`HBox`窗格。 ![Quit button](https://img.kancloud.cn/fa/ef/faefb0112673bdba0cce326586b7e7a0_282x226.jpg) 圖:退出按鈕 ## 工具提示 任何節點都可以顯示工具提示。 `Tooltip`是常見的 UI 元素,通常用于顯示有關場景圖中節點的其他信息。 當我們將鼠標指針懸停在節點上時,將顯示該圖標。 `TooltipEx.java` ```java 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); } } ``` 在示例中,我們將工具提示設置為按鈕控件。 ```java Button btn = new Button("Button"); ``` 實例化`Button`控件。 ```java Tooltip tooltip = new Tooltip("Button control"); Tooltip.install(btn, tooltip); ``` 創建一個`Tooltip`并將其設置為通過`Tooltip`的`install()`方法設置的按鈕。 ![Tooltip](https://img.kancloud.cn/97/75/9775ceb1ed98d4cfb95d92e4b5369a92_302x276.jpg) 圖:工具提示 ## 助記符 助記符是激活支持助記符的控件的快捷鍵。 例如,它們可以與標簽,按鈕或菜單項一起使用。 助記符是通過在控件的標簽上添加`_`字符來創建的。 它使下一個字符成為助記符。 字符與無鼠標修飾符(通常為 `Alt` )結合在一起。 選擇的字符帶有下劃線,但是可以以平臺特定的方式強調。 在某些平臺上,僅在按下無鼠標修飾符后才對字符加下劃線。 `MnemonicEx.java` ```java 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); } } ``` 我們為按鈕控件設置了一個助記符。 可以使用 `Alt + B` 鍵盤快捷鍵激活。 ```java Button btn = new Button("_Button"); ``` 在按鈕的標簽中,`_`字符位于`B`字符之前; 因此,`B`字符帶有下劃線,并包含在鍵盤快捷鍵中。 ```java btn.setOnAction((ActionEvent event) -> { System.out.println("Button fired"); }); ``` 觸發按鈕后,它將消息發送到控制臺。 目前,有三種激活按鈕的方式:單擊鼠標左鍵, `Alt + B` 快捷方式以及`空格鍵` 按鈕具有焦點)。 ## 設置控件樣式 JavaFX 中的控件可以使用 CSS 設置樣式。 `text.css` ```java #root {-fx-background-color: linear-gradient(gray, darkgray); } #text {-fx-fill:linear-gradient(orange, orangered); } ``` 此 CSS 文件為根節點和`Text`節點創建樣式。 `StylingTextEx.java` ```java 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`控件創建線性漸變填充。 ```java Text text = new Text("ZetCode"); text.setFont(Font.font("Serif", FontWeight.BOLD, 76)); ``` 創建一個`Text`控件。 較大的粗體襯線字體設置為控件。 ```java text.setId("text"); root.setId("root"); ``` 節點由其 ID 標識,該 ID 由`setId()`方法設置。 ```java scene.getStylesheets().add(this.getClass().getResource("text.css") .toExternalForm()); ``` 樣式表已添加到`Scene`中。 ![Styled Text control](https://img.kancloud.cn/a0/d2/a0d2da16e8ce11f9d0b06372df5fc2a9_394x156.jpg) 圖:樣式文本控件 在本章中,我們創建了一些簡單的 JavaFX 程序。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看