Java学习-GUI编程-下拉框、列表框

本文展示了如何在Java中创建和使用下拉框(JComboBox)和列表框(JList)。通过示例代码,演示了添加静态选项以及如何动态添加和删除列表框中的项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java学习-GUI编程-下拉框、列表框

下拉框

public class TestConboboxDemo extends JFrame {
    public TestConboboxDemo(){
        Container container = this.getContentPane();
        JComboBox status = new JComboBox();
        status.addItem(null);
        status.addItem("正在热映");
        status.addItem("已下架");
        status.addItem("即将上映");
        container.add(status);
        this.setVisible(true);
        this.setBounds(100,100,200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestConboboxDemo();
    }
}

运行程序,点击下拉,可以选择:
在这里插入图片描述
在这里插入图片描述

列表框

public class TestConboboxDemo extends JFrame {
    public TestConboboxDemo(){
        Container container = this.getContentPane();
        String[] contents = {"1","2","3"};
        JList jList = new JList(contents);
        container.add(jList);
        this.setVisible(true);
        this.setBounds(100,100,200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestConboboxDemo();
    }
}

运行程序,列表可选:
在这里插入图片描述

列表也可以动态增减
public class TestConboboxDemo extends JFrame {
    public TestConboboxDemo(){
        Container container = this.getContentPane();
        Vector vector = new Vector();
        JList jList = new JList(vector);
        vector.add("张三");
        vector.add("李四");
        vector.add("王五");
        vector.add("赵六");
        container.add(jList);
        this.setVisible(true);
        this.setBounds(100,100,200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestConboboxDemo();
    }
}

运行程序:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值