選擇框、單選框和單選按鈕都是選擇組件,選擇組件有兩種狀態,一種是選中(on),另一種是未選中(off),它們提供一種簡單的“on/off ”選擇功能,讓用戶在一組選擇項目中作選擇。
選擇框
選擇框(JCheckBox)的選中與否開狀是一個小方框,被選中則在框中打勾。當在一個容器中有多個選擇框,同時可以有多個選擇框被選中,這樣的選擇框也稱複選框。與選擇框相關的接口是ItemListener,事件類是ItemEvent。
JCheckBox類常用的構造方法有以下3個:
1.JCheckBox():用空標題構造選擇框。
2.JCheckBox(String s):用給定的標題s構造選擇框。
3.JCheckBox(String s, boolean b):用給定的標題s構造選擇框,參數b設置選中與否的初始狀態。
JCheckBox類的其他常用方法如下:
1.getState():獲取選擇框的狀態。
2.setState(boolean b):設置選擇框的狀態
3.getLabel():獲取選擇框的標題。
4.setLabel(String s):設置選擇框的標題。
5.isSelected():獲取選擇框是否被選中的狀態。
6.itemStateChanged(ItemEvent e):處理選擇框事件的接口方法。
7.getItemSelectable():獲取可選項,獲取事件源。
8.addItemListener(ItemListener l):為選擇框設定監視器。
9.removeItemListener(ItemListener l):移去選擇框的監視器。
【例11-11】聲明一個面板子類,面板子類對像有3個選擇框。
class Panel1 extends JPanel{
JCheckBox box1,box2,box3;
Panel1(){
box1 = new JCheckBox(“足球”);
box2 = new JCheckBox(“排球”);
box2 = new JCheckBox(“籃球”);
}
}
單選框
當在一個容器中放入多個選擇框,且沒有ButtonGroup對象將它們分組,則可以同時選中多個選擇框。如果使用ButtonGroup對象將選擇框分組,同一時刻組內的多個選擇框只允許有一個被選中,稱同一組內的選擇框為單選框。單選框分組的方法是先創建ButtonGroup對象,然後將希望為同組的選擇框添加到同一個ButtonGroup對像中。參見例6.2程序的面板子類Panel2的聲明,組內有3個單選框。
單選按鈕
單選按鈕(JRadioButton)的功能與單選框相似。使用單選按鈕的方法是將一些單選按鈕用ButtonGroup對象分組,使同一組的單選按鈕只允許有一個被選中。單選按鈕與單選框的差異是顯示的樣式不同,單選按鈕是一個圓形的按鈕,單選框是一個小方框。
JRadioButton類的常用構造方法有以下幾個:
1.JRadioButton():用空標題構造單選按鈕。
2.JRadioButton(String s):用給定的標題s構造單選按鈕。
3.JRadioButton(String s,boolean b):用給定的標題s構造單選按鈕,參數b設置選中與否的初始狀態。
單選按鈕使用時需要使用ButtonGroup將單選按鈕分組,單選按鈕的分組方法是先創建對象,然後將同組的單選按鈕添加到同一個ButtonGroup對像中。參見例6.2程序的子類panel1的聲明,組內有3個單選按鈕。
選擇項目事件處理
用戶對選擇框或單選按鈕做出選擇後,程序應對這個選擇作出必要的響應,程序為此要處理選擇項目事件。選擇項目處理程序的基本內容有:
1.監視選擇項目對象的類要實現接口ItemListener,
2.程序要聲明和建立選擇對象,
3.為選擇對象註冊監視器,
4.編寫處理選擇項目事件的接口方法itemStateChanged(ItemEvent e),在該方法內用getItemSelectable()方法獲取事件源,並作相應處理。
【例11-12】處理選擇項目事件的小應用程序。一個由3個單選按鈕組成的產品選擇組,當選中某個產品時,文本區將顯示該產品的信息。一個由3個選擇框組成的購買產品數量選擇框組,當選擇了購買數量後,在另一個文本框顯示每台價格。
import java.applet.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; class Panel1 extends JPanel{ JRadioButton box1,box2,box3; ButtonGroup g; Panel1(){ setLayout (new GridLayout(1,3)); g = new ButtonGroup(); box1 = new JRadioButton(MyWindow.fName[0]+"計算機",false); box2 = new JRadioButton(MyWindow.fName[1]+"計算機",false); box3 = new JRadioButton(MyWindow.fName[2]+"計算機",false); g.add(box1);g.add(box2);g.add(box3); add(box1); add(box2);add(box3); add(new JLabel("計算機3選1") ); } } class Panel2 extends JPanel{ JCheckBox box1,box2,box3; ButtonGroup g; Panel2(){ setLayout(new GridLayout( 1,3)); g = new ButtonGroup(); box1 = new JCheckBox("購買1台"); box2 = new JCheckBox("購買2台"); box3 = new JCheckBox("購買3台"); g .add(box1);g.add(box2);g.add(box3); add(box1);add(box2);add(box3); add(new JLabel(" 選擇1、2或3")) ; } } class MyWindow extends JFrame implements ItemListener{ Panel1 panel1; Panel2 panel2; JLabel label1,label2; JTextArea text1,text2; static String fName[] = {"HP","IBM","DELL"}; static double priTbl[ ][]={{1.20,1.15,1.10},{1.70,1.65,1.60},{1.65,1.60,1.58}}; static int productin = -1; MyWindow(String s){ super(s); Container con = this.getContentPane(); con.setLayout(new GridLayout(3,2)); this.setLocation(100,100); this.setSize(400,100); panel1 = new Panel1();panel2 = new Panel2(); label1 = new JLabel("產品介紹",JLabel.CENTER); label2 = new JLabel("產品價格",JLabel.CENTER); text1 = new JTextArea();text2 = new JTextArea(); con.add(label1);con .add(label2);con.add(panel1); con.add(panel2);con.add(text1);con.add(text2); panel1.box1.addItemListener(this); panel1.box2.addItemListener(this ); panel1.box3.addItemListener(this); panel2.box1.addItemListener(this); panel2.box2.addItemListener(this); panel2.box3.addItemListener(this); this.setVisible(true);this.pack() ; } public void itemStateChanged(ItemEvent e){ //選項狀態已改變if(e.getItemSelectable()==panel1.box1){ //獲取可選項production =0; text1.setText(fName[0]+"公司生產");text2.setText(""); } else if(e.getItemSelectable()==panel1.box2){ production =1; text1.setText(fName[1]+"公司生產");text2.setText (""); } else if(e.getItemSelectable()==panel1.box3){ production =2; text1.setText(fName[2]+"公司生產");text2.setText(""); } else { if(production ==-1) return; if(e.getItemSelectable()==panel2.box1){ text2.setText(""+priTbl[production][0]+"萬元/台"); } else if(e.getItemSelectable()==panel2.box2){ text2.setText(""+priTbl[production][1]+"萬元/台"); } else if(e.getItemSelectable()==panel2. box3){ text2.setText(""+priTbl[production][2]+"萬元/台"); } } } } public class Example6_2 extends Applet{ MyWindow myWin = new MyWindow("選擇項目處理示例程序") ; }以上所述就是本文的全部內容了,希望大家能夠喜歡。