一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java Swing組件單選框JRadioButton用法示例

Java Swing組件單選框JRadioButton用法示例

2021-02-05 11:47pzy4447 Java教程

這篇文章主要介紹了Java Swing組件單選框JRadioButton用法,結合具體實例形式分析了Swing單選框JRadioButton的使用方法及相關操作注意事項,需要的朋友可以參考下

本文實例講述了java swing組件單選框jradiobutton用法。分享給大家供大家參考,具體如下:

jradiobutton是swing中的單選框。所謂單選框是指,在同一個組內雖然有多個單選框存在,然而同一時刻只能有一個單選框處于選中狀態。它就像收音機的按鈕,按下一個時此前被按下的會自動彈起,故因此得名。因此,在添加jradiobutton控件時,要記得將它們添加到同一個buttongroup中。

jradiobutton的常用方法如下圖所示:

Java Swing組件單選框JRadioButton用法示例

可以為它添加actionlistener對象來響應事件。這里有一個問題,當多個jradiobutton共用一個事件監聽器時,如何獲取產生事件的按鈕?

有4種方法:

1.遍歷這些按鈕并檢查是否選中,這種方法比較笨重。

2.使用事件的getactioncommand()方法,這需要事先為每個控件設置actioncommand

3.使用事件的getsource,并轉化為控件對象。

4.使用buttongroupgetselection方法,它返回的并不是控件,而是那個控件的buttonmodel,需再次調用buttonmodel的getactioncommand方法。

使用demo如下:

jradiobuttondemo.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package awtdemo;
import java.awt.borderlayout;
import java.awt.font;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.util.hashmap;
import java.util.map;
import javax.swing.buttongroup;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.jpanel;
import javax.swing.jradiobutton;
/*
 * source code from 《java核心技術 卷1 基礎知識》 p329
 */
@suppresswarnings("serial")
public class jradiobuttondemo extends jframe {
  int default_width = 600;
  int default_height = 400;
  private jlabel label;
  private jpanel buttonpanel;
  private buttongroup group;
  private static final int default_size = 12;
  private map actioncommandsizemap = new hashmap();
  // 二維數組存儲按鈕屬性,第一維是按鈕名稱,第二維是字體大小
  private string[][] buttonattributes = {
      { "small", "medium", "large", "extra" }, { "8", "12", "18", "36" } };
  public jradiobuttondemo() {
    settitle("jradiobuttondemo - m.ythuaji.com.cn");
    setsize(default_width, default_height);
    // 添加label
    label = new jlabel("歡迎訪問服務器之家 m.ythuaji.com.cn");
    label.setfont(new font("serif", font.plain, default_size));
    add(label, borderlayout.center);
    // 添加buttonpanel,它包含4個radiobutton
    buttonpanel = new jpanel();
    group = new buttongroup();
    add(buttonpanel, borderlayout.south);
    // 添加radiobutton
    for (int i = 0; i < buttonattributes[0].length; i++) {
      addradiobutton(buttonattributes[0][i],
          integer.parseint(buttonattributes[1][i]));
      // 將按鈕名稱和字體大小添加為對應表,名稱等同于actioncommand
      actioncommandsizemap.put(buttonattributes[0][i],
          integer.parseint(buttonattributes[1][i]));
    }
  }
  public void addradiobutton(string name, final int size) {
    boolean selected = size == default_size;
    jradiobutton button = new jradiobutton(name, selected);
    button.setactioncommand(name);// 設置name即為actioncommand
    group.add(button);
    buttonpanel.add(button);
    // 構造一個監聽器,響應checkbox事件
    actionlistener actionlistener = new actionlistener() {
      public void actionperformed(actionevent e) {
        // 1.通過eactioncommand
        string eactioncommand = e.getactioncommand();
        system.out.printf("e.getactioncommand() is %s\n",
            eactioncommand);
        // 2.通過getsource()
        object sourceobject = e.getsource();
        if (sourceobject instanceof jradiobutton) {
          jradiobutton sourcebutton = (jradiobutton) sourceobject;
          system.out.printf("selected jradiobutton is %s\n",
              sourcebutton.gettext());
        }
        // 3.通過groupselectionactioncommand
        string groupselectionactioncommand = group.getselection()
            .getactioncommand();
        system.out.printf("groupselectionactioncommand is %s\n",
            groupselectionactioncommand);
        label.setfont(new font("serif", font.plain,
            actioncommandsizemap.get(groupselectionactioncommand)));
      }
    };
    button.addactionlistener(actionlistener);
  }
  public static void main(string[] args) {
    // todo auto-generated method stub
    // 創建窗體并指定標題
    jradiobuttondemo frame = new jradiobuttondemo();
    // 關閉窗體后退出程序
    frame.setdefaultcloseoperation(jframe.exit_on_close);
    // 自動適配所有控件大小
    // frame.pack();
    // 設置窗體位置在屏幕中央
    frame.setlocationrelativeto(null);
    // 顯示窗體
    frame.setvisible(true);
  }
}

運行效果如下:

Java Swing組件單選框JRadioButton用法示例

希望本文所述對大家java程序設計有所幫助。

原文鏈接:http://www.cnblogs.com/pzy4447/p/4641101.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 五月天综合久久 | 俄罗斯13一14处出血视频在线 | 亚州在线播放 | 精品国产麻豆免费人成网站 | 国产探花在线观看 | 日韩欧美在线一区二区三区 | 日韩一区二区三区不卡视频 | 99久久香蕉 | videos护士有奶水 | 久久久久青草大香线综合精品 | 日本一区二区三区在线 视频 | 青青网| 99久9在线视频 | 小sao货水好多真紧h的视频 | www在线看| 男人叼女人的痛爽视频免费 | 国产精品2| 青青草在线观看 | 色综合久久98天天综合 | 国模李丽莎大尺度啪啪 | 荷兰艾优apiyoo | 精品人人做人人爽久久久 | 日韩在线观看免费 | 国产麻豆麻豆 | 亚洲国产综合久久久无码色伦 | 2020最新版的ab片 | 操破苍穹小说 | 大陆黄色片| 亚洲精品第一国产综合 | 日韩成本大片35分钟免费播放 | 欧洲一级| 娇妻与公陈峰姚瑶最新版 | 精品夜夜澡人妻无码AV蜜桃 | 日本熟hdx| 精品在线免费观看视频 | 911精品国产亚洲日本美国韩国 | 国产精品密播放国产免费看 | 国产精品久久久久久久久免费 | 亚洲国产综合久久精品 | hezyo加勒比一区二区三区 | wc凹凸撒尿间谍女厕hd |