單選按鈕被稱為RadioButton棵譬,它通過JRadioButton類實(shí)現(xiàn)佛点,例如在一些管理軟件中會(huì)出現(xiàn)“性別”單選按鈕,通過選擇不同的單選按鈕來實(shí)現(xiàn)不同性別的選擇。要使用單選按鈕咏雌,首先必須創(chuàng)建它凡怎,而后再通過其內(nèi)置方法來操縱組件,所以下圖將給出單選按鈕的構(gòu)造器赊抖。
下面將通過實(shí)例來演示如何使用單選按鈕统倒。其代碼如下所示:
/**
*創(chuàng)建單選按鈕組件,將按鈕組件添加到內(nèi)容面板中去
*/
publicclassBwfJRadioButton {
publicstaticintwidth=300;
publicstaticintheight=200;
publicstaticvoidmain(String args[]){
JFrame jf=newJFrame("添加單選按鈕組件");
jf.setSize(width,height);
JPanel contentPane=newJPanel();
JRadioButton jr1=newJRadioButton("忽略");//創(chuàng)建單選按鈕
JRadioButton jr2=newJRadioButton("繼續(xù)");
JRadioButton jr3=newJRadioButton("跳過");
contentPane.add(jr1);//將按鈕添加到內(nèi)容面板中
contentPane.add(jr2);
contentPane.add(jr3);
jf.setContentPane(contentPane);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}