@Override
public void mouseClicked(MouseEvent e) {
if(egetButton()==eBUTTON1){//点击鼠标左键
int x=egetX();
int y=egetY();
String str="您点击的是左键,鼠标当前点击位置的坐标是(" + x + "," + y+")";
labelsetText(str);
}else if(egetButton()==eBUTTON2){//点击鼠标滑轮
int x=egetX();
int y=egetY();
String str="您点击的是滑轮,鼠标当前点击位置的坐标是(" + x + "," + y+")";
labelsetText(str);
}
else if(egetButton()==eBUTTON3){//点击鼠标右键
int x=egetX();
int y=egetY();
String str="您点击的是右键,鼠标当前点击位置的坐标是(" + x + "," + y+")";
labelsetText(str);
}
}
});
egetButton()返回值分别为NOBUTTON、BUTTON1、BUTTON2、BUTTON3,分别代表着无点击、左击、中间键、右击三种情况。
给Java初学者的建议:
1,不一定要把java学深入。有多少人搞程序是喜欢这个的,搞程序一半是因为钱多,一半是因为晋升公平。
2,初学者学java至少要脱产半年的时间才能算了解了,才适合被人带,因为至少半年的时间,才知道遇到问题如何解决,有的人需要更长的时间。现在有很多人已经有5年左右的经验了,在他们的压制下,java初学者很难有逆袭的机会了。
3,行业环境,现在数据存储,后台打包的方案的创业公司已经有苗头,也就是说,后台开发已经被集中解决,日后会越来越成熟,这就是意味着,java招聘需要有经验的人,也就是说,必须熬几年后才能有突破。
4,java离市场较远,所以呢?java开发人员很难有一个大的突破。
5,程序里面有变量所以解决了一系列类似的问题,所以注定了程序必须提供大批服务时才能有高收益。
代码如下:
import javaawtDimension;
import javaawtMouseInfo;
import javaawtPoint;
import javaawtRobot;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventMouseEvent;
import javaawteventMouseMotionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingWindowConstants;
public class MouseHelp extends javaxswingJPanel implements MouseMotionListener {
private JButton textButton;
Robot robot;
/
Auto-generated main method to display this
JPanel inside a new JFrame
/
public static void main(String[] args) {
JFrame frame = new JFrame();
framegetContentPane()add(new MouseHelp());
framesetDefaultCloseOperation(WindowConstantsDISPOSE_ON_CLOSE);
framepack();
framesetVisible(true);
}
public MouseHelp() {
super();
initGUI();
}
private void initGUI() {
try {
robot=new Robot();
addMouseMotionListener(this);
setPreferredSize(new Dimension(400, 300));
thissetLayout(null);
{
textButton = new JButton();
thisadd(textButton);
textButtonsetText("\u8fd0 \u884c");
textButtonsetBounds(136, 72, 127, 22);
textButtonaddActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
robotmouseMove(30,745);
try {
Threadsleep(1000);
robotmousePress(MouseEventBUTTON1_MASK);
robotmouseRelease(MouseEventBUTTON1_MASK);
Threadsleep(1000);
robotmouseMove(150,481);
robotmousePress(MouseEventBUTTON1_MASK);
robotmouseRelease(MouseEventBUTTON1_MASK);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1printStackTrace();
}
}});
}
} catch (Exception e) {
eprintStackTrace();
}
}
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
//从这里获取鼠标的全局坐标
Point mousepoint = MouseInfogetPointerInfo()getLocation();
Systemoutprintln(mousepointx+"\t"+mousepointy);
}
}
以上就是关于java鼠标点击事件怎么做全部的内容,包括:java鼠标点击事件怎么做、Java 怎么获取鼠标当前的(jpanel)位置!鼠标是移动会发生变化!请给出代码谢谢、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)