[JAVA100例]019、菜单[Java编程]
本文“[JAVA100例]019、菜单[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
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(this);
menu.add(cbMenuItem);
cbMenuItem = new JCheckBoxMenuItem("(H)别的一个查抄盒");
cbMenuItem.setMnemonic(KeyEvent.VK_H);
cbMenuItem.addItemListener(this);
menu.add(cbMenuItem);
//定义一个带子菜单
menu.addSeparator();
submenu = new JMenu("(S)带有子菜单");
submenu.setMnemonic(KeyEvent.VK_S);
//定义子菜单
menuItem = new JMenuItem("这是子菜单");
//定义快速键
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_2, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
submenu.add(menuItem);
menuItem = new JMenuItem("子菜单项");
menuItem.addActionListener(this);
submenu.add(menuItem);
menu.add(submenu);
//定义第二个菜单
menu = new JMenu("(N)第二个菜单");
menu.setMnemonic(KeyEvent.VK_N);
menuBar.add(menu);
return menuBar;
}
/**
*<br>办法阐明:构建面板
*<br>输入参数:
*<br>返回范例:
*/
public Container createContentPane() {
//构造一个面板
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
//定义一个文本域
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
//将文本域增添到面板中
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}
/**
*<br>办法阐明:构建弹出菜单
*<br>输入参数:
*<br>返回范例:
*/
public void createPopupMenu() {
JMenuItem menuItem;
//构件弹出菜单
JPopupMenu popup = new JPopupMenu();
ImageIcon openicon = createImageIcon("images/Open16.gif");
menuItem = new JMenuItem("翻开文件",openicon);
menuItem.addActionListener(this);
popup.add(menuItem);
ImageIcon saveicon = createImageIcon("images/Save16.gif");
menuItem = new JMenuItem("保存文件",saveicon);
menuItem.addActionListener(this);
popup.add(menuItem);
//增添一个监听给文本域,以便点击右键时呼应
MouseListener popupListener = new PopupListener(popup);
output.addMouseListener(popupListener);
}
/**
*<br>办法阐明:监听普通的菜单挑选
*<br>输入参数:ActionEvent e 事件
*<br>返回范例:
*/
public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
String s = "监测事件."
+ newline
+ " 事件源: " + source.getText()
+ " (挑选对象" + getClassName(source) + ")";
output.append(s + newline);
}
/**
*<br>办法阐明:监听查抄盒菜单挑选项
*<br>输入参数:ItemEvent e 查抄盒触发的事件
*<br>返回范例:
*/
public void itemStateChanged(ItemEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
String s = "菜单项监听"
+ newline
+ " 事件源: " + source.getText()
+ " (挑选对象 " + getClassName(source) + ")"
+ newline
+ " 新的状况: "
+ ((e.getStateChange() == ItemEvent.SELECTED) ?
"挑选":"不挑选");
output.append(s + newline);
}
/**
*<br>办法阐明:得到类的名字
*<br>输入参数:
*<br>返回范例:
*/
protected String getClassName(Object o) {
String classString = o.getClass().getName();
int dotIndex = classString.lastIndexOf(".");
return classString.substring(dotIndex+1);
}
/**
*<br>办法阐明:按照途径查找图片
*<br>输入参数:String path 图片的途径
*<br>返回范例:ImageIcon 图片对象
*/
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = MenuDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn´t find file: " + path);
return null;
}
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
//成立一个窗体
JFrame frame = new JFrame("MenuDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//成立菜单,并增添到面板中
MenuDemo demo = new MenuDemo();
frame.setJMenuBar(demo.createMenuBar());
frame.setContentPane(demo.createContentPane());
//生成弹出菜单
demo.createPopupMenu();
//显示窗体
frame.setSize(450, 260);
frame.setVisible(true);
}
//弹出菜单监听类
class PopupListener extends MouseAdapter {
JPopupMenu popup;
PopupListener(JPopupMenu popupMenu) {
popup = popupMenu;
}
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
}
}
以上是“[JAVA100例]019、菜单[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |