JavaSwing按钮键盘快捷键
答案:1 悬赏:80 手机版
解决时间 2021-03-06 10:18
- 提问者网友:捧腹剧
- 2021-03-05 22:02
我想给Swing按钮添加一个快捷键,在键盘上按这个键时相当于用鼠标按下该键(注意!!!!!不用按Alt键应也能相应!如计算器的按钮功能,按"1"输入一,按"2"输入二),不知应调用哪个方法(函数)?求大虾指点!!!注意,我刚才说过,不用按Alt键应也能相应!千万别忘了!!
最佳答案
- 五星知识达人网友:野味小生
- 2021-01-25 02:29
貌似是KeyListener里面的常量。 比如0-9就是 VK_0,VK-1..........VK_9
上下左右则是 VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT
package test;
import java.awt.Button;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class MyTest extends JFrame{
public void MyTest(){
this.setSize(400,500);
this.setVisible(true);
Button button = new Button("回车");
button.setBounds(20, 20, 80, 20);
this.add(button);
button.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_UP){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_0){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_0){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_1){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_2){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_3){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_F1){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_F2){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_F3){
System.out.print(e.getKeyText(e.getKeyCode()));
}
}
});
this.setFocusable(true);
}
public static void main(String[] args) {
new MyTest().MyTest();
}
}
望采纳!!!!
上下左右则是 VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT
package test;
import java.awt.Button;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class MyTest extends JFrame{
public void MyTest(){
this.setSize(400,500);
this.setVisible(true);
Button button = new Button("回车");
button.setBounds(20, 20, 80, 20);
this.add(button);
button.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_UP){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_0){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_0){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_1){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_2){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_3){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_F1){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_F2){
System.out.print(e.getKeyText(e.getKeyCode()));
}
if(e.getKeyCode()==KeyEvent.VK_F3){
System.out.print(e.getKeyText(e.getKeyCode()));
}
}
});
this.setFocusable(true);
}
public static void main(String[] args) {
new MyTest().MyTest();
}
}
望采纳!!!!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯