当前位置: 技术问答>java相关
如何设置一个jButton按钮的快捷键?
来源: 互联网 发布时间:2015-06-14
本文导语: 比如我想用“Enter”回车键来确定“确定”按钮, “e”键来确定“取消”按钮 请问该如何处理?最好给出一些代码…… | jButton.setMnemonic(KeyEvent.VK_ENTER);试试看 | import javax.swi...
比如我想用“Enter”回车键来确定“确定”按钮,
“e”键来确定“取消”按钮
请问该如何处理?最好给出一些代码……
“e”键来确定“取消”按钮
请问该如何处理?最好给出一些代码……
|
jButton.setMnemonic(KeyEvent.VK_ENTER);试试看
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AuthenDlg extends JDialog implements ActionListener
{
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JPasswordField jPasswordField1 = new JPasswordField(10);
public AuthenDlg(JFrame frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
setSize(400,170);
setLocation(300,200);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public AuthenDlg() {
this(null, "key_test", false);
}
void jbInit() throws Exception {
panel1.setLayout(null);
usrName = null;
jLabel1.setText("PASSWORD");
jLabel1.setBounds(new Rectangle(73, 40,70,20));
jButton1.setText("OK");
jButton1.setBounds(new Rectangle(102, 91,85,25));
jButton1.addActionListener(this);
jButton1.addKeyListener(new java.awt.event.KeyListener() {
public void keyPressed(KeyEvent e)
{
key_press_event(e);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
);
jButton2.setText("CANCEL");
jButton2.setBounds(new Rectangle(199, 91,85,25));
jButton2.addActionListener(this);
jButton2.addKeyListener(new java.awt.event.KeyListener() {
public void keyPressed(KeyEvent e)
{
key_press_event(e);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
);
getContentPane().add(panel1);
jPasswordField1.setBounds(new Rectangle(171, 40,140,20));
jPasswordField1.addKeyListener(new java.awt.event.KeyListener() {
public void keyPressed(KeyEvent e)
{
key_press_event(e);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
);
panel1.add(jButton1, null);
panel1.add(jButton2, null);
panel1.add(jLabel1, null);
panel1.add(jPasswordField1,null);
this.addWindowListener(new MyWindowListener());
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
}
public static void main(String args[]){
AuthenDlg lock = new AuthenDlg();
lock.show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jButton1)
{
jButton1_actionPerformed();
}
if(e.getSource() == jButton2)
{
cancelAct();
}
}
void jButton1_actionPerformed()
{
System.out.println("this is ok");
}
void key_press_event(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_ENTER )
jButton1_actionPerformed();
if (e.getKeyCode()==KeyEvent.VK_E)
cancelAct();
}
void cancelAct()
{
System.out.println("this is cancel");
this.hide();
}
class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
cancelAct();
}
}
}
import java.awt.*;
import java.awt.event.*;
public class AuthenDlg extends JDialog implements ActionListener
{
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JPasswordField jPasswordField1 = new JPasswordField(10);
public AuthenDlg(JFrame frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
setSize(400,170);
setLocation(300,200);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public AuthenDlg() {
this(null, "key_test", false);
}
void jbInit() throws Exception {
panel1.setLayout(null);
usrName = null;
jLabel1.setText("PASSWORD");
jLabel1.setBounds(new Rectangle(73, 40,70,20));
jButton1.setText("OK");
jButton1.setBounds(new Rectangle(102, 91,85,25));
jButton1.addActionListener(this);
jButton1.addKeyListener(new java.awt.event.KeyListener() {
public void keyPressed(KeyEvent e)
{
key_press_event(e);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
);
jButton2.setText("CANCEL");
jButton2.setBounds(new Rectangle(199, 91,85,25));
jButton2.addActionListener(this);
jButton2.addKeyListener(new java.awt.event.KeyListener() {
public void keyPressed(KeyEvent e)
{
key_press_event(e);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
);
getContentPane().add(panel1);
jPasswordField1.setBounds(new Rectangle(171, 40,140,20));
jPasswordField1.addKeyListener(new java.awt.event.KeyListener() {
public void keyPressed(KeyEvent e)
{
key_press_event(e);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
);
panel1.add(jButton1, null);
panel1.add(jButton2, null);
panel1.add(jLabel1, null);
panel1.add(jPasswordField1,null);
this.addWindowListener(new MyWindowListener());
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
}
public static void main(String args[]){
AuthenDlg lock = new AuthenDlg();
lock.show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jButton1)
{
jButton1_actionPerformed();
}
if(e.getSource() == jButton2)
{
cancelAct();
}
}
void jButton1_actionPerformed()
{
System.out.println("this is ok");
}
void key_press_event(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_ENTER )
jButton1_actionPerformed();
if (e.getKeyCode()==KeyEvent.VK_E)
cancelAct();
}
void cancelAct()
{
System.out.println("this is cancel");
this.hide();
}
class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
cancelAct();
}
}
}
|
当button有焦点时, 按空格将激发action事件
|
jButton.setMnemonic(KeyEvent.VK_ENTER);是肯定可以的,不过就是“Alt+Enter”。其它的我也不知道啊,是不是有类似的KeyEvent.?可以选择成只要回车就可以?
|
你要想用Enter直接响应,最好的办法是将每个组件都加键盘的响应事件,然后,执行你要的方法
例如:
class Key_Listener implements KeyListener
{
public void keyPressed(KeyEvent e)
{
key_press_event(e);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
void key_press_event(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_ENTER )
System.out.println("enter");
if (e.getKeyCode()==KeyEvent.VK_E)
System.out.println("e");
}
}
在你的程序中,每个组件都加响应事件
Key_Listener keyL = new Key_Listener();
........
jButton1.addKeyListener(keyL) ;
jButton2.addKeyListener(keyL) ;
........
例如:
class Key_Listener implements KeyListener
{
public void keyPressed(KeyEvent e)
{
key_press_event(e);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
void key_press_event(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_ENTER )
System.out.println("enter");
if (e.getKeyCode()==KeyEvent.VK_E)
System.out.println("e");
}
}
在你的程序中,每个组件都加响应事件
Key_Listener keyL = new Key_Listener();
........
jButton1.addKeyListener(keyL) ;
jButton2.addKeyListener(keyL) ;
........
|
监视按键事件咯. . .当有回车按下的时候,发送一个action事件给这个按钮
|
to drinkant(喝酒的蚂蚁):
天哪!如果是这样的话,那岂不是我在其他想回车的地方都不能回撤了?呵呵!
你可以试试registerKeyboardAction,下面是它的api文档。
public void registerKeyboardAction(ActionListener anAction,
KeyStroke aKeyStroke,
int aCondition)This method is now obsolete, please use a combination of getActionMap() and getInputMap() for similiar behavior.
天哪!如果是这样的话,那岂不是我在其他想回车的地方都不能回撤了?呵呵!
你可以试试registerKeyboardAction,下面是它的api文档。
public void registerKeyboardAction(ActionListener anAction,
KeyStroke aKeyStroke,
int aCondition)This method is now obsolete, please use a combination of getActionMap() and getInputMap() for similiar behavior.
|
1。jButton.setMnemonic(KeyEvent.VK_ENTER);这是一种较简单的,要按alt+enter。
|
首先引入包
import javax.swing.event.*;
然后定义一个
KeyStroke stroke1 = KeyStroke.getKeyStroke (KeyEvent.VK_ENTER,ActionEvent.CTRL_MASK,true);//创建一个KeyStroke类
最后一个参数的意思是是否在Keyrelease时触发此事件
JButton jButton1 = new JButton();
jButton1.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等
import javax.swing.event.*;
然后定义一个
KeyStroke stroke1 = KeyStroke.getKeyStroke (KeyEvent.VK_ENTER,ActionEvent.CTRL_MASK,true);//创建一个KeyStroke类
最后一个参数的意思是是否在Keyrelease时触发此事件
JButton jButton1 = new JButton();
jButton1.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等