java - 鼠标点击坐标不起作用

标签 java swing click mouse mouselistener

我有一个程序,目前我正在尝试在单击时找到面板上的坐标位置。到目前为止我得到了 0,0。有什么建议吗?

P.S - 抱歉缺少评论...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.lang.Math;

public class ShapePanel extends JPanel{

  private JButton startButton, stopButton;
  private JTextField textField;
  private JLabel label;
  private Timer timer;
  private final int DELAY = 5;


  ArrayList<Shape> obj = new ArrayList<Shape>();


  public static void main(String[] args){

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new ShapePanel());
    frame.pack();
    frame.setVisible(true);
  }

  public ShapePanel(){

    DrawingPanel dpanel = new DrawingPanel();
    JPanel cpanel = new JPanel();

    startButton = new JButton("Start");
    stopButton = new JButton("Stop");

    cpanel.setLayout(new GridLayout(2,1));
    cpanel.add(startButton);
    cpanel.add(stopButton);


    dpanel.addMouseListener(new MouseListen());

    TimerListener tListen = new TimerListener();

    startButton.addActionListener(tListen);
    stopButton.addActionListener(tListen);
    add(cpanel);
    add(dpanel);

    timer = new Timer(DELAY, tListen);
    timer.start();

  }
  private class TimerListener implements ActionListener{


    public void actionPerformed(ActionEvent e){

      if (e.getSource() == timer){

        for (int i = 0; i < obj.size(); i++){
          obj.get(i).move();
        }

      }else if (e.getSource() == startButton){

        timer.start();
      }else if (e.getSource() == stopButton){

        timer.stop();
      }


      repaint();
    }
  }

  private class MouseListen implements MouseListener {


    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }

    public void mouseClicked(MouseEvent e) {


      System.out.println(getX());
    }

}

  private class DrawingPanel extends JPanel{

    DrawingPanel(){

      setPreferredSize(new Dimension(400,400));
      setBackground(Color.pink);

    }
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      for(int i = 0; i < obj.size(); i++){

        obj.get(i).display(g);
      }

    }
  }
}

最佳答案

检查代码的以下部分:

public void mouseClicked(MouseEvent e) {


  System.out.println(getX());  
       // you are putting here only getX() which get the postion of panel
        // put e.getX() instead
}

关于java - 鼠标点击坐标不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19271785/

相关文章:

java - 使 Swing-JTabbedPane-Tab 填充其容器的整个宽度的最简单方法

javascript - 点击 radio 没用。为什么?

jQuery - 单击链接将所有类从一个元素复制到另一个元素

Jquery Onclick 不再发生第二次

java - Google DRIVE API V3 - 获取带有名称的文件夹 ID

javascript - 如何在特定 session 中跟踪特定浏览器的网站

java - 你能在struts-config.xml中使用正则表达式吗?

java - MongoDB Morphia 仅保存 1 个用户

java - Java Swing 中的贪吃蛇游戏——我的蛇只会长大

Java:同一个类中有多个 ActionListeners?