日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

JAVA之获取JavaSwing单选框JRadioButton选中的值(内容)

發布時間:2024/7/23 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA之获取JavaSwing单选框JRadioButton选中的值(内容) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JAVA之獲取JavaSwing單選框JRadioButton選中的值(內容)

package word;import java.awt.BorderLayout; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.print.attribute.standard.Severity; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton;public class www extends JFrame{JButton btn;private static JPanel pane1 ;public www() {JFrame frame = new JFrame("單選框"); //頂層容器frame.setSize(200, 200); //窗口大小pane1 = new JPanel(); //中間容器//單選框JRadioButton c1 = new JRadioButton("草莓",true);//創建單選框,true為默認選中,不需要可去掉JRadioButton c2 = new JRadioButton("檸檬");//創建單選框JRadioButton c3 = new JRadioButton("香蕉");//創建單選框ButtonGroup group = new ButtonGroup(); //創建單選框按鈕組JLabel l1 = new JLabel("模式:");group.add(c1);//將單選框組件加入單選框按鈕組,否則兩個都可以選擇group.add(c2);group.add(c3);pane1.add(l1);pane1.add(c1);//將單選框組件加入面板pane1.add(c2);pane1.add(c3);//按鈕btn = new JButton("你選擇的是:");pane1.add(btn);//將按鈕加入面板frame.add(pane1);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);//顯示btn.addActionListener(new ActionListener() {//按鈕監聽@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString info ="";//通過面板屬性名獲取到該面板上的所有組件System.out.println(info);for(Component c:pane1.getComponents()){if(c instanceof JRadioButton){if(((JRadioButton) c).isSelected()){info += ((JRadioButton)c).getText();}}}System.out.println(info);//輸出選擇的單選框文本JOptionPane.showMessageDialog(null, "你選擇了"+info);}});}public static void main(String[] args) {// TODO Auto-generated method stubnew www();}}

輸出為:

總結

以上是生活随笔為你收集整理的JAVA之获取JavaSwing单选框JRadioButton选中的值(内容)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。