ButtonGroup group = new ButtonGroup();
JRadioButton sex1 = new JRadioButton("男");
JRadioButton sex2 = new JRadioButton("女");
JRadioButton sex3 = new JRadioButton("保密");
group.add(sex1);
gruop.add(sex2);
group.add(sex3);
//下面是自己建立的MyPanel类package com.qq.common;import java.awt.Graphics;import javax.swing.ImageIcon;
import javax.swing.JPanel;class MyPanel extends JPanel
{
public MyPanel(ImageIcon i)
{
super();
icon = i;
}
ImageIcon icon;
public void paintComponent(Graphics g){
//super.paintComponent(g);
int x=0,y=0;
if(icon==null)
return;
g.drawImage(icon.getImage(),x,y,getSize().width,getSize().height,this);
while(true)
{
g.drawImage(icon.getImage(),x,y,this);
if(x>getSize().width && y>getSize().height)break; //这段代码是为了保证在窗口大于图片时,图片仍能覆盖整个窗口
if(x>getSize().width)
{
x=0;
y+=icon.getIconHeight();
}
else
x+=icon.getIconWidth();
}
}
}
gruop.add(sex2); //单词写错,应为group
group.add(sex3);还有添加至容器时,注意别直接添加ButtonGroup的实例,因ButtonGroup直接继承Object.
添加的时候还是添加 JRadioButton
如panel.add(sex1);等。