如何在 JavaFX 中建立按鈕?
在 JavaFX 中,javafx.scene.control 包提供各種專門針對 UI 應用程式設計的節點(類),並且這些節點可重複使用。你可以自定義這些節點併為 JavaFX 應用程式構建檢視頁面。示例:按鈕、複選框、標籤等。
按鈕是使用者介面應用程式中的控制元件,通常單擊該按鈕會執行相應的操作。
你可以透過例項化該包的 javafx.scene.control.Button 類來建立按鈕,並且可以使用 setText() 方法向按鈕設定文字。
示例
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.paint.Color; import javafx.stage.Stage; public class ButtonExample extends Application { @Override public void start(Stage stage) { //Creating a Button Button button = new Button(); //Setting text to the button button.setText("Sample Button"); //Setting the location of the button button.setTranslateX(150); button.setTranslateY(60); //Setting the stage Group root = new Group(button); Scene scene = new Scene(root, 595, 150, Color.BEIGE); stage.setTitle("Button Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
輸出
廣告