2 onepinch onePinch 于 2014.09.13 18:05 提问

为JRadioButton添加动态图标

试着为两个radioButton添加gif图标,发现图标第一次加载时会动,之后就动不了,且有一个会消失,请问是什么原因??

代码:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class RunFrame extends JFrame {
/**

  • */ private static final long serialVersionUID = 1L;

public RunFrame() {
final Icon icon1= new ImageIcon("C:/Users/Administrator/Desktop/1.gif");
final Icon icon2 = new ImageIcon("C:/Users/Administrator/Desktop/2.gif");
setLayout(new FlowLayout(0, 10, 10));
Container c = getContentPane();

final JRadioButton jr1 = new JRadioButton("男", true);
final JRadioButton jr2 = new JRadioButton("女", true);
jr1.setFocusPainted(false);
jr2.setFocusPainted(false);
ButtonGroup bg = new ButtonGroup();
bg.add(jr1);
bg.add(jr2);
jr1.setIcon(icon2);
jr2.setIcon(icon1);

jr1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jr1.setIcon(icon1);
jr2.setIcon(icon2);
jr1.setSelected(true);
jr2.setSelected(false);
}
});

jr2.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jr2.setIcon(icon1);
jr1.setIcon(icon2);
jr2.setSelected(true);
jr1.setSelected(false);
}
});

c.add(jr1);
c.add(jr2);
setTitle("new Frame");
setBounds(300,200, 250, 100);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}

public static void main(String args[]) {
new RunFrame();
}
}

看到的都帮一下忙吧,十分感谢~
附上两个gif图标按钮:
CSDN移动问答

CSDN移动问答

Csdn user default icon
上传中...
上传图片
插入图片