[JAVA100例]019、菜单
时间:2011-01-09
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPopupMenu;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
/**
* <p>Title: 菜单演示</p>
* <p>Description: 演示菜单的建立和快捷键的使用。</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: MenuDemo.java</p>
* @version 1.0
*/
public class MenuDemo implements ActionListener, ItemListener {
JTextArea output;
JScrollPane scrollPane;
String newline = "\n";
/**
*<br>方法说明:组建菜单栏
*<br>输入参数:
*<br>返回类型:
*/
public JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;
JCheckBoxMenuItem cbMenuItem;
//定义菜单条
menuBar = new JMenuBar();
//定义第一个菜单
menu = new JMenu("(A)菜单");
menu.setMnemonic(KeyEvent.VK_A);
menuBar.add(menu);
//下面开始定义菜单项
//只有文字
menuItem = new JMenuItem("(O)只有文本的菜单",
KeyEvent.VK_O);
//设置快捷键
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
//添加监听
menuItem.addActionListener(this);
menu.add(menuItem);
//有图标还有文字
ImageIcon icon = createImageIcon("images/middle.gif");
menuItem = new JMenuItem("(B)有图标和文字的菜单", icon);
menuItem.setMnemonic(KeyEvent.VK_B);
menuItem.addActionListener(this);
menu.add(menuItem);
//只有图标
menuItem = new JMenuItem(icon);
menuItem.setMnemonic(KeyEvent.VK_D);
menuItem.addActionListener(this);
menu.add(menuItem);
//定义一组radio button(单选按钮)菜单
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("(R)使用radio的菜单");
rbMenuItem.setSelected(true);
rbMenuItem.setMnemonic(KeyEvent.VK_R);
group.add(rbMenuItem);
rbMenuItem.addActionListener(this);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("(d)另外一个radio菜单");
rbMenuItem.setMnemonic(KeyEvent.VK_D);
group.add(rbMenuItem);
rbMenuItem.addActionListener(this);
menu.add(rbMenuItem);
//定义一组check box(检查盒)菜单
menu.addSeparator();
cbMenuItem = new JCheckBoxMenuItem("(C)使用检查盒的菜单");
cbMenuItem.setMnemonic(KeyEvent.VK_C);
cbMenuItem.addItemListener
|