GUI编程17:下拉框、列表框

视频链接:19、下拉框、列表框_哔哩哔哩_bilibiliicon-default.png?t=O83Ahttps://www.bilibili.com/video/BV1DJ411B75F?p=19&vd_source=b5775c3a4ea16a5306db9c7c1c1486b5

1.下拉框

代码示例

package com.yundait.lesson06;

import javax.swing.*;
import java.awt.*;

public class TestComboBoxDemo01 extends JFrame {
    public TestComboBoxDemo01(){
        Container container = this.getContentPane();

        //创建下拉框
        JComboBox jComboBox = new JComboBox();

        //在下拉框中添加内容
        jComboBox.addItem(null);
        jComboBox.addItem("正在热映1");
        jComboBox.addItem("正在热映2");
        jComboBox.addItem("正在热映3");
        jComboBox.addItem("已下架1");
        jComboBox.addItem("已下架2");
        jComboBox.addItem("已下架3");
        jComboBox.addItem("即将上映1");
        jComboBox.addItem("即将上映2");
        jComboBox.addItem("即将上映3");
        
        //添加下拉框到容器中
        container.add(jComboBox);

        this.setVisible(true);
        this.setSize(600,700);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestComboBoxDemo01();
    }
}

运行结果:

2.列表框

代码示例:

package com.yundait.lesson06;

import javax.swing.*;
import java.awt.*;

public class JListDemo extends JFrame {
    public JListDemo(){

        Container container = this.getContentPane();

        String[] contents = {"张三", "李四", "王五", "赵六"};

        JList jList = new JList(contents);


        //添加下拉框到容器中
        container.add(jList);

        this.setVisible(true);
        this.setSize(600,700);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JListDemo();
    }
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值