Java圖形界面——下拉框、列表框、滾動窗格組件

package com.test.swing;

import javax.swing.*;
import java.awt.*;

public class Test0 extends JFrame{
	JList jlist;	//列表框
	JComboBox jcb;	//下拉框
	JPanel jp1, jp2;	//面板
	JLabel jlb1, jlb2;
	JScrollPane jsp;	//滾動控件
	
	//構造函數
	public Test0(){
		jp1 = new JPanel();
		jp2 = new JPanel();
		
		jlb1 = new JLabel("你的水平:");
		String str1[] = {"巨坑", "菜鳥", "一般", "大神"};
		jcb = new JComboBox(str1);
		
		jlb2 = new JLabel("選擇英雄:");
		String str2[] = {"蓋侖", "艾希", "提莫", "趙信", "李青", "安妮"};
		jlist = new JList(str2);
		jlist.setVisibleRowCount(2);//默認顯示行數
		jsp = new JScrollPane(jlist);
		
		jp1.add(jlb1);
		jp1.add(jcb);
		
		jp2.add(jlb2);
		jp2.add(jsp);
		
			//網格佈局2行一列
		this.setLayout(new GridLayout(2, 1));
		
		this.add(jp1);
		this.add(jp2);
		
		this.setSize(200,200);
		this.setTitle("組件演示");
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		Test0 test0 = new Test0();

	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章