

1 2 import javax.swing.*; 3 4 import java.awt.*; 5 class Win extends JFrame 6 { 7 JTextField mytext; // 設置一個文本區 8 JButton mybutton; 9 JCheckBox mycheckBox[]; 10 JRadioButton myradio[]; 11 ButtonGroup group; //為一組按鈕創建相坼的功能 12 JComboBox myComboBox; 13 JTextArea myText; 14 public Win(){} ; //設置一個構造函數 15 public Win(String str ,int x,int y,int h,int w) //設置一個自定義的構造函數 16 { 17 setinit(str); 18 setBounds(x,y,h,w); //對其進行位置大小的更改 19 setVisible(true); //設置其是否可見 20 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //退出并關閉窗口 21 } 22 void setinit(String str) 23 { 24 setTitle(str); //跟文本加一個標題 25 //設置一個布局 26 setLayout(new FlowLayout(FlowLayout.LEFT)); //設置一個布局FlowLayout流布局,向左對齊 27 add(new Label("文本框")); //添加一個label 28 mytext = new JTextField(10); 29 add(mytext); 30 add(new Label("按鈕")); 31 mybutton = new JButton("確定"); 32 add(mybutton); 33 mycheckBox = new JCheckBox [3]; //運用數組實現吧! 34 String title[] ={"音樂","旅游","籃球"}; 35 add( new Label("選擇框") ); 36 for( int i=0 ; i<3 ; i++ ) 37 { 38 mycheckBox[i] = new JCheckBox("喜歡"+title[i]); 39 add(mycheckBox[i]); 40 } 41 add( new Label("單選按鈕")); 42 myradio =new JRadioButton [2]; 43 group = new ButtonGroup(); 44 String mystr[] = {"男","女"}; 45 for(int i=0;i<2;i++) 46 { 47 myradio[i] = new JRadioButton( mystr[i] ); 48 group.add(myradio[i]); 49 add(myradio[i]); 50 } 51 add( new Label("下拉列表")); 52 myComboBox = new JComboBox(); //創建一個下拉菜單 53 String substr[] ={"音樂天地","武術天地","象棋樂園"}; 54 for(int i=0 ; i<3 ;i++) 55 myComboBox.addItem(substr[i]); 56 add(myComboBox); 57 add( new Label("文本區:")); 58 myText = new JTextArea(6,12); 59 add( new JScrollPane(myText)); 60 } 61 } 62 63 public class gong 64 { 65 public static void main(String args[]) 66 { 67 Win mywin = new Win("Demo",100,100,330,290); 68 } 69 }