博為峰Java技術(shù)文章 ——JavaSE Swing 單選按鈕Ⅰ

博為峰小博老師

單選按鈕被稱為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);

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容