就像外挂那样的 模拟打开软件 填写一些东西 模拟点击的 有知道怎么实现的吗?有人说能用junit做,不知道有人知道吗,有做过的吗?能否发个案例什么的。谢谢!
junit是测试框架
应该说的是AWT框架,你百度下就知道了
[code="java"]import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestFrame extends JFrame
{
private JButton btn1 = new JButton("Button 1");
private JButton btn2 = new JButton("Button 2");
private Robot robot;
public TestFrame()
{
setLayout(new GridLayout(0, 1));
add(btn1);
add(btn2);
btn1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent event)
{
if (event.getButton() == MouseEvent.BUTTON1)
System.out.println(btn1.getText() + "鼠标左键点击");
if (event.getButton() == MouseEvent.BUTTON3)
System.out.println(btn1.getText() + "鼠标右键点击");
}
});
btn2.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent event)
{
if (event.getButton() == MouseEvent.BUTTON1)
System.out.println(btn2.getText() + "鼠标左键点击");
if (event.getButton() == MouseEvent.BUTTON3)
System.out.println(btn2.getText() + "鼠标右键点击");
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent event)
{
}
});
}
public void simulationClick() throws AWTException
{
//模拟鼠标点击代码
Point p1 = btn1.getLocation();
Point p2 = btn2.getLocation();
System.out.println(p1.x + "," + p1.y);
System.out.println(p2.x + "," + p2.y);
Point p = MouseInfo.getPointerInfo().getLocation();
int x = p.x;
int y = p.y;
robot = new Robot();
System.out.println(x + "," + y);
robot.mouseMove(p1.x + 10, p2.y + 10);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.mouseMove(x, y);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
TestFrame frame = new TestFrame();
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try
{
frame.simulationClick();
}
catch (AWTException e)
{
e.printStackTrace();
}
}
});
}
}
[/code]
junit只是一个java单元测试框架,要做的话就如ls的,要用swing之类来做
public class FileChoose
{
public static void main(String[] args)
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int result = chooser.showSaveDialog(null);
chooser.setDialogTitle("0.0");
if(result==JFileChooser.APPROVE_OPTION);
System.out.println(chooser.getSelectedFile().getPath());
}
}
我看明白了,你是来控制鼠标和键盘,模拟实现人工点击对吧,使用swt
下面是个例子
[code="java"]
import java.awt.MouseInfo;
public class App extends JFrame implements KeyListener {
private JLabel lblNewLabel;
private String str = "";
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
lblNewLabel = new JLabel("");
lblNewLabel.setBounds(0, 259, 394, 13);
getContentPane().add(lblNewLabel);
setVisible(true);
new Thread() {
public void run() {
while (true) {
try {
Point mainPoint = getLocation();
Point point = MouseInfo.getPointerInfo().getLocation();
int x = point.x - mainPoint.x;
int y = point.y - mainPoint.y;
if (x < 0 || x > 400) {
sleep(10);
continue;
}
if (y < 0 || y > 300) {
sleep(10);
continue;
}
lblNewLabel.setText(str + "x : " + x + " y : " + y);
sleep(10);
} catch (Exception e) {
}
}
}
}.start();
this.addKeyListener(this);
}
public static void main(String[] args) {
new App();
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
str = "C ";
return;
}
if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
str = "S ";
return;
}
str = "D ";
}
public void keyReleased(KeyEvent e) {
str = "U ";
}
public void keyTyped(KeyEvent e) {
}
}
[/code]
上面贴的不对
这个是模拟关闭文件的
[code="java"]
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
public class APP
{
public static void main(String[] args)
{
try
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
robot.mouseMove(screenSize.width - 30, 5);
robot.delay(1000);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(300);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
catch (AWTException e)
{
e.printStackTrace();
}
}
}
[/code]