//下拉列表和滾動條 import java.awt.*; import javax.swing.*; public class Jiemian7 extends JFrame{ JPanel mb1,mb2; //面板定義 JLabel bq1,bq2; //標籤訂義 JComboBox xlk; //下拉框 JList lb; //單選按鈕定義 JScrollPane gd; //滾動條 public static void main(String[] args){ //運行本類的構造方法 Jiemian7 jiemian = new Jiemian7(); } public Jiemian7(){ //建立面板 mb1 = new JPanel(); mb2 = new JPanel(); bq1 = new JLabel("籍貫"); bq2 = new JLabel("學歷"); String[] jg = {"北京","天津","上海","重慶"}; xlk = new JComboBox(jg); String[] xl = {"高中","大專","本科","碩士","博士"}; lb = new JList(xl); lb.setVisibleRowCount(3); //滾動多少行 gd=new JScrollPane(lb); //和單選按鈕不一樣,這裏要添加滾動組件到面板 //使用網格佈局 this.setLayout(new GridLayout(2,1)); //向面板添加按鈕 mb1.add(bq1); mb1.add(xlk); mb2.add(bq2); mb2.add(gd); //向界面添加面板 this.add(mb1); this.add(mb2); //設置窗口標題 this.setTitle("下拉列表和滾動條"); //設置窗口的寬高 this.setSize(300,150); //設置窗口出現對於屏幕的位置 this.setLocation(100,100); //禁止拉大拉小 this.setResizable(false); //關閉窗口後釋放資源 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //輸出窗口 this.setVisible(true); } }