下拉框+列表框+滚动窗格组件应用小案例 - 用户调查界面

版权声明:转载请注明出处 https://blog.csdn.net/doubleguy/article/details/83099211

效果图:

今天了解一下这三个组件:

1.下拉框组件:JComboBox

2.列表框组件:JList

3.滚动窗格组件:JScrollPane

代码如下:

/**
 * @ Author_张斌
 * Layout演示
 *
 * 1.定义组件
 * 2.创建组件(构造函数)
 * 3.添加组件
 * 4.对窗体进行设置
 * 5.显示窗口
 */

import javax.swing.*;
import javax.xml.stream.Location;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;

public class Tests extends JFrame{

    //定义组件
    JLabel jl1,jl2;
    JList jl;
    JComboBox jcb;
    JScrollPane jsp;
    JPanel jp1,jp2;

    public static void main(String[] args){

        Tests tests = new Tests();

    }

    public Tests(){
        //创建组件
        jp1 = new JPanel();
        jp2 = new JPanel();
        jl1 = new JLabel("籍贯");
        jl2 = new JLabel("旅游地点");

        String[] jg = {"北京","上海","广州","深圳"};
        jcb = new JComboBox(jg);

        String[] dd = {"青天河","云台山","长城","天安门","故宫","圆明园","老子故里"};
        jl = new JList(dd);
        jl.setVisibleRowCount(4);
        jsp = new JScrollPane(jl);

        //添加组件
        jp1.add(jl1);
        jp1.add(jcb);
        jp2.add(jl2);
        jp2.add(jsp);

        this.add(jp1);
        this.add(jp2);

        //添加布局管理器
        this.setLayout(new GridLayout(3,1));

        //设置窗体
        this.setTitle("用户调查");
        this.setSize(400,300);
        this.setLocation(200,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //禁止用户改变窗体大小
        this.setResizable(false);

        //显示窗体
        this.setVisible(true);
    }
}

猜你喜欢

转载自blog.csdn.net/doubleguy/article/details/83099211