JavaFX使用ChoiceBox、ComboBox实现下拉列表

13 篇文章 1 订阅

目录

 

一、环境

二、模拟

三、代码


一、环境

jdk8

二、模拟

三、代码

package xyz.hashdog.class36_37_38;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;


/**
 * @author th
 * @description: TODO
 * @projectName hashdog
 * @date 2020/2/1620:48
 */
public class Launch extends Application {

    public static void main(String[] args) {
        Application.launch(Launch.class,args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        Student s1 = new Student("s1",18,90);
        Student s2 = new Student("s2",18,90);
        Student s3 = new Student("s3",18,90);
        Student s4 = new Student("s4",18,90);
        Student s5 = new Student("s5",18,90);

        AnchorPane an = new AnchorPane();
        //下拉列表
        ChoiceBox<Student> cb = new ChoiceBox<Student>();
        cb.getItems().addAll(s1,s2,s3,s4,s5);

        //数据转换
        cb.setConverter(new StringConverter<Student>() {
            //只显示名字
            @Override
            public String toString(Student object) {
                String value = object.getName();
                return value;
            }

            @Override
            public Student fromString(String string) {
                return null;
            }
        });


        ComboBox<String> cbb = new ComboBox<String>();
        cbb.getItems().addAll("str1","str2","str3");
        //运行编辑
        cbb.setEditable(true);

        AnchorPane.setLeftAnchor(cbb,60.0);
        an.getChildren().addAll(cb,cbb);
        an.setStyle("-fx-background-color: deepskyblue");
        Scene s= new Scene(an);
        primaryStage.setScene(s);
        primaryStage.setTitle("hashdog");
        primaryStage.setWidth(500);
        primaryStage.setHeight(300);
        //设置窗口不可拉伸
        primaryStage.setResizable(false);
        primaryStage.show();






    }

}


class Student{
    private  String name;
    private  int age;
    private  double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public Student(){

    }

}

 

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值