在单选button操作中。能够使用ItemListener接口进行事件的监听。
package com.beyole.util;import java.awt.Container;import java.awt.GridLayout;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import javax.swing.BorderFactory;import javax.swing.ButtonGroup;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JRadioButton;class MyRadio1 implements ItemListener { private String right = "f:" + File.separator + "2.png";// 定义图片路径 private String wrong = "f:" + File.separator + "3.png";// 定义图片路径 private JFrame frame = new JFrame("Beyole");// 定义窗口 private Container container = frame.getContentPane();// 得到窗口容器 private JRadioButton jb1 = new JRadioButton("男", new ImageIcon(right), true); private JRadioButton jb2 = new JRadioButton("女", new ImageIcon(wrong), false); private JPanel panel = new JPanel();// 定义一个面板 public MyRadio1() { panel.setBorder(BorderFactory.createTitledBorder("选择性别"));// 定义一个面板的边框显示条 panel.setLayout(new GridLayout(1, 3));// 定义排版,一行三列 ButtonGroup group = new ButtonGroup();// 定义一个button组 group.add(this.jb1);// 将单选button组增加到一个组 group.add(this.jb2);// 将单选button组增加到一个组 panel.add(this.jb1);// 将单选button增加到面板其中 panel.add(this.jb2);// 将单选button增加到面板其中 jb1.addItemListener(this);// 增加监听事件 jb2.addItemListener(this);// 增加监听事件 container.add(panel);// 增加面板到容器中 this.frame.setSize(200, 100); this.frame.setVisible(true); this.frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { System.exit(1); } }); } public void itemStateChanged(ItemEvent e) { if (e.getSource() == jb1) { jb1.setIcon(new ImageIcon(right)); jb2.setIcon(new ImageIcon(wrong)); } else { jb2.setIcon(new ImageIcon(right)); jb1.setIcon(new ImageIcon(wrong)); } }}public class JRadioButtonDemo1 { public static void main(String[] args) { new MyRadio1(); }}以上程序。使用ImageIcon设置了两个单选button的图片,每次选项改变后都会触发itemStateChanged事件,之后改动每一个选项的显示图片。
程序截图: