Java窗口(JFrame)从零开始(7)——下拉列表+列表框

下拉列表(JComboBox)
列表框(JList)

package jframe;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.*;

public class jframe extends JFrame{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    public jframe(){
        this.setTitle("test");
        Container con = this.getContentPane();
        con.setLayout(new BorderLayout());                                                
        String [] a = {"身份证","军人证","残疾证","护照","身份证","军人证","残疾证","护照"};                //定义字符串
        JComboBox jcombo = new JComboBox(a);                                                //实例化下拉列表
        JList jlist = new JList(a);                                                            //实例化列表框
        //实例化JScrollPane面板
        JScrollPane jp = new JScrollPane(jlist);
        jp.setPreferredSize(new Dimension(100, 200));                        //设置JScrollPane大小
      //实例化JPanel面板
        JPanel jpp1 = new JPanel();
        jpp1.add(jcombo);
        
        con.add(BorderLayout.EAST,jp);
        con.add(BorderLayout.WEST,jpp1);
        this.setVisible(true);
        this.setBounds(50, 50, 300, 200);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);            //设置关闭方式,可以选择多种关闭玄子选项
    }
    public static void main(String[] args) {
        new jframe();
    }
}

Java窗口(JFrame)从零开始(7)——下拉列表+列表框_第1张图片

你可能感兴趣的:(Java窗口(JFrame)从零开始(7)——下拉列表+列表框)