请问在java 中能否让鼠标指针按某种轨迹自己移动?是否有这样的类或方法? actually only Robot can do this in java if no JNI code 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 OK,要点专家分过年package xiruo.paint;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class testPaint extends JFrame implements Runnable { private JPanel contentPanel; private Robot robot; private Thread thread; private JButton button1; private int x,y; private int movex,movey; public testPaint() throws Exception { super("testPaint"); contentPanel=(JPanel)this.getContentPane(); button1=new JButton("start"); robot=new Robot(); thread=new Thread(this); x=y=0; movex=2; movey=3; } private void init() { this.setSize(600,450); this.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-this.getWidth()/2, Toolkit.getDefaultToolkit().getScreenSize().height/2-this.getHeight()/2); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); contentPanel.setLayout(new FlowLayout()); contentPanel.add(button1); clickButton cb=new clickButton(); contentPanel.addMouseMotionListener(new movePanel()); button1.addMouseListener(cb); this.addWindowListener(new closeWin()); this.setVisible(true); this.setExtendedState(JFrame.MAXIMIZED_BOTH); this.setResizable(false); } public void run() { try { while(true){ if (x <= 10) movex = 2; if (x >= 450) movex = -2; if(y<=10) movey=3; if(y>=600) movey=-3; x+=movex; y+=movey; robot.mouseMove(x,y); Thread.sleep(50); } }catch(Exception e){ if(thread!=null){ thread.destroy(); thread=null; } } } public static void main(String[] args) { try{ testPaint tp=new testPaint(); tp.init(); }catch(Exception e){ e.printStackTrace(); System.exit(-1); } }class closeWin extends WindowAdapter { public void windowClosing(WindowEvent e) { e.getWindow().setVisible(false); e.getWindow().dispose(); System.exit(0); }}class clickButton extends MouseAdapter { public void mouseClicked(MouseEvent e) { super.mouseClicked(e); JButton button=(JButton)e.getSource(); try{ if(thread==null){ thread=new Thread(testPaint.this); } thread.start(); }catch(Exception ex){ ex.printStackTrace(); } }}class movePanel extends MouseMotionAdapter{ public void mouseMove(MouseEvent e) { x=e.getX(); y=e.getY(); }}} 3ks beyond_xiruo(CorruptionException)! 请教两个String对比的内存问题 登录的确定和取消按钮为什么不出现。 怎么加载图片到程序中去呢? Jbutton控件组显示层次的问题 可以为基于swt的java桌面应用程序设置系统的快捷键吗? 初学JAVA新手求教问题。 请教一个简单的gui类设计的问题(有关多线程) 一个关于3DES密钥的问题 没参数程序怎么还能运行???????????????????????? 有没有人用java做过类似邮件系统的软件(包括客户端和服务器端) 关于Runtime.exec()的问题,请大家帮帮忙 书上的一个例子,但是有一点问题,请高手帮忙~
import java.awt.*;
import java.awt.event.*;public class testPaint extends JFrame implements Runnable {
private JPanel contentPanel;
private Robot robot;
private Thread thread;
private JButton button1;
private int x,y;
private int movex,movey;
public testPaint() throws Exception {
super("testPaint");
contentPanel=(JPanel)this.getContentPane();
button1=new JButton("start");
robot=new Robot();
thread=new Thread(this);
x=y=0;
movex=2;
movey=3;
} private void init() {
this.setSize(600,450);
this.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-this.getWidth()/2,
Toolkit.getDefaultToolkit().getScreenSize().height/2-this.getHeight()/2);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPanel.setLayout(new FlowLayout());
contentPanel.add(button1);
clickButton cb=new clickButton();
contentPanel.addMouseMotionListener(new movePanel());
button1.addMouseListener(cb);
this.addWindowListener(new closeWin());
this.setVisible(true);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setResizable(false);
} public void run() {
try {
while(true){
if (x <= 10)
movex = 2;
if (x >= 450)
movex = -2;
if(y<=10)
movey=3;
if(y>=600)
movey=-3;
x+=movex;
y+=movey;
robot.mouseMove(x,y);
Thread.sleep(50);
}
}catch(Exception e){
if(thread!=null){
thread.destroy();
thread=null;
}
}
} public static void main(String[] args) {
try{
testPaint tp=new testPaint();
tp.init();
}catch(Exception e){
e.printStackTrace();
System.exit(-1);
}
}
class closeWin extends WindowAdapter {
public void windowClosing(WindowEvent e) {
e.getWindow().setVisible(false);
e.getWindow().dispose();
System.exit(0);
}
}class clickButton extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
JButton button=(JButton)e.getSource();
try{
if(thread==null){
thread=new Thread(testPaint.this);
}
thread.start();
}catch(Exception ex){
ex.printStackTrace();
}
}
}class movePanel extends MouseMotionAdapter{
public void mouseMove(MouseEvent e) {
x=e.getX();
y=e.getY();
}
}
}