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();
}}}
to chinaredflag(chinaredflag):你是我试过一下,但是不行,难道是我截取错了?能不能只给出我需要的?精简一点btw:我再加点分
天哪!如果是这样的话,那岂不是我在其他想回车的地方都不能回撤了?呵呵!你可以试试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.
你这只是增加/去除jbutton的键盘事件而已
事实上,默认的就是有回车事件的,只不过这位老兄要做的是在发生VK_ENTER和VK_E的时候处理他自己的事情
“监视该监视的那些东东”那你说该监听什么?
“增加/去除jbutton的键盘事件”对呀!所以在你的anAction就要抓住这个事件,并作相应的处理喽!
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等
例如:
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) ;
........
算了
我先结贴
再开一个窗口问!!