使用idea创建一个javafx项目
![](https://img-blog.csdnimg.cn/fea00508993a47ee8db9230a96bd1996.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Lq65rW35Lit55qE5rW355uX,size_10,color_FFFFFF,t_70,g_se,x_16)
修改FXML布局文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ListView fx:id="listView"
maxHeight="1.7976931348623157E308"
prefHeight="400.0"
prefWidth="400.0"
AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
编写HelloApplication类
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
private ListView<String> listView;
public void showListView() {
ObservableList<String> list = FXCollections.observableArrayList();
list.add("张三");
list.add("张三1");
list.add("张三3");
list.add("李四1");
list.add("李四2");
list.add("张三6");
list.add("李四3");
listView.setItems(list);
listView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
}
@Override
public void start(Stage stage) throws IOException {
// Parent root = FXMLLoader.load(getClass().getResource("hello-view.fxml"));
// Scene scene = new Scene(root);
//初始化FXML布局文件内容
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
//父级
Parent root = fxmlLoader.load();
//根据fx:id从父级root布局中查询并实例化listView组件
listView= (ListView<String>) root.lookup("#listView");
//渲染listView组件
showListView();
//设置标题
stage.setTitle("JavaFx示例");
//实例化场景长宽
Scene scene = new Scene(root, 400, 240);
//设置场景
stage.setScene(scene);
//渲染场景
stage.show();
}
public static void main(String[] args) {
launch();
}
}
运行界面
![](https://img-blog.csdnimg.cn/c015071ac6a749f490a02d41c30b95c4.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Lq65rW35Lit55qE5rW355uX,size_13,color_FFFFFF,t_70,g_se,x_16)
最大化界面
![](https://img-blog.csdnimg.cn/beff835073d048fca7bb267de1e92597.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Lq65rW35Lit55qE5rW355uX,size_20,color_FFFFFF,t_70,g_se,x_16)
其他代码
HelloController
import javafx.fxml.Initializable;
import java.net.URL;
import java.util.ResourceBundle;
public class HelloController implements Initializable {
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
}
module-info.java
module yu.javafxdemos {
requires javafx.controls;
requires javafx.fxml;
opens yu.javafxdemos to javafx.fxml;
exports yu.javafxdemos;
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>yu</groupId>
<artifactId>JavaFXDemos</artifactId>
<version>1.0-SNAPSHOT</version>
<name>JavaFXDemos</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javafx.version>17.0.2</javafx.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.6</version>
<executions>
<execution>
<!-- Default configuration for running with: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>yu.javafxdemos/yu.javafxdemos.HelloApplication</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>