在jframe中有一个jbutton按钮,想按ctrl+enter组合键响应事件,怎么处理
我用了setMnemonic但是这个只能实现alt+enter!高手帮忙,最好用例子!
我用了setMnemonic但是这个只能实现alt+enter!高手帮忙,最好用例子!
解决方案 »
- Java如何调用可执行文件和批处理命令
- 关于单例模式懒汉式空指针的问题!
- "TableHeader.cellBorder"-是什么
- java天气(转)
- 新人请教一下啊!为什么我的这段代码无法将图片显示出来呢?
- 菜鸟问题:读取本地文件的毛病
- 哪位给我一份java in a Nutshell的电子版
- StringTokenizer 怎么用?
- 在GUI中,如何遍歷一個窗體中的所有物件
- 请教!jdbc web(win2000/tomcat) database(solaris oracle8i)
- JFrame 标题栏的简单三问寻求三杰
- gui 编程如何把数据库内容显示到frame里面 可以有删除什么的界面 在线等
public void keyPressed(KeyEvent e) {
if(e.getModifiers()==InputEvent.CTRL_MASK&&e.getKeyCode()==KeyEvent.VK_ENTER){
doSomething();
}
}
KeyListener keyL=new MyKeyListener();
添加方法public void setKeyListener(Component com) {
com.addKeyListener(keyL);
if (com instanceof Container) {
Container con = (Container) com;
for (int i = 0; i < con.getComponentCount(); i++) {
Component c = con.getComponent(i);
setKeyListener(c);
}
}
}
调用setKeyListener(button.getTopLevelAncestor());记住在button添加到frame后在调用这个
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
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
ActionListener action=new ActionListener(){
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);