帮忙看一下,我运行后得出的结果不太对

tangyuanamen 2007-08-20 11:23:08
import javax.swing.*;
import java.awt.event.*;

class JRadioButtonDemo implements ActionListener
{
private JFrame jframe;
private JPanel jpl;
private JRadioButton jrb1,jrb2,jrb3,jrb4;
private JLabel jlb;
private ButtonGroup bg;
public JRadioButtonDemo()
{
jframe=new JFrame("JRadioButton Demo");
jpl = new JPanel();
jlb= new JLabel("Please choose your answer");
bg = new ButtonGroup();

jrb1 = new JRadioButton("A");
jrb1.addActionListener(this);
jpl.add(jrb1);

jrb2 = new JRadioButton("B");
jrb1.addActionListener(this);
jpl.add(jrb2);

jrb3 = new JRadioButton("C");
jrb1.addActionListener(this);
jpl.add(jrb3);

jrb4 = new JRadioButton("D");
jrb1.addActionListener(this);
jpl.add(jrb4);

bg.add(jrb1);bg.add(jrb2);bg.add(jrb3);bg.add(jrb4);

jpl.add(jlb);
jframe.getContentPane().add(jpl);
jframe.pack();
jframe.setVisible(true);
jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e)
{
jlb.setText("You choose the answer:"+e.getActionCommand());
}
public static void main(String[] args)
{
new JRadioButtonDemo();
}
}
我运行后得出的结果不太对,只有点A时有反映,那里错了,谢谢
...全文
169 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangyuanamen 2007-08-22
  • 打赏
  • 举报
回复
呵呵,谢谢
zdjray 2007-08-20
  • 打赏
  • 举报
回复
请详细描述一下
jiuxiaofei 2007-08-20
  • 打赏
  • 举报
回复
jrb1 = new JRadioButton("A");
jrb1.addActionListener(this);
jpl.add(jrb1);

jrb2 = new JRadioButton("B");
jrb1.addActionListener(this);
jpl.add(jrb2);

jrb3 = new JRadioButton("C");
jrb1.addActionListener(this);
jpl.add(jrb3);

jrb4 = new JRadioButton("D");
jrb1.addActionListener(this);
jpl.add(jrb4);
==================

addActionListener() 方法统统加到“ jrb1”上了。。。 ctrl+c ctrl+v的后遗症!!
lfcai 2007-08-20
  • 打赏
  • 举报
回复
加监听器的对象不对吧,全加到jrb1上去了
应该为
jrb1 = new JRadioButton("A");
jpl.add(jrb1);
jrb1.addActionListener(this);

jrb2 = new JRadioButton("B");
jpl.add(jrb2);
jrb2.addActionListener(this);

jrb3 = new JRadioButton("C");
jpl.add(jrb3);
jrb3.addActionListener(this);

jrb4 = new JRadioButton("D");
jpl.add(jrb4);
jrb4.addActionListener(this);

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧