下拉列表和滚动条的使用
下拉列表和滚动条的使用
每当我准备认真看书的时候,这个世界就变得非常有趣,任何风吹草动都能吸引我
下拉列表JComboBox的使用
1、创建一个数组
2、new一个JComboBox,将数组传进来
滚动条JScrollPane的使用
1、创建一个数组
2、new一个JList,将数组传进来
3、设置Jlist的可见列数setVisibleRowCount(2);
4、new一个JScrollPane,将list传进来
5、添加JScrollPane到Jpanel或者JFrame
package com.layout.test;
import javax.swing.*;
import java.awt.*;
public class ComboBoxTest extends JFrame {
JPanel p1,p2;
JLabel la1,la2;
JComboBox box;
JList list;
JScrollPane scroll;
public static void main(String[] args) {
ComboBoxTest test=new ComboBoxTest();
}
ComboBoxTest() {
p1 = new JPanel();
p2 = new JPanel();
la1 = new JLabel();
la2 = new JLabel();
String[] erea = {"北京", "上海", "广州"};
box = new JComboBox(erea);
String[] job = {"医生", "教师", "公务员", "自由职业"};
list = new JList(job); /new一个Jlist
list.setVisibleRowCount(2); //设置Jlist的可见列数
scroll = new JScrollPane(list); //new一个JScrollPane,讲JList传进来
this.setLayout(new GridLayout(2, 1));
p1.add(la1);
p1.add(box);
p2.add(la2);
p2.add(scroll); //将滚动条加入面板
this.add(p1);
this.add(p2);
this.setTitle("下拉列表和滚动条");
this.setSize(300, 300);
this.setLocation(300, 300);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
结果: