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());
}
}
}
}