如何用JAVA编一个 鼠标在在屏幕上移动的 并点击的 程序?
3个回答
展开全部
楼主提问的水平很高啊,题目的要求都没有,如何回答。。。
相关类都在
import java.awt.*;
import javax.swing.* ;
动作事件相关类在import java.awt.event.*;中,在代码头import就可以看了
给一个相关的代码吧,包括所有的鼠标事件。
下面代码包括
按下,释放,进入区域,离开区域,点击 这5个事件
鼠标的相关事件会在窗口上方的文本区显示。。。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestMouseEvent extends JFrame implements MouseListener{
JPanel jp = new JPanel();
JTextField jtf = new JTextField();
public TestMouseEvent(){
setTitle("Mouse Event");
getContentPane().add(jtf,BorderLayout.NORTH);
getContentPane().add(jp,BorderLayout.CENTER);
jp.addMouseListener(this);
}
public static void main(String[] args){
TestMouseEvent frame = new TestMouseEvent();
frame.setSize(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void mousePressed(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出按下";
jtf.setText(s);
}
public void mouseReleased(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出释放";
jtf.setText(s);
}
public void mouseClicked(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出点击";
jtf.setText(s);
}
public void mouseEntered(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出进入";
jtf.setText(s);
}
public void mouseExited(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出离开";
jtf.setText(s);
}
}
相关类都在
import java.awt.*;
import javax.swing.* ;
动作事件相关类在import java.awt.event.*;中,在代码头import就可以看了
给一个相关的代码吧,包括所有的鼠标事件。
下面代码包括
按下,释放,进入区域,离开区域,点击 这5个事件
鼠标的相关事件会在窗口上方的文本区显示。。。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestMouseEvent extends JFrame implements MouseListener{
JPanel jp = new JPanel();
JTextField jtf = new JTextField();
public TestMouseEvent(){
setTitle("Mouse Event");
getContentPane().add(jtf,BorderLayout.NORTH);
getContentPane().add(jp,BorderLayout.CENTER);
jp.addMouseListener(this);
}
public static void main(String[] args){
TestMouseEvent frame = new TestMouseEvent();
frame.setSize(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void mousePressed(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出按下";
jtf.setText(s);
}
public void mouseReleased(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出释放";
jtf.setText(s);
}
public void mouseClicked(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出点击";
jtf.setText(s);
}
public void mouseEntered(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出进入";
jtf.setText(s);
}
public void mouseExited(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出离开";
jtf.setText(s);
}
}
展开全部
//JAVA中的 java.awt.Robot 类可以操作鼠标和键盘的动作
//以下程序将随机移动鼠标,只有当鼠标移动到屏幕中间红色的小窗口上时,才会点击一下,然后退出程序
//: 保存成: AutoMouseTest.java ,编译运行就行了
import java.awt.Color;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JWindow;
public class AutoMouseTest {
public static void main(String[] args) throws Exception {
moveAndClick(new Win());
}
private static void moveAndClick(final Win win) throws Exception{
final Robot r = new Robot();
new Thread(){
public void run(){
int w = Toolkit.getDefaultToolkit().getScreenSize().width;
int h = Toolkit.getDefaultToolkit().getScreenSize().height;
while(!win.stop){
//随机产生 x,y 坐标
int x=(int)(Math.random()*w);
int y=(int)(Math.random()*h);
//鼠标移动到 x,y
r.mouseMove(x, y);
//为了不乱点鼠标,设置条件:只有当鼠标到红色的 WIN 上才点击!
if(win.win.getBounds().contains(x,y)){
//按下鼠标
r.mousePress(MouseEvent.BUTTON1_MASK);
//释放鼠标
r.mouseRelease(MouseEvent.BUTTON1_MASK);
}
String info = "<html>x:"+x+"<br/>y:"+y;
//显示鼠标当前坐标的信息
win.setInfo(info);
try {sleep(50);} catch (Exception e) {}
}
}
}.start();
}
}
class Win{
JWindow win = new JWindow();
JLabel info = new JLabel();
boolean stop;
public void setInfo(String s){info.setText(s);}
Win(){
info.setOpaque(true);
info.setBackground(Color.red);
info.setHorizontalAlignment(JLabel.CENTER);
win.setSize(80,50);
win.setAlwaysOnTop(true);
win.add(info);
win.setLocationRelativeTo(null);
win.setVisible(true);
win.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
stop=true;
win.setVisible(false);
JOptionPane.showMessageDialog(win,"鼠标已移动到了WIN上面,并点击了一下,程序将退出!");
System.exit(0);
}
});
win.setVisible(true);
}
}
//以下程序将随机移动鼠标,只有当鼠标移动到屏幕中间红色的小窗口上时,才会点击一下,然后退出程序
//: 保存成: AutoMouseTest.java ,编译运行就行了
import java.awt.Color;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JWindow;
public class AutoMouseTest {
public static void main(String[] args) throws Exception {
moveAndClick(new Win());
}
private static void moveAndClick(final Win win) throws Exception{
final Robot r = new Robot();
new Thread(){
public void run(){
int w = Toolkit.getDefaultToolkit().getScreenSize().width;
int h = Toolkit.getDefaultToolkit().getScreenSize().height;
while(!win.stop){
//随机产生 x,y 坐标
int x=(int)(Math.random()*w);
int y=(int)(Math.random()*h);
//鼠标移动到 x,y
r.mouseMove(x, y);
//为了不乱点鼠标,设置条件:只有当鼠标到红色的 WIN 上才点击!
if(win.win.getBounds().contains(x,y)){
//按下鼠标
r.mousePress(MouseEvent.BUTTON1_MASK);
//释放鼠标
r.mouseRelease(MouseEvent.BUTTON1_MASK);
}
String info = "<html>x:"+x+"<br/>y:"+y;
//显示鼠标当前坐标的信息
win.setInfo(info);
try {sleep(50);} catch (Exception e) {}
}
}
}.start();
}
}
class Win{
JWindow win = new JWindow();
JLabel info = new JLabel();
boolean stop;
public void setInfo(String s){info.setText(s);}
Win(){
info.setOpaque(true);
info.setBackground(Color.red);
info.setHorizontalAlignment(JLabel.CENTER);
win.setSize(80,50);
win.setAlwaysOnTop(true);
win.add(info);
win.setLocationRelativeTo(null);
win.setVisible(true);
win.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
stop=true;
win.setVisible(false);
JOptionPane.showMessageDialog(win,"鼠标已移动到了WIN上面,并点击了一下,程序将退出!");
System.exit(0);
}
});
win.setVisible(true);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以参考以一下这个程序,有鼠标的移动,点击
import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;
public class TestMouseEvent extends JFrame{
private JTextArea jta;
private JLabel jlab;
JScrollPane jsp;
public TestMouseEvent(){
jta=new JTextArea(20,20);
jlab=new JLabel("鼠标位置信息");
jsp=new JScrollPane(jta);
addEventHandle();
this.add(jsp);
this.add(jlab,"South");
this.setSize(400, 400);
this.setLocation(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addEventHandle(){
jta.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
jta.append("鼠标被点击:("+e.getX()+","+e.getY()+")\n");
}
public void mouseEntered(MouseEvent e){
jta.append("鼠标进入\n");
}
public void mouseExited(MouseEvent e){
jta.append("鼠标移出\n");
}
});
jta.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e){
jlab.setText("鼠标当前位置:("+e.getX()+","+e.getY()+")\n");
}
});
}
public static void main(String[] args) {
new TestMouseEvent();
}
}
import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;
public class TestMouseEvent extends JFrame{
private JTextArea jta;
private JLabel jlab;
JScrollPane jsp;
public TestMouseEvent(){
jta=new JTextArea(20,20);
jlab=new JLabel("鼠标位置信息");
jsp=new JScrollPane(jta);
addEventHandle();
this.add(jsp);
this.add(jlab,"South");
this.setSize(400, 400);
this.setLocation(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addEventHandle(){
jta.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
jta.append("鼠标被点击:("+e.getX()+","+e.getY()+")\n");
}
public void mouseEntered(MouseEvent e){
jta.append("鼠标进入\n");
}
public void mouseExited(MouseEvent e){
jta.append("鼠标移出\n");
}
});
jta.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e){
jlab.setText("鼠标当前位置:("+e.getX()+","+e.getY()+")\n");
}
});
}
public static void main(String[] args) {
new TestMouseEvent();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询