Ⅰ 鼠标如何进行编程
在java.awt.*包下面有一个Robot类,可以生成输入事件,例如,Robot.mouseMove 将移动鼠标光标
这个类除了模拟鼠标键盘操作以外,还可以用来截取屏幕,只演示一下怎么模拟鼠标键盘操作,具体api参考javadoc。这个演示完成了弹出QQ和移动窗口的功能。代码如下:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
/**
* @author bean
*
*/
public class RobotDemo {
private Robot robot = null;
public RobotDemo() {
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
/** 可以弹出QQ */
public void keyBoardDemo() {
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_ALT);
}
/** 前提是有个最大化的窗口,功能是移动到标题栏,然后拖拽到600,600的位置*/
public void mouseDemo(){
robot.mouseMove(80, 10);
robot.mousePress(KeyEvent.BUTTON1_MASK);
try {
Ⅱ Java程序运行后出现一张笑脸,鼠标点击一次则变成哭脸,再点击一次又变成笑脸,依次轮换。
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class design extends Frame {
private String title = "笑脸";
design() {
this.setTitle("变脸小程序");
}
public void paint(Graphics g) {
g.drawArc(100, 200, 100, 60, 0, 180);
g.drawArc(260, 200, 100, 60, 0, 180);
if (!title.equals("笑脸")) {
g.drawArc(180, 260, 100, 60, 0, -180); // 画弧---嘴巴
title = "笑脸";
} else {
g.drawArc(180, 260, 100, 60, 0, 180); // 画弧---嘴巴
title = "哭脸";
}
g.drawString(title, 80, 100);
g.drawOval(80, 100, 300, 300);
}
public static void main(String args[]) {
design D = new design();
D.setSize(new Dimension(500, 500));
D.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
D.addMouseListener(new MouseClick(D));
D.setVisible(true);
}
}
class MouseClick implements MouseListener {// 实现鼠标监听事件
private design d;
public MouseClick() {
}
public MouseClick(design d) {
this.d = d;
}
public void mouseClicked(MouseEvent e) {// 这这里做鼠标单机处理
// TODO Auto-generated method stub
if (d != null) {
d.repaint();
}
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
已经做好了,顺便帮你调整一下格式