当前位置: 技术问答>java相关
如何让Button响应快捷键回车"Enter"和退出"Esc"!!
来源: 互联网 发布时间:2015-06-25
本文导语: 如何让Button响应快捷键回车"Enter"和退出"Esc"!!? 注意是Button,不是JButton,怎么设置它的快捷键为回车“ENTER”或者为退出“ESC”呢? 能实现的大虾给出一个完整的小应用程序application的代码,只要有个按钮能表示这...
如何让Button响应快捷键回车"Enter"和退出"Esc"!!?
注意是Button,不是JButton,怎么设置它的快捷键为回车“ENTER”或者为退出“ESC”呢?
能实现的大虾给出一个完整的小应用程序application的代码,只要有个按钮能表示这个实现就行。
100分奉上!!
注意是Button,不是JButton,怎么设置它的快捷键为回车“ENTER”或者为退出“ESC”呢?
能实现的大虾给出一个完整的小应用程序application的代码,只要有个按钮能表示这个实现就行。
100分奉上!!
|
import java.awt.*;
import java.awt.event.*;
public class Test extends Dialog {
Button btnOK = new Button("OK");
Button btnCancel = new Button("Cancel");
Button btnClean = new Button("Clean");
Label lblResult = new Label();
TextField txtTest = new TextField();
public Test() {
super(new Frame());
this.setTitle("Test");
this.setSize(400, 180);
this.setLayout(null);
this.add(txtTest);
txtTest.setBounds(100,70,200,20);
this.add(lblResult);
lblResult.setBounds(100,40,200,20);
this.add(btnOK);
btnOK.setBounds(300,130,60,25);
this.add(btnClean);
btnClean.setBounds(200,130,60,25);
this.add(btnCancel);
btnCancel.setBounds(40,130,60,25);
SymAction symAction = new SymAction();
btnOK.addActionListener(symAction);
btnCancel.addActionListener(symAction);
btnClean.addActionListener(symAction);
setKeyListener();
}
public static void main(String[] argv) {
Test temp = new Test();
temp.setVisible(true);
}
class SymAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if (obj == btnOK) {
btnOK_actionPerformed();
} else if (obj == btnCancel) {
btnCancel_actionPerformed();
} else if (obj == btnClean) {
btnClean_actionPerformed();
}
}
}
private void btnOK_actionPerformed() {
lblResult.setText("You have press OK!");
}
private void btnCancel_actionPerformed() {
System.exit(0);
}
private void btnClean_actionPerformed() {
lblResult.setText("");
txtTest.setText("");
}
class SymKey extends KeyAdapter {
public void keyPressed(KeyEvent ke) {
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
btnOK_actionPerformed();
} else if (ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
btnCancel_actionPerformed();
} else {
super.keyReleased(ke);
}
}
}
private void setKeyListener() {
SymKey symKey = new SymKey();
Component[] components = this.getComponents();
for (int i = 0; i
import java.awt.event.*;
public class Test extends Dialog {
Button btnOK = new Button("OK");
Button btnCancel = new Button("Cancel");
Button btnClean = new Button("Clean");
Label lblResult = new Label();
TextField txtTest = new TextField();
public Test() {
super(new Frame());
this.setTitle("Test");
this.setSize(400, 180);
this.setLayout(null);
this.add(txtTest);
txtTest.setBounds(100,70,200,20);
this.add(lblResult);
lblResult.setBounds(100,40,200,20);
this.add(btnOK);
btnOK.setBounds(300,130,60,25);
this.add(btnClean);
btnClean.setBounds(200,130,60,25);
this.add(btnCancel);
btnCancel.setBounds(40,130,60,25);
SymAction symAction = new SymAction();
btnOK.addActionListener(symAction);
btnCancel.addActionListener(symAction);
btnClean.addActionListener(symAction);
setKeyListener();
}
public static void main(String[] argv) {
Test temp = new Test();
temp.setVisible(true);
}
class SymAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if (obj == btnOK) {
btnOK_actionPerformed();
} else if (obj == btnCancel) {
btnCancel_actionPerformed();
} else if (obj == btnClean) {
btnClean_actionPerformed();
}
}
}
private void btnOK_actionPerformed() {
lblResult.setText("You have press OK!");
}
private void btnCancel_actionPerformed() {
System.exit(0);
}
private void btnClean_actionPerformed() {
lblResult.setText("");
txtTest.setText("");
}
class SymKey extends KeyAdapter {
public void keyPressed(KeyEvent ke) {
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
btnOK_actionPerformed();
} else if (ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
btnCancel_actionPerformed();
} else {
super.keyReleased(ke);
}
}
}
private void setKeyListener() {
SymKey symKey = new SymKey();
Component[] components = this.getComponents();
for (int i = 0; i