我想要实现鼠标点击某个按钮可以获得按钮的 keycode 请问该如何实现?
解决方案 »
- 怎么写一段程序来验证java程序的正确性?求思路。。
- 单例模式的实践应用失败了,请问为什么?
- 请用过appfuse程序的大侠进来看看!谢谢!
- 如何判断是编译错误还是运行时异常
- 哪位好心人可以帮帮我?哪里有jbuilder9的入门教程下载呀?我找了好久找不到
- [讨论]一个设计方面的问题——登陆这个动作(或叫方法)应该属于User类还是属于Application类(描述系统类)
- 一个绝对非常有趣的问题,看谁能解答出来
- 看了很久的书 怎么连一个键盘输入一个数字都搞不定,请指点一下!
- 程序为什么会有这样的错误java:18:package oracle.jdbc.driver does not exist DriverManager.registerDriver(new oracle.jdbc.driver.Or
- 我请大家帮我,紧急啊!先谢谢了!
- 跪求一个发送邮件的源码
- 弱弱的问一句,csdn论坛上让放网址吗?
import java.awt.event.KeyListener;import javax.swing.JFrame;
import javax.swing.JLabel;
public class KeyTest extends JFrame implements KeyListener { /**
* @param args
*
*/
JLabel label = new JLabel("Key Code",JLabel.CENTER);
KeyTest() {
super("get key code");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(200,100);
this.add(label);
this.addKeyListener(this);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new KeyTest();
} @Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
label.setText(e.getKeyCode()+""); //
System.out.println(e.getKeyCode()); //取得KeyCode并打印
} @Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
} @Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}}
鼠标点击某个按钮可以获得按钮的 keycode
而不是键盘获得keycode
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;public class KeyTest extends JFrame implements ActionListener{
JLabel label = new JLabel("Key Code",JLabel.CENTER);
JButton bt1 = new JButton("Key Code1");
JButton bt2 = new JButton("Key Code2");
KeyTest() {
super("get key code");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300,100);
this.setLayout(new FlowLayout());
this.add(label);
this.add(bt1);
this.add(bt2);
bt1.addActionListener(this);
bt2.addActionListener(this);
this.setVisible(true);
}
public static void main(String[] args) {
new KeyTest();
}
@Override
public void actionPerformed(ActionEvent e) {
label.setText(e.getActionCommand());
System.out.println(e.getActionCommand());//打印Button上的字符串
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class KeyTest extends JFrame implements ActionListener,KeyListener{
JLabel label = new JLabel("Key Code",JLabel.CENTER);
JButton bt[]=new JButton[80];
KeyTest() {
super("how to mouse get key code?");
bt[65] = new JButton("A");
bt[66] = new JButton("B");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300,100);
this.setLayout(new FlowLayout());this.add(label);
this.add(bt[65]);
this.add(bt[66]);
bt[65].addActionListener(this);
bt[66].addActionListener(this);
this.addKeyListener(this);
bt[65].addKeyListener(this);
bt[66].addKeyListener(this);this.setVisible(true);
}
public static void main(String[] args) {
new KeyTest();
}
@Override
public void actionPerformed(ActionEvent e) {
label.setText(e.getActionCommand());
System.out.println(e.getActionCommand());//打印Button上的字符串
} public void keyPressed(KeyEvent e){
bt[e.getKeyCode()].doClick();
System.out.println("keycode:"+e.getKeyCode());
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
}
例如:p.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.out.pringln(e.getButton());
}
});