写一个JavaFX基本组件使用综合示例
传奇科技果
首发
2023-12-2415:41甘肃瓜州县第四中学计算机科学与技术高级教师
网络图片非实物
一、目标
使用javafx 写一个基本组件综合示例。比如,标签、文本框、按钮、水平布局和垂直布局显示在一个窗口。包括按钮单击事件。
二、示例代码
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class JavaFXExample extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX Example");
// 创建标签和文本框
Label nameLabel = new Label("Name:");
TextField nameField = new TextField();
// 创建按钮
Button submitButton = new Button("Submit");
Button clearButton = new Button("Clear");
// 设置按钮点击事件
submitButton.setOnAction(e -> {
System.out.println("Name submitted: " + nameField.getText());
});
clearButton.setOnAction(e -> {
nameField.clear();
});