一.文本输入类控件
文本输入类控件用于接收用户的输入。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
TextField textField = new TextField();
StackPane root = new StackPane();
root.getChildren().add(textField);
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("JavaFX应用程序");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
二.列表类控件
列表类控件用于展示列表数据。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
TableView<User> tableView = new TableView<>();
TableColumn<User, String> nameCol = new TableColumn<>("姓名");
TableColumn<User, Integer> ageCol = new TableColumn<>("年龄");
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
ageCol.setCellValueFactory(new PropertyValueFactory<>("age"));
tableView.getColumns().addAll(nameCol, ageCol);
tableView.getItems().addAll(
new User("张三", 20),
new User("李四", 25),
new User("王五", 30)
);
StackPane root = new StackPane();
root.getChildren().add(tableView);
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("JavaFX应用程序");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
三.布局类控件
布局类控件用于管理其他控件的位置和大小。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Label label1 = new Label("Label 1");
Label label2 = new Label("Label 2");
VBox box = new VBox();
box.getChildren().addAll(label1, label2);
StackPane root = new StackPane();
root.getChildren().add(box);
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("JavaFX应用程序");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}