当前位置:首页>开发>正文

java怎样获取鼠标在屏幕的坐标 java获取鼠标坐标

2023-04-29 10:48:50 互联网 未知 开发

 java怎样获取鼠标在屏幕的坐标 java获取鼠标坐标

java怎样获取鼠标在屏幕的坐标

import java.awt.*
import javax.swing.*
import java.awt.event.*
import java.util.EventListener
class myMouseListener implements MouseMotionListener
{
public void mouseMoved(MouseEvent e){
int x=e.getX()
int y=e.getY()
String s="当前鼠标坐标:" x , y
MouseMove.lab.setText(s)
}
public void mouseDragged(MouseEvent e){}
}
public class MouseMove extends JFrame{
public static JLabel lab=new JLabel()
public MouseMove() {
}
public static void main(String [] args)
{
// MouseMove fm=new MouseMove("鼠标坐标测试")
JFrame fm=new JFrame("鼠标坐标测试")
JPanel fp=new JPanel()
fp.addMouseMotionListener(new myMouseListener())//对在面板上的鼠标移动进行监听。
Container con=fm.getContentPane()
fp.add(lab)
con.add(fp)
fm.setSize(500,400)
fm.setVisible(true)
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
}

java获取鼠标坐标

//你运行一下,看在你的电脑上能不能用。
//不能用的话肯定是坐标不对
//把mouseMoved方法里的注释去掉重新获取
import java.awt.Dimension
import java.awt.MouseInfo
import java.awt.Point
import java.awt.Robot
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.MouseEvent
import java.awt.event.MouseMotionListener

import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.WindowConstants

public class MouseHelp extends javax.swing.JPanel 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()
frame.getContentPane().add(new MouseHelp())
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
frame.pack()
frame.setVisible(true)
}

public MouseHelp() {
super()
initGUI()
}

private void initGUI() {

try {
robot=new Robot()
addMouseMotionListener(this)
setPreferredSize(new Dimension(400, 300))
this.setLayout(null)
{
textButton = new JButton()
this.add(textButton)
textButton.setText("u8fd0 u884c")
textButton.setBounds(136, 72, 127, 22)
textButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
robot.mouseMove(30,745)
try {
Thread.sleep(1000)
robot.mousePress(MouseEvent.BUTTON1_MASK)
robot.mouseRelease(MouseEvent.BUTTON1_MASK)
Thread.sleep(1000)
robot.mouseMove(150,481)
robot.mousePress(MouseEvent.BUTTON1_MASK)
robot.mouseRelease(MouseEvent.BUTTON1_MASK)
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace()
}
}})
}
} catch (Exception e) {
e.printStackTrace()
}
}

public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
//从这里获取鼠标的全局坐标
//Point mousepoint = MouseInfo.getPointerInfo().getLocation()
//System.out.println(mousepoint.x " " mousepoint.y)
}

}

Java 怎么获取鼠标当前的(jpanel)位置!鼠标是移动会发生变化!请给出代码谢谢

代码如下:
import java.awt.Dimension
import java.awt.MouseInfo
import java.awt.Point
import java.awt.Robot
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.MouseEvent
import java.awt.event.MouseMotionListener

import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.WindowConstants

public class MouseHelp extends javax.swing.JPanel 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()
frame.getContentPane().add(new MouseHelp())
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
frame.pack()
frame.setVisible(true)
}

public MouseHelp() {
super()
initGUI()
}

private void initGUI() {

try {
robot=new Robot()
addMouseMotionListener(this)
setPreferredSize(new Dimension(400, 300))
this.setLayout(null)
{
textButton = new JButton()
this.add(textButton)
textButton.setText("u8fd0 u884c")
textButton.setBounds(136, 72, 127, 22)
textButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
robot.mouseMove(30,745)
try {
Thread.sleep(1000)
robot.mousePress(MouseEvent.BUTTON1_MASK)
robot.mouseRelease(MouseEvent.BUTTON1_MASK)
Thread.sleep(1000)
robot.mouseMove(150,481)
robot.mousePress(MouseEvent.BUTTON1_MASK)
robot.mouseRelease(MouseEvent.BUTTON1_MASK)
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace()
}
}})
}
} catch (Exception e) {
e.printStackTrace()
}
}

public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
//从这里获取鼠标的全局坐标
Point mousepoint = MouseInfo.getPointerInfo().getLocation()
System.out.println(mousepoint.x " " mousepoint.y)
}
}

java获得鼠标相对于窗体的坐标

有的,你可以去继承MouseEvent这个类,里面有个方法叫做,getXOnScreen()和getYOnScreen(),

getXOnScreen
public int getXOnScreen()

返回事件的绝对水平 x 坐标。在虚拟设备多屏幕环境中,桌面区域可以跨越多个物理屏幕设备,这些坐标相对于虚拟坐标系。否则,这些坐标相对于与
Component 的 GraphicsConfiguration 关联的坐标系。

返回:
x 表示绝对水平位置的整数。

getYOnScreen
public int getYOnScreen()

返回事件的绝对垂直 y 坐标。在虚拟设备多屏幕环境中,桌面区域可以跨越多个物理屏幕设备,这些坐标相对于虚拟坐标系。否则,这些坐标相对于与
Component 的 GraphicsConfiguration 关联的坐标系。

返回:
y 表示绝对垂直位置的整数。

这个就是你在窗体(Frame里的位置了),希望能帮到你。

JAVA中如何获得当前鼠标选中的值

//类名 MyMouseLister
import java.awt.event.MouseEvent
import java.awt.event.MouseListener

public class MyMouseLister implements MouseListener{
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
}

//类名OnMouseOver
import java.awt.GridLayout
import java.awt.event.MouseEvent

import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JPanel

public class OnMouseOver extends MyMouseLister{
private JLabel test1
private JLabel test2
private JLabel test3
private JLabel test4
private JPanel p
private JFrame f
public OnMouseOver(){
test1 = new JLabel("test1.text")
test1.addMouseListener(this)
test2 = new JLabel("test2.text")
test2.addMouseListener(this)
test3 = new JLabel("test3.text")
test3.addMouseListener(this)
test4 = new JLabel("test4.text")
test4.addMouseListener(this)
p = new JPanel(new GridLayout(2,2))
p.add(test1,0)
p.add(test2,1)
p.add(test3,2)
p.add(test4,3)
f = new JFrame("Test")
f.add(p)
f.setVisible(true)
f.setBounds(300, 200, 300, 200)
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}

//事件可以自己换
public void mouseEntered(MouseEvent e) {
if(e.getSource() == test1){
System.out.println(test1.getText())
}else if(e.getSource() == test2){
System.out.println(test2.getText())
}else if(e.getSource() == test3){
System.out.println(test3.getText())
}else if(e.getSource() == test4){
System.out.println(test4.getText())
}
}

public static void main(String[] args) {
new OnMouseOver()
}
}

希望能帮到你虽然这个是JLabel但是道理是相通的,你看看就明白了。
//附上操作表格的方法:
getSelectedColumn()返回列索引
getSelectedRow()返回行索引
getValueAt()返回单元格的值