java 使用 getX() 和 getY() 方法来获取面板的位置,而不是框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29173941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making the getX() and getY() methods to get the locations of the panel, not the frame
提问by Ski
I'm using the getX()
and getY()
methods in the MouseListener
class to get the location of the panel, but it's getting the location of the frame instead, and that includes the top of the title bar and the sides, so it's not getting the location that I want. I also have the:
我正在使用类中的getX()
和getY()
方法MouseListener
来获取面板的位置,但它正在获取框架的位置,并且包括标题栏的顶部和侧面,因此它没有获取我想要的位置. 我还有:
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
method in my paint class to override the frame so when you paint in graphics, and set the paint location of x, and y, it sets it to the panel location, and not the frame location, but I don't know how to make it so that the MouseListener class does the same. Please help. Thanks.
我的绘画类中的方法来覆盖框架,因此当您在图形中绘画并设置 x 和 y 的绘画位置时,它将其设置为面板位置,而不是框架位置,但我不知道如何制作它使 MouseListener 类执行相同的操作。请帮忙。谢谢。
Code for the MouseListener
class:
MouseListener
类的代码:
package events;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Events implements ActionListener, MouseListener, MouseMotionListener {
static Events events = new Events();
int x;
int y;
public void actionPerformed(ActionEvent e) {
}
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) {
x = e.getX();
y = e.getY();
System.out.println("X:" + x + " " + "Y:" + y);
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
}
}
采纳答案by Ski
Nevermind. I can just call the panel name for the panel x, and y locations. But thanks anyways.
没关系。我可以只调用面板 x 和 y 位置的面板名称。不过还是谢谢。
x = P.g.getX();
y = P.g.getY()