如何在 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);
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP