少打几个字 button.setMnemonic( 'c' );当然可以有两种 void setMnemonic(char mnemonic) This method is now obsolete, please use setMnemonic(int) to set the mnemonic for a button. void setMnemonic(int mnemonic) Sets the keyboard mnemonic on the current model.
这种方式的快截键是用ALT组合的,,
//OK? import javax.swing.*; import java.awt.event.*;public class JMenuItem3e extends JFrame{ JTextArea theArea = null; public JMenuItem3e(){
super("JMenuItem3e"); theArea = new JTextArea(); theArea.setEditable(false); getContentPane().add(new JScrollPane(theArea)); JMenuBar MBar = new JMenuBar(); MBar.setOpaque(true);
JMenu mfile = buildFileMenu(); MBar.add(mfile); setJMenuBar(MBar); }//end of JMenuItem3e() 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');
public static void main(String[] args){ JFrame F = new JMenuItem3e(); 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 JMenuItem3e
button.setMnemonic( 'c' );当然可以有两种 void setMnemonic(char mnemonic)
This method is now obsolete, please use setMnemonic(int) to set the mnemonic for a button.
void setMnemonic(int mnemonic)
Sets the keyboard mnemonic on the current model.
import javax.swing.*;
import java.awt.event.*;public class JMenuItem3e extends JFrame{ JTextArea theArea = null; public JMenuItem3e(){
super("JMenuItem3e");
theArea = new JTextArea();
theArea.setEditable(false);
getContentPane().add(new JScrollPane(theArea));
JMenuBar MBar = new JMenuBar();
MBar.setOpaque(true);
JMenu mfile = buildFileMenu(); MBar.add(mfile);
setJMenuBar(MBar);
}//end of JMenuItem3e() 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) ); thefile.add(newf);
thefile.add(open);
thefile.add(close);
thefile.addSeparator();
thefile.add(quit); return thefile;
}//end of buildFileMenu()
public static void main(String[] args){ JFrame F = new JMenuItem3e();
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 JMenuItem3e
public void actionPerformed(ActionEvent e){
System.out.println("^G has pressed.");
}
};
KeyStroke ks=KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK,false);//Ctrl+G
InputMap map = jButton4.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
map.put(ks,"key");//这个"key"可以是任意字符串,但是不能重复,就好像是Hashtable中的key - value一样,而下面actionmap中put进去的action就是它的value;
jButton4.getActionMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.put("key",action);
setAccelerator
方法
因为系统默认的Ctrl+C是拷贝功能啊。
一个KeyListener自己判断处理.