JBuilder 设置快捷键问题? KeyEvent理由所有的键。到里面找就是了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我写的一个例子,下面有解释import javax.swing.*;import javax.swing.event.*;import java.awt.event.*;public class KeyTest extends JFrame { public KeyTest() { this.setSize(300,200); JButton btn = new JButton(); btn.setText("Test"); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("CLICK"); } }); KeyStroke stroke1 = KeyStroke.getKeyStroke(KeyEvent.VK_F,ActionEvent.CTRL_MASK,true); btn.registerKeyboardAction(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("OK"); } },stroke1,JComponent.WHEN_FOCUSED); this.getContentPane().add(btn); } public static void main(String[] args) { KeyTest keyTest1 = new KeyTest(); keyTest1.show(); }}首先引入包import javax.swing.event.*;然后定义一个 KeyStroke stroke1 = KeyStroke.getKeyStroke (KeyEvent.VK_F,ActionEvent.CTRL_MASK,true);//创建一个KeyStroke类 最后一个参数的意思是是否在Keyrelease时触发此事件 btn.registerKeyboardAction(new ActionListener(){ //要执行的方法 public void actionPerformed(ActionEvent e){ System.out.println("OK"); } },stroke1,JComponent.WHEN_IN_FOCUSED_WINDOW); } registerKeyboardAction方法的参数意思是这样的 1.ActionListener对象,可以定义你要执行的方法 2.KeyStroke 对象,定义触发事件的条件 3.何时按Ctrl+Enter时发生此事件,比如 JComponent.WHEN_IN_FOCUSED_WINDOW JComponent.WHEN_FOCUSED等 关于使用marshaller生成xml文件的一个问题 swt相关的一个小问题 javaSE里面StringReader把我搞疯了 java连接ACCESS奇怪问题 请问Hibernate3中如何使用cglib 郁闷 位运算小问题! Applet 求救? 从c++转到java的疑惑。 关于下载的问题 安装java1.4(win98下)后运行程序时出现错误 怎样消除JTree在展开和合闭时的闪烁?(付代码)
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class KeyTest extends JFrame {
public KeyTest() {
this.setSize(300,200);
JButton btn = new JButton();
btn.setText("Test");
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("CLICK");
}
});
KeyStroke stroke1 = KeyStroke.getKeyStroke(KeyEvent.VK_F,ActionEvent.CTRL_MASK,true);
btn.registerKeyboardAction(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("OK");
}
},stroke1,JComponent.WHEN_FOCUSED);
this.getContentPane().add(btn); }
public static void main(String[] args) {
KeyTest keyTest1 = new KeyTest();
keyTest1.show();
}
}
首先引入包
import javax.swing.event.*;
然后定义一个
KeyStroke stroke1 = KeyStroke.getKeyStroke (KeyEvent.VK_F,ActionEvent.CTRL_MASK,true);//创建一个KeyStroke类
最后一个参数的意思是是否在Keyrelease时触发此事件
btn.registerKeyboardAction(new ActionListener(){
//要执行的方法
public void actionPerformed(ActionEvent e){
System.out.println("OK");
}
},stroke1,JComponent.WHEN_IN_FOCUSED_WINDOW);
}
registerKeyboardAction方法的参数意思是这样的
1.ActionListener对象,可以定义你要执行的方法
2.KeyStroke 对象,定义触发事件的条件
3.何时按Ctrl+Enter时发生此事件,比如
JComponent.WHEN_IN_FOCUSED_WINDOW
JComponent.WHEN_FOCUSED等