java Swing组件练习三
程序员文章站
2024-01-31 13:33:04
...
/**
* JComboBox 下拉框组件
* JList 列表框组件
* JScrollPane 滚动窗格组件
*/
package 韩顺平;
import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Demo4 extends JFrame{
//定义组件
JPanel jp1,jp2;
JLabel jl1,jl2;
JComboBox jcb1;
JList jlist;
JScrollPane jsp;
public static void main(String[] args) {
// TODO 自动生成的方法存根
Demo4 d4 = new Demo4();
}
//构造方法
public Demo4() {
jp1 = new JPanel();
jp2 = new JPanel();
jl1 = new JLabel("你的籍贯");
jl2 = new JLabel("旅游景点");
String[] jg = {"河南省","河北省","湖北省","湖南省"};
jcb1 = new JComboBox(jg);
String[] dd = {"九寨沟","*","故宫","长城","西湖"};
jlist = new JList(dd);
//设置列表框显示的选项的个数
jlist.setVisibleRowCount(3);
//把jlist加入到jsp中
jsp = new JScrollPane(jlist);
//设置布局
this.setLayout(new GridLayout(3, 1));
//添加组件
jp1.add(jl1);
jp1.add(jcb1);
jp2.add(jl2);
jp2.add(jsp);
this.add(jp1);
this.add(jp2);
this.setTitle("欢迎");
this.setSize(400, 300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}