并要求实现用ItemListener实现事件处理
----------------解决方案--------------------------------------------------------
[CODE]import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuDemo extends JFrame{
private JMenuBar menubar;
private JMenu menu,menufile;
private JRadioButtonMenuItem radio;
private JRadioButton btn;
private JTextField t;
private int menucount = 0,btncount = 0;
public MenuDemo(String title){
super(title);
setLayout(new FlowLayout());
menubar = new JMenuBar();
setJMenuBar(menubar);
menu = new JMenu("File");
menubar.add(menu);
menufile = new JMenu("Radio");
menu.add(menufile);
t = new JTextField(20);
t.setHorizontalAlignment(JTextField.CENTER);
radio = new JRadioButtonMenuItem("RadioButtonMenuItem");
radio.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e){
menucount++;
t.setText("Click JRadioButtonMenuItem: " + menucount);
}
});
menufile.add(radio);
btn = new JRadioButton("JRadioButton");
btn.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e){
btncount++;
t.setText("Click JRadioButton: " + btncount);
}
});
menufile.add(btn);
getContentPane().add(t);
}
public static void main(String [] args){
MenuDemo md = new MenuDemo("Text");
md.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
md.setSize(300,100);
md.setVisible(true);
}
}[/CODE]
在JMenu里面用的不是JRadioButton,而是JRadioButtonMenuItem,这个也不算是难题啊,你应该可以自己做出来的
----------------解决方案--------------------------------------------------------
补充一下.如果按钮多的话就要用到ButtonGroup类了,
去添加每个button
----------------解决方案--------------------------------------------------------