JRadioButton的名字?是指JRadioButton的文本即text,还是该JRadioButton的组件名? 若是组件名则应用 public String getName()获得组件的名称。
用getText()方法,楼上几位已经说了
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class HH extends JFrame implements ActionListener { public JRadioButton jb1 ; public JRadioButton jb2 ; public JRadioButton jb3 ; public JLabel jl1;
public HH() { ButtonGroup bg = new ButtonGroup(); jb1 = new JRadioButton("Button1"); jb1.addActionListener(this); jb2 = new JRadioButton("Button2"); jb2.addActionListener(this); jb3 = new JRadioButton("Button3"); jb3.addActionListener(this); bg.add(jb1); bg.add(jb2); bg.add(jb3); setSize(300,300); setLayout(new GridLayout(1,2)); JPanel jpbutton = new JPanel(new GridLayout(3,1)); jpbutton.add(jb1); jpbutton.add(jb2); jpbutton.add(jb3);
jl1 = new JLabel("Hehe"); add(jpbutton); add(jl1); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); setVisible(true);
} public void actionPerformed(ActionEvent e) { jl1.setText("Name Button :"+((JRadioButton)e.getSource()).getLabel());
} public static void main(String args[]) { HH hh = new HH();
楼主【jidushanbojue】截止到2008-07-22 11:44:57的历史汇总数据(不包括此帖):
发帖的总数量:19 发帖的总分数:270 每贴平均分数:14
回帖的总数量:17 得分贴总数量:2 回帖的得分率:11%
结贴的总数量:19 结贴的总分数:270
无满意结贴数:9 无满意结贴分:200
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:47.37 % 无满意结分率:74.07 %
敬礼!
Enumeration<AbstractButton> ec = buttonGroup.getElements();
JRadioButton button; while(ec.hasMoreElements())
{
button = (JRadioButton)ec.nextElement(); if(button.isSelected())
{
System.out.println(button.getText());
}
}
Enumeration<AbstractButton> ec = buttonGroup.getElements();
JRadioButton button; while(ec.hasMoreElements())
{
button = (JRadioButton)ec.nextElement(); if(button.isSelected())
{
System.out.println(button.getText());
break;
}
}
JRadioButton button; while(ec.hasMoreElements())
{
button = (JRadioButton)ec.nextElement(); if(button.isSelected())
{
System.out.println(button.getText());
break;
}
}
若是组件名则应用
public String getName()获得组件的名称。
import javax.swing.*;
import java.awt.event.*;
public class HH extends JFrame implements ActionListener
{
public JRadioButton jb1 ;
public JRadioButton jb2 ;
public JRadioButton jb3 ;
public JLabel jl1;
public HH()
{
ButtonGroup bg = new ButtonGroup();
jb1 = new JRadioButton("Button1");
jb1.addActionListener(this);
jb2 = new JRadioButton("Button2");
jb2.addActionListener(this);
jb3 = new JRadioButton("Button3");
jb3.addActionListener(this);
bg.add(jb1);
bg.add(jb2);
bg.add(jb3);
setSize(300,300);
setLayout(new GridLayout(1,2));
JPanel jpbutton = new JPanel(new GridLayout(3,1));
jpbutton.add(jb1);
jpbutton.add(jb2);
jpbutton.add(jb3);
jl1 = new JLabel("Hehe");
add(jpbutton);
add(jl1);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
jl1.setText("Name Button :"+((JRadioButton)e.getSource()).getLabel());
}
public static void main(String args[])
{
HH hh = new HH();
}
}