如何使用JavaFX创建ListView?
例
下面的JavaFX程序演示了ListView的创建。
import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.stage.Stage; public class ListViewExample extends Application { public void start(Stage stage) { //教育标签 Label label = new Label("学历:"); Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12); label.setFont(font); //列表查看学历 ObservableList<String> names = FXCollections.observableArrayList("Engineering", "MCA", "MBA", "Graduation", "MTECH", "Mphil", "Phd"); ListView<String> listView = new ListView<String>(names); listView.setMaxSize(200, 160); //创建布局 VBox layout = new VBox(10); layout.setPadding(new Insets(5, 5, 5, 50)); layout.getChildren().addAll(label, listView); layout.setStyle("-fx-background-color: BEIGE"); //设置舞台 Scene scene = new Scene(layout, 595, 200); stage.setTitle("List View Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
输出结果