Swing常用组件之单选按钮和复选框


本文为大家分享了Swing单选按钮和复选框的使用方法,供大家参考,具体内容如下

JRadioButton构造函数:

JRadioButton():建立一个新的JRadioButton.
JRadioButton(Icon icon):建立一个有图像但没有文字的JRadioButton.
JRadioButton(Icon icon,boolean selected):建立一个有图像但没有文字的JRadioButton,且设置其初始状态(有无被选取).
JRadioButton(String text):建立一个有文字的JRadioButton.
JRadioButton(String text,boolean selected):建立一个有文字的JRadioButton,且设置其初始状态(有无被选取)。
JRadioButton(String text,Icon icon):建立一个有文字且有图像的JRadioButton,初始状态为无被选取。
JRadioButton(String text,Icon icon,boolean selected):建立一个有文字且有图像的JRadioButton,且设置其初始状态(有无被选取)
要将RadioButton改成单选,我们必须用到ButtonGroup这个类。这个类位于javax.swing这个package下面,ButtonGroup类的主 要功能是:同一时间内只会有一个组件的状态为"on",其他皆为"off",也就是同一时间只有一个组件会被选取。而ButtonGroup类可 被AbstractButton下面的子类所使用,最常被使用的就是JRadioButton、JradioButtonMenu、Item与JToggleButton这些组件

ButtonGroup类的构造方法如下:

ButtonGroup()创建一个新的ButtonGroup()

ButtonGroup()类的常用的方法如下:
public void add(AbstractButton b):添加按钮到组中
public void clearSelection():清除选中内容,即没有选中按钮组中的任何按钮
pubic int getButtonCount():获取此组中的按钮数
public Enumeration<AbstractButton>getElemeent():获取此组中的所用按钮
public void remove (AbstractButton b):从按钮中删除按钮

JCheckBox构造函数

JCheckBox():建立一个新的JChcekBox.
JCheckBox(Icon icon):建立一个有图像但没有文字的JCheckBox.
JCheckBox(Icon icon,boolean selected):建立一个有图像但没有文字的JCheckBox,且设置其初始状态(有无被选取)。
JCheckBox(String text):建立一个有文字的JCheckBox.
JCheckBox(String text,boolean selected):建立一个有文字的JCheckBox,且设置其初始状态(有无被选取)。
JCheckBox(String text,Icon icon):建立一个有文字且有图像的JCheckBox,初始状态为无被选取。
JCheckBox(String text,Icon icon,boolean selected):建立一个有文字且有图像的JCheckBox,且设置其初始状态(有无被选取 )。
当JCheckBox中的选项被选取 或取消时,它会触发ItemEvent的事件,ItemEvent这个类共提供了4种方法可以使用,分别是getItem()、getItemSelectable()、 getStateChange()、paramString()。getItem()与paramString()方法会返回一些这个JCheckBox的状态值。一般我们较少用到这两 个方法。

getItemSelectable()相当于getSource()方法,一样都是返回触发事件的组件,用来判断是那个组件产生事件。getSource()方法是EventObject类所提供,而所有事件类都会继承这个类,因此所有的事件我们均能用getSource() 方法来判断到底是哪个组件触发了事件。

最后getStateChange()方法会返回此组件到底有没有被选取。这个方法会返回一个整数值。而我们可以用ItemEvent所提供的类 变量;若被选取则返回SELECTED,若没有被选取则返回DESELECTED.
单选按钮和复选框注册和注销ItemEvent事件监听器的方法如下:
public void addItemListener(ItemListener l): 注册指定的ItemListener事件监听器
public void removeItemListener(ItemListener l): 注销指定的ItemListener事件监听器

package ch10; 
 
import java.awt.event.*; 
 
import javax.swing.*; 
 
public class Vote extends JFrame implements ActionListener 
{ 
   private JPanel jp = new JPanel(); 
   JRadioButton jrb1 = new JRadioButton("这个网站很好,很新颖!",true); 
   JRadioButton jrb2 = new JRadioButton("这个网站很普通,太一般"); 
   JRadioButton jrb3 = new JRadioButton("这个网站很差,偶尔看一下"); 
   JRadioButton jrb4 = new JRadioButton("这个网站太差了,不来了"); 
   private JRadioButton[] jrb = new JRadioButton[]{jrb1,jrb2,jrb3,jrb4}; 
   private ButtonGroup bg = new ButtonGroup(); 
   JCheckBox jcb1 = new JCheckBox("界面比较漂亮"); 
   JCheckBox jcb2 = new JCheckBox("内容比较丰富"); 
   JCheckBox jcb3 = new JCheckBox("增值服务比较好"); 
   JCheckBox jcb4 = new JCheckBox("会员服务比较好"); 
   private JCheckBox[] jcb =new JCheckBox[]{jcb1,jcb2,jcb3,jcb4}; 
   private JButton [] jb = {new JButton("我要投票"),new JButton("我要重选")}; 
   private JLabel[] jl = {new JLabel("这个网站给你的印象是:"),new JLabel("您认为本站哪里做的比较好"),new JLabel("您投票的内容是:")}; 
   private JTextArea jt = new JTextArea(); 
   private JScrollPane js= new JScrollPane(jt); 
   public Vote() 
   { 
     jp.setLayout(null); 
     for(int i=0;i<4;i++) 
     { 
       jrb[i].setBounds(30+170*i,40,170,30); 
       jcb[i].setBounds(30+120*i,100,120,30); 
       jp.add(jrb[i]); 
       jp.add(jcb[i]); 
       jcb[i].addActionListener(this); 
       jrb[i].addActionListener(this); 
       bg.add(jrb[i]); 
       if(i>1) 
         continue; 
       jl[i].setBounds(20,20+50*i,200,30); 
       jb[i].setBounds(380+120*i,200,100,20); 
       jp.add(jl[i]); 
       jp.add(jb[i]); 
       jb[i].addActionListener(this); 
     } 
     jl[2].setBounds(20,150,120,30); 
     jp.add(jl[2]); 
     js.setBounds(120,150,500,50); 
     jp.add(js); 
     jt.setLineWrap(true); 
     jt.setEditable(false); 
     this.add(jp); 
     this.setTitle("网站满意调查表"); 
     this.setBounds(150,150,750,300); 
     this.setVisible(true); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
   } 
   public void actionPerformed(ActionEvent a) 
   { 
     if(a.getSource()==jb[1]) 
     { 
       bg.clearSelection(); 
       for(int i=0;i<jcb.length;i++) 
         jcb[i].setSelected(false); 
       jt.setText(""); 
     } 
     else 
     { 
       StringBuffer temp1 = new StringBuffer("你认为这个网站"); 
       StringBuffer temp2 = new StringBuffer(""); 
       for(int i=0;i<4;i++) 
       { 
         if(jrb[i].isSelected()) 
           temp1.append(jrb[i].getText()); 
         if(jcb[i].isSelected()) 
           temp2.append(jcb[i].getText()+","); 
       } 
       if(temp2.length()==0) 
         jt.setText("请将两项调查都选择"); 
       else 
       { 
         temp1.append("你认为这个网站"); 
         temp1.append(temp2.substring(0,temp2.length()-1)); 
         jt.setText(temp1.toString()); 
       } 
     } 
   } 
   public static void main(String args[]) 
   { 
     new Vote(); 
   } 
}

效果图:


以上就是本文的全部内容,希望对大家的学习有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3