使用JRadioButtonMenuItem组件

使用JRadioButtonMenuItem组件: 
JRadioButtonMenuItem的类层次结构图: 
 java.lang.Object
   --java.awt.Component
    --java.awt.Container
      --javax.swing.JComponent
       --javax.swing.AbstractButton
        --javax.swing.JMenuItem
         --javax.swing.JRadioButtonMenuItem与JCheckBoxMenuItem相同,JRadioButtonItem也是继承JMenuItem,因此JRadioButtonMenuItem也具备JMenuItem的许多特性.而 
JRadioButtonMenuItem和JCheckBoxMenuItem一样也是一种特殊的JMenuItem.我们在前面也介绍过JRadioButton组件,而 
JRadioButtonMenuItem是专用在MenuItem上.我们来看看JRadioButtonMenuItem的构造方法,如下表: 
JRadioButtonMenuItem的构造函数:
  JRadioButtonMenuItem():建立一个新的JRadioButtonMenuItem. 
  JRadioButtonMenuItem(Action a):建立一个支持Action的新的JRadioButtonMenuItem. 
  JRadioButtonMenuItem(Icon icon):建立一个有图标的JRadioButtonMenuItem. 
  JRadioButtonMenuItem(Icon icon,Boolean 
  selected):建立一个有图标和设置选择状态的JRadioButtonMenuItem. 
  JRadioButtonMenuItem(String text):建立一个有文字的JRadioButtonMenuItem. 
  JRadioButtonMenuItem(String text,Boolean 
  selected):建立一个有文字和设置选择状态的JRadioButtonMenuItem. 
  JRadioButtonMenuItem(String text,Icon icon):建立一个有文字和图标的JRadioButtonMenuItem. 
  JRadioButtonMenuItem(String text,Icon icon,Boolean 
  selected):建立一个有文字,图标和设置状态的JRadioButtonMenuItem. 
12-5-1:构造JRadioButtonMenuItem组件: 
由一表可以看出JRadioButtonMenuItem的构造方式与JCheckBoxMenuItem构造方式几乎相同,而且和JCheckBoxMenuItem的构造方 
式一样都是比JMenuItem的构造方式多了设置选择状况的构造方式,设置选择状态就是决定是否要将构造好的JRadioButtonMenuItem 
设置为默认值.我们来看下面这个范例: 
import javax.swing.*;
import java.awt.event.*;

public class JRadioButtonMenuItem1 extends JFrame implements ActionListener {

 JTextArea theArea = null;

 public JRadioButtonMenuItem1() {

  super("JRadioButtonMenuItem1");
  theArea = new JTextArea();
  theArea.setEditable(false);
  getContentPane().add(new JScrollPane(theArea));
  JMenuBar MBar = new JMenuBar();
  MBar.setOpaque(true);

  JMenu mfile = buildFileMenu();
  JMenu mstyle = buildStyleMenu();

  MBar.add(mfile);
  MBar.add(mstyle);
  setJMenuBar(MBar);
 }// end of JRadioButtonMenuItem1()

 public JMenu buildFileMenu() {

  JMenu thefile = new JMenu("File");
  thefile.setMnemonic('F');

  JMenuItem newf = new JMenuItem("New", new ImageIcon("icons/new24.gif"));
  JMenuItem open = new JMenuItem("Open",
    new ImageIcon("icons/open24.gif"));
  JMenuItem close = new JMenuItem("Close", new ImageIcon(
    "icons/close24.gif"));
  JMenuItem quit = new JMenuItem("Exit",
    new ImageIcon("icons/exit24.gif"));

  newf.setMnemonic('N');
  open.setMnemonic('O');
  close.setMnemonic('L');
  quit.setMnemonic('X');

  newf.setAccelerator(KeyStroke.getKeyStroke('N',
    java.awt.Event.CTRL_MASK, false));
  open.setAccelerator(KeyStroke.getKeyStroke('O',
    java.awt.Event.CTRL_MASK, false));
  close.setAccelerator(KeyStroke.getKeyStroke('L',
    java.awt.Event.CTRL_MASK, false));
  quit.setAccelerator(KeyStroke.getKeyStroke('X',
    java.awt.Event.CTRL_MASK, false));

  newf.addActionListener(this);
  open.addActionListener(this);
  close.addActionListener(this);
  quit.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    System.exit(0);
   }
  });

  thefile.add(newf);
  thefile.add(open);
  thefile.add(close);
  thefile.addSeparator();
  thefile.add(quit);

  return thefile;
 }// end of buildFileMenu()

 public JMenu buildStyleMenu() {

  JMenu style = new JMenu("Style");
  style.setMnemonic('S');

  JRadioButtonMenuItem Left = new JRadioButtonMenuItem("Left",
    new ImageIcon("icons/left24.gif"));
  JRadioButtonMenuItem Center = new JRadioButtonMenuItem("Center",
    new ImageIcon("icons/center24.gif"));
  JRadioButtonMenuItem Right = new JRadioButtonMenuItem("Right",
    new ImageIcon("icons/right24.gif"));
  JRadioButtonMenuItem Justify = new JRadioButtonMenuItem("Justify",
    new ImageIcon("icons/justify24.gif"));

  Left.setMnemonic('L');
  Center.setMnemonic('E');
  Right.setMnemonic('R');
  Justify.setMnemonic('J');

  Left.setAccelerator(KeyStroke.getKeyStroke('L',
    java.awt.Event.SHIFT_MASK, false));
  Center.setAccelerator(KeyStroke.getKeyStroke('E',
    java.awt.Event.SHIFT_MASK, false));
  Right.setAccelerator(KeyStroke.getKeyStroke('R',
    java.awt.Event.SHIFT_MASK, false));
  Justify.setAccelerator(KeyStroke.getKeyStroke('J',
    java.awt.Event.SHIFT_MASK, false));

  Left.addActionListener(this);
  Center.addActionListener(this);
  Right.addActionListener(this);
  Justify.addActionListener(this);

  style.add(Left);
  style.add(Center);
  style.add(Right);
  style.add(Justify);

  return style;
 }// end of buildStyleMenu()

 public void actionPerformed(ActionEvent ae) {
  try {
   theArea.append("* action '" + ae.getActionCommand()
     + "' performed. */n");
  } catch (Exception e) {
   System.out.println("actionPerformed Exception:" + e);
  }
 }

 public static void main(String[] args) {

  JFrame F = new JRadioButtonMenuItem1();
  F.setSize(400, 200);
  F.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });// end of addWindowListener
  F.setVisible(true);
 } // end of main
}// end of class JRadioButtonMenuItem1在这个范例中我们遇到了与JRadioButton同样的问题,那就是选项的单、复选的问题.一般来说RadioButton组件是用来做单选 
的情况时使用,而CheckBox则是多为复选情况时使用.那么我们该怎么来解决这个问题呢?还记得我们介绍JRadioButton时的解决方式 
吗?没错,就是要利用ButtonGroup类来处理.我们在JRadioButtonMenuItem1.java的buildStyleMenu()方法中加下下列五行:

      ButtonGroup bg=new ButtonGroup();
         bg.add(Left);
         bg.add(Center);
         bg.add(Right);
         bg.add(Justify);       运行程序我们可看到加入ButtonGroup后的JRadioButtonMenuItem变成只能被单选的菜单.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值