JavaFX-ListView组件

使用idea创建一个javafx项目

 修改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();
    }

}

运行界面

最大化界面

 其他代码

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>

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人海中的海盗

你的打赏将是对我的激励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值