博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Exec 66 快捷键设置

Posted on 2012-08-08 09:25  紫冰龙  阅读(419)  评论(0编辑  收藏  举报
import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
public class Exec66 extends JFrame implements ActionListener{
    JTextField text;
    static int i=0;
    public Exec66() {
        this.setTitle("Test");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(100, 100, 200, 300);
        this.setLocationRelativeTo(null);
        
        JMenuBar bar = new JMenuBar();
        
        JMenu file = new JMenu("文件");
        JMenu edit = new JMenu("编辑");
        JMenu help = new JMenu("帮助");
        
        JMenuItem f1 = new JMenuItem("OPEN");
        JMenuItem f2 = new JMenuItem("保存");
        JMenuItem f3 = new JMenuItem("退出");
        JMenuItem e1 = new JMenuItem("复制");
        JMenuItem e2 = new JMenuItem("粘贴");
        JMenuItem e3 = new JMenuItem("全选");
        JMenuItem h1 = new JMenuItem("帮助");
        JMenuItem h2 = new JMenuItem("关于");
        
        f1.setMnemonic('O');
        f1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
    
        f1.addActionListener(this);
        f2.addActionListener(this);
        f3.addActionListener(this);
        e1.addActionListener(this);
        e2.addActionListener(this);
        e3.addActionListener(this);
        h1.addActionListener(this);
        h2.addActionListener(this);
        
        
        this.setJMenuBar(bar);
        bar.add(file);
        bar.add(edit);
        bar.add(help);
        
        file.add(f1);file.add(f2);file.add(f3);
        edit.add(e1);edit.add(e2);edit.add(e3);
        help.add(h1);help.add(h2);
        
        text = new JTextField(10);
        
        add(text);
        
        this.setVisible(true);
        
        JButton bt = new JButton("BUTTON");
        bt.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e ) {
                text.setText("BUTTON "+(i++));
            }
        });
        bt.setMnemonic(KeyEvent.VK_O);
        add(bt,BorderLayout.SOUTH);
    }

    
    public static void main(String[] args) {
         new Exec66();

    }
    public void actionPerformed(ActionEvent e) {
        JMenuItem item = (JMenuItem)(e.getSource());
        if (item.getActionCommand().equals("退出")) System.exit(1);
        else text.setText(item.getActionCommand());
    }

}