「javamouse控制」java中mouseevent
今天给各位分享javamouse控制的知识,其中也会对java中mouseevent进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java中控制鼠标点击的方法
- 2、java中的mouse事件
- 3、java怎么让按钮跟着鼠标移动
- 4、java 如何实现控制鼠标点击
- 5、java里怎么设置鼠标按下后mousepressed一直执行某个命令知道鼠标释放
- 6、java mouse事件
java中控制鼠标点击的方法
//利用 java.awt.Robot 类来自动完成一些键盘或鼠标的动作,下面是一个小例子
//程序流程:模拟鼠标左键点击 开始--运行--CMD--DIR--CLS--EXIT
//模拟右键点击 移动到右下角--右键点击--调整日期和时间--退出
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
//Test.java
public class Test {
public static void main(String[] args) throws Exception {
final Robot rb = new Robot();
new Thread(){
public void run(){
rb.delay(2000);
//回车
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
}
}.start();
JOptionPane.showMessageDialog(null,"以下程序自动执行,包括本对话框,请不必进行人为干预.\n如果不能正常执行程序,请先关闭输入法");
//设置开始菜单的大概位置
int x = 40;
int y = Toolkit.getDefaultToolkit().getScreenSize().height-10;
//鼠标移动到开始菜单,
rb.mouseMove(x,y);
rb.delay(500);
//单击三次开始菜单
for(int i=0; i3; i++)
pressMouse(rb,InputEvent.BUTTON1_MASK,500);
rb.delay(1000);
//运行CMD命令 r cmd enter
int[] ks = {KeyEvent.VK_R,KeyEvent.VK_C,KeyEvent.VK_M,KeyEvent.VK_D,KeyEvent.VK_ENTER,};
pressKeys(rb,ks,500);
rb.mouseMove(400,400);
rb.delay(500);
//运行DIR命令 dir enter
ks = new int[]{KeyEvent.VK_D,KeyEvent.VK_I,KeyEvent.VK_R,KeyEvent.VK_ENTER};
pressKeys(rb,ks,500);
rb.delay(1000);
//运行CLS命令 cls enter
ks = new int[]{KeyEvent.VK_C,KeyEvent.VK_L,KeyEvent.VK_S,KeyEvent.VK_ENTER};
pressKeys(rb,ks,500);
rb.delay(1000);
//运行EXIT命令 exit enter
ks = new int[]{KeyEvent.VK_E,KeyEvent.VK_X,KeyEvent.VK_I,KeyEvent.VK_T,KeyEvent.VK_ENTER};
pressKeys(rb,ks,500);
rb.delay(1000);
//右键测试
x=Toolkit.getDefaultToolkit().getScreenSize().width-10;
rb.mouseMove(x, y);
//如果是双键鼠标,请改用InputEvent.BUTTON2_MASK试试,我没有这种鼠标
pressMouse(rb,InputEvent.BUTTON3_MASK,500);
//显示日期调整对话框 a
pressKeys(rb,new int[]{KeyEvent.VK_A},1000);
rb.delay(2000);
pressKeys(rb,new int[]{KeyEvent.VK_ESCAPE},0);
rb.delay(1000);
new Thread(){
public void run(){
rb.delay(1000);
//回车
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
}
}.start();
JOptionPane.showMessageDialog(null,"演示完毕!");
}
//鼠标单击,要双击就连续调用
private static void pressMouse(Robot r,int m,int delay){
r.mousePress(m);
r.delay(10);
r.mouseRelease(m);
r.delay(delay);
}
//键盘输入
private static void pressKeys(Robot r,int[] ks,int delay){
for(int i=0; iks.length; i++){
r.keyPress(ks[i]);
r.delay(10);
r.keyRelease(ks[i]);
r.delay(delay);
}
}
}
java中的mouse事件
mouseover 鼠标移到某个对象上发生的事件
mouseout 鼠标从某个对象上离开发生的事件
click 鼠标单击事件
dclick 鼠标双击事件
clickdown 鼠标按下不松开发生的事件
clickdown 鼠标松开鼠标按键发生的事件
java怎么让按钮跟着鼠标移动
前提:你容器使用的布局需要null,这样你容器上其它组件都需要自己控制大小和位置了。
问题:你需要显示的鼠标坐标是基于屏幕还是容器还是窗口还是按钮?
给你个简单的显示鼠标基于窗口坐标的例子,鼠标坐标显示在窗口标题栏上。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TraceMouse extends JFrame{
JButton btnTrace;
boolean showMousePosition;
Insets insets;
public TraceMouse(){
setSize(640,480);
setLayout(null);
btnTrace=new JButton("点我显示鼠标坐标");
btnTrace.setSize(btnTrace.getPreferredSize());
add(btnTrace);
btnTrace.addMouseMotionListener(new MouseMotionListener(){
public void mouseDragged(MouseEvent e){
Point position=getMousePosition();
btnTrace.setLocation(position.x-insets.left-btnTrace.getWidth()/2,position.y-insets.top-btnTrace.getHeight()/2);
TraceMouse.this.setTitle(String.format("当前鼠标坐标为:(%1$d,%2$d)",position.x,position.y));
}
public void mouseMoved(MouseEvent e){
Point position=getMousePosition();
btnTrace.setLocation(position.x-insets.left-btnTrace.getWidth()/2,position.y-insets.top-btnTrace.getHeight()/2);
}
});
addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
Point position=getMousePosition();
btnTrace.setLocation(position.x-insets.left-btnTrace.getWidth()/2,position.y-insets.top-btnTrace.getHeight()/2);
}
});
setVisible(true);
insets=getInsets();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args){
new TraceMouse();
}
}
java 如何实现控制鼠标点击
1//例子1
2import java.applet.*;import java.awt.*;
3import java.awt.event.*;
4public class Example18_1 extends Applet implements MouseListener
5{ TextField text;
6 public void init()
7 { text=new TextField(40); add(text);
8 addMouseListener(this) ;//向小程序增加鼠标事件监视器。
9 }
10 public void mousePressed(MouseEvent e)
11 { text.setText("鼠标键按下了,位置是"+e.getX()+","+e.getY() );
12 }
13 public void mouseReleased(MouseEvent e)
14 { text.setText(" 鼠标松开了,位置是"+e.getX()+","+e.getY() );
15 }
16 public void mouseEntered(MouseEvent e)
17 { text.setText(" 鼠标进来了,位置是"+e.getX()+","+e.getY() );
18 }
19 public void mouseExited(MouseEvent e)
20 { text.setText(" 鼠标走开了");
21 }
22 public void mouseClicked(MouseEvent e)
23 { if(e.getClickCount()==2)
24 { text.setText("鼠标键双击,位置:"+e.getX()+","+e.getY());
25 }
26 else {}
27 }
28}
29
30//例子2
31import java.awt.*;import java.awt.event.*;
32class MyCanvas extends Canvas implements MouseListener
33{ int left=-1,right=-1; //记录左、右键用的变量。
34 int x=-1,y=-1; //记录鼠标位置用的变量。
35 MyCanvas()
36 { setSize(100,100);
37 setBackground(Color.cyan) ;
38 addMouseListener(this);
39 }
40 public void paint(Graphics g)
41 { if(left==1)
42 { g.drawOval(x-10,y-10,20,20);
43 }
44 else if(right==1)
45 { g.drawRect(x-8,y-8,16,16);
46 }
47 }
48 public void mousePressed(MouseEvent e)
49 { x=e.getX(); y=e.getY();
50 if(e.getModifiers()==InputEvent.BUTTON1_MASK)
51 { left=1;right=-1;
52 repaint();
53 }
54 else if(e.getModifiers()==InputEvent.BUTTON3_MASK)
55 { right=1; left=-1;
56 repaint();
57 }
58 }
59 public void mouseReleased(MouseEvent e){}
60 public void mouseEntered(MouseEvent e){}
61 public void mouseExited(MouseEvent e)
62 { left=-1;right=-1;
63 repaint();
64 }
65 public void mouseClicked(MouseEvent e){}
66 public void update(Graphics g)
67 { if(left==1||right==1)
68 { paint(g);
69 }
70 else
71 { super.update(g);
72 }
73 }
74}
75public class Example18_2
76{ public static void main(String args[])
77 { Frame f=new Frame();
78 f.setBounds(100,100,200,200);f.setVisible(true);
79 f.addWindowListener(new WindowAdapter() //适配器
80 {public void windowClosing(WindowEvent e)
81 {System.exit(0);
82 }
83 });
84 f.add(new MyCanvas(),BorderLayout.CENTER);//添加画布。
85 f.validate();
86 }
87}
88
89//例子3
90import java.awt.*;import java.awt.event.*;
91import java.applet.*;
92public class Example18_3 extends Applet implements MouseListener
93{ TextField text; Button button;
94 TextArea textArea;
95 public void init()
96 { text=new TextField(10); text.addMouseListener(this);
97 button=new Button("按钮"); button.addMouseListener(this);
98 addMouseListener(this);
99 textArea=new TextArea(8,28);
100 add(button);add(text);add(textArea);
101 }
102 public void mousePressed(MouseEvent e)
103 { if(e.getSource()==button)
104 {textArea.append("\n在按钮上鼠标按下,位置:"+"("+e.getX()+","+e.getY()+")");
105 }
106 else if(e.getSource()==text)
107 {textArea.append("\n在文本框上鼠标按下,位置:"+"("+e.getX()+","+e.getY()+")");
108 }
109 else if(e.getSource()==this)
110 {textArea.append("\n在容器上鼠标按下,位置:"+"("+e.getX()+","+e.getY()+")");
111 }
112 }
113 public void mouseReleased(MouseEvent e) {}
114 public void mouseEntered(MouseEvent e) {}
115 public void mouseExited(MouseEvent e) {}
116 public void mouseClicked(MouseEvent e)
117 { if(e.getClickCount()=2)
118 textArea.setText("鼠标连击,位置:"+"("+e.getX()+","+e.getY()+")");
119 }
120}
java里怎么设置鼠标按下后mousepressed一直执行某个命令知道鼠标释放
button onmousedown="testOnMouseDown()" onmouseup="testOnMouseUp()"测试/button
function testOnMouseDown() {
鼠标按下时执行的方法;
}
function testOnMouseUp() {
鼠标放开时执行的方法;
}
java mouse事件
onClick 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击
onDblClick 鼠标双击事件
onMouseDown 鼠标上的按钮被按下了
onMouseUp 鼠标按下后,松开时激发的事件
onMouseOver 当鼠标移动到某对象范围的上方时触发的事件
onMouseMove 鼠标移动时触发的事件
onMouseOut 当鼠标离开某对象范围时触发的事件
给你写了个小DEMO,希望对你有用:
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
HTML
HEAD
TITLE New Document /TITLE
META NAME="Generator" CONTENT="EditPlus"
META NAME="Author" CONTENT=""
META NAME="Keywords" CONTENT=""
META NAME="Description" CONTENT=""
SCRIPT LANGUAGE="JavaScript"
!--
function show(flag){
if(flag==1){
document.all("showt1").value="单击";
}else if(flag==2){
document.all("showt1").value="双击";
}else if(flag==3){
document.all("showt2").value="按下";
}else if(flag==4){
document.all("showt2").value="弹起";
}else if(flag==5){
document.all("showt3").value="覆盖";
}else if(flag==6){
document.all("showt3").value="移开";
}
}
//--
/SCRIPT
/HEAD
BODY onclick="show(1)" ondblclick="show(2)" onMouseDown="show(3)" onMouseUp="show(4)"
TABLE
TR
TD整个页面测试单击双击,按下弹.起/TD
/TR
/TABLE
单双击INPUT TYPE="text" NAME="showt1"
按下弹起INPUT TYPE="text" NAME="showt2"
TABLE bgcolor="yellow" onmouseover="show(5)" onmouseout="show(6)"
TR
td这一块测试鼠标移开或覆盖/td
TD width="200" height="200"INPUT TYPE="text" NAME="showt3"/TD
/TR
/TABLE
/BODY
/HTML
关于javamouse控制和java中mouseevent的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。