//耐返笑
VK_S换成你想代替的快捷键。是通过alt+你设定的键组合
当你按下组合键后世野会有和你点击同样的效果。昌含要想JButton凹下去,可以用button1.setSelected(true)
注册KeyListener的组件..要求焦点在它头上的时候 这个监听才起作用而组件获得焦点的函数是requestFocus()基本上 所有的Swing组件里都有
因为他是基类中森谨并就定义了的....
最好的方法是 无论你点了哪个键 都此迹以当前窗口的最外层容器 来注册KeyListener 并且无论你按哪晌竖了个键,事件处理方法中一定要重复一次 这个容器获得焦点..才能保证下一次能起作用..
///用者灶大.java.awt.Robot类能做你想要的首竖事情!///我的小例子完辩岩全符合楼主想法~~
///: RobotTest.java
import java.awt.*
import java.awt.event.*
import javax.swing.*
public class RobotTest extends JFrame{
private Robot r
private JTextField txt
private JPasswordField pwd
private JButton bt,bt3
RobotTest(){
super("ROBOT TEST")
try{r = new Robot()}catch(Exception e){e.printStackTrace()}
this.getContentPane().setLayout(null)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
this.setSize(200,200)
this.setLocationRelativeTo(null)
txt = new JTextField()
txt.setBounds(18,14,100,20)
add(txt)
pwd = new JPasswordField()
pwd.setBounds(18,44,100,20)
pwd.setEchoChar('*')
add(pwd)
bt = new JButton("2")
bt.setBounds(58,80,68,22)
add(bt)
bt3=new JButton("3")
bt3.setBounds(58,110,68,22)
add(bt3)
bt.setFocusable(false)
bt3.setFocusable(false)
bt.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {robotDo(KeyEvent.VK_2)}})
bt3.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {robotDo(KeyEvent.VK_3)}})
}
void robotDo(int key){
if(r==null)return
r.keyPress(key)
r.delay(10)
r.keyRelease(key)
}
public static void main(String[] args) {new RobotTest().setVisible(true)}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)