dabocaiqq
2020-05-27 11:58
采纳率: 66.6%
浏览 171

Java语言高分悬赏:怎么获得鼠标在Jpanel中的坐标,并且在坐标处画圆

Java语言高分悬赏:怎么获得鼠标在Jpanel中的坐标,并且在坐标处画圆
Java语言高分悬赏:怎么获得鼠标在Jpanel中的坐标,并且在坐标处画圆

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 毕小宝 2020-06-20 08:59
    已采纳

    需要监听鼠标事件,它回传鼠标的点击位置:

    public void mousePressed(MouseEvent e) {
            System.out.println("X坐标:"+e.getX()+" "+"Y坐标"+e.getY());
            int clickPointX = e.getX();
            int clickPointY = e.getY();
                }
    
    打赏 评论
  • 断岸残桥 2020-05-27 20:52
    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
    }
    
    打赏 评论

相关推荐 更多相似问题