自学内容网 自学内容网

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

运行结果:


原文地址:https://blog.csdn.net/zhangjinajian759/article/details/142370165

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!