public class MaxFrame extends JFrame {
JButton button = new JButton("hit me");
public MaxFrame(){
getContentPane().add(button);
setVisible(true);
setSize(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// create a robot to feed in GUI events
Robot rob = new Robot();
// move cursor to the top, left corner
Point p = getLocationOnScreen();
rob.mouseMove(p.x + 5, p.y + 5);
// press and release left mouse button
rob.mousePress(InputEvent.BUTTON1_MASK);
rob.mouseRelease(InputEvent.BUTTON1_MASK);
// send an 'x' key press
rob.keyPress(KeyEvent.VK_X);
}
catch (Exception exc) {
System.err.println(exc);
}
}
});
}
public static void main(String args[]){
MaxFrame f = new MaxFrame();
f.show();
}
}