import java.awt.Container; import java.awt.FlowLayout; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JApplet; import javax.swing.JRadioButton;public class Test extends JApplet { public void init() { Container contentPane = getContentPane(); Icon icon = new ImageIcon(getImage(getCodeBase(),"bulb.gif")); JRadioButton[] radioButtons = new JRadioButton[] { new JRadioButton(), new JRadioButton(icon), new JRadioButton(icon,true), new JRadioButton("idea!"), new JRadioButton("idea!",true), new JRadioButton("idea!",icon), new JRadioButton("idea!",icon,true) }; contentPane.setLayout(new FlowLayout()); for(int i = 0;i < radioButtons.length;++i) { radioButtons[i].setBorderPainted(true); contentPane.add(radioButtons[i]); if(radioButtons[i].getIcon() != null) { System.out.println("setting selected icon"); radioButtons[i].setSelectedIcon(new ImageIcon(getImage(getCodeBase(),"bulb_bright.gif"))); } } } }
according to liad, the icon we add will hold a very wide space even if setting a width property for img,td,or tr html tag. Also, I use img tag directly,but the same result.....for YuLimin(阿敏当兵) : the method tells us how to change icon ,not to set a new icon together with default round icon.
<![CDATA[ Sorry,Chinese Input failed!!!!!!!!!!!Set a icon for a JRadioButton component meeting the following: 1: a icon including 2: text excluded and keeing radiobutton's original round check box to indicate whether checked or not. High score is available for good answer ]]>
import java.awt.FlowLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JRadioButton;public class Test extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
Icon icon = new ImageIcon(getImage(getCodeBase(),"bulb.gif"));
JRadioButton[] radioButtons = new JRadioButton[]
{
new JRadioButton(),
new JRadioButton(icon),
new JRadioButton(icon,true),
new JRadioButton("idea!"),
new JRadioButton("idea!",true),
new JRadioButton("idea!",icon),
new JRadioButton("idea!",icon,true)
};
contentPane.setLayout(new FlowLayout()); for(int i = 0;i < radioButtons.length;++i)
{
radioButtons[i].setBorderPainted(true);
contentPane.add(radioButtons[i]); if(radioButtons[i].getIcon() != null)
{
System.out.println("setting selected icon");
radioButtons[i].setSelectedIcon(new ImageIcon(getImage(getCodeBase(),"bulb_bright.gif")));
}
}
}
}
even if setting a width property for img,td,or tr html tag.
Also, I use img tag directly,but the same result.....for YuLimin(阿敏当兵) :
the method tells us how to change icon ,not to set a new icon
together with default round icon.
http://javaalmanac.com/egs/javax.swing/checkbox_CustIcon.html
一个是我们配置的,用来显示按钮含义;但是我们定义的图标将占据
很宽的空间,不管怎样设置大小,都无法避免.按照后面两者的说法,只能放一个图标.
radioButton.setText("<html><img src=a image file name>");
可以保留原来的圆形按钮,并将我们的图标放到后面,但是问题是:
我们的这个图标将尽可能地占据剩余空间.如果将这个按钮与其他普通按钮
加到一个工具栏中,我们再最大化窗口,就会发现这个按钮占据了庞大的空间
宽度!!!!!!这是为什么呢?这个按钮变得象glue一样!!!
Sorry,Chinese Input failed!!!!!!!!!!!Set a icon for a JRadioButton component meeting the following:
1: a icon including
2: text excluded
and keeing radiobutton's original round check box to indicate whether checked or not. High score is available for good answer
]]>
toolBar.setLayout(new FlowLayout(FlowLayout.LEFT));
// toolBar.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 0));