[英]Passing pointer position to an object in Java
I've got a JPanel class called Board with a static subclass, MouseHanlder, which tracks the mouse position along the appropriate listener in Board. 我有一个名为Board的JPanel类,带有一个静态子类MouseHanlder,它沿着Board中适当的侦听器跟踪鼠标的位置。 My Board class has fields pointerX and pointerY. 我的Board类具有pointerX和pointerY字段。
How do i pass the e.getX() and e.getY() from the MouseHandler subclass to its super class JPanel? 如何将MouseHandler子类中的e.getX()和e.getY()传递给其父类JPanel? I tried with getters, setters, super, and cant get the data transfer between subclass and parent class. 我尝试使用getters,setters,super和无法获得子类和父类之间的数据传输。 I'm certain it's a concept issue, but im stuck. 我敢肯定这是一个概念问题,但我陷入了困境。
Thanks! 谢谢!
Due popular demand, some code. 由于受欢迎的需求,一些代码。 This is the code without any atempt of passing : 这是没有任何通过的代码:
public class Board extends JPanel {
int x; // Mouse pointer fields.
int y;
public Board() {
blah blah
MouseHandler handler = new MouseHandler();
addMouseMotionListener(handler);
}
static class MouseHandler implements MouseMotionListener {
int pointerX;
int pointerY;
public void mouseMoved(MouseEvent e) {
i'd like to do something like:
super.x = e.getX();
super.x = e.getY();
or
Board.setX() = e.getX(); // Missing setters below, this is just an example.
Board.setX() = e.getY();
}
}
} }
This because your static implementation of the class doesn't see your jpanel instance. 这是因为您的类的静态实现看不到您的jpanel实例。 You can do it passing a reference to the MouseAdapter
(or MouseListener
) 您可以通过传递对MouseAdapter
(或MouseListener
)的引用来完成此MouseAdapter
class MyPanel extends JPanel
{
MyPanel()
{
item.addMouseListener(new MyListener(this));
}
void pass(int x, int y)
{
//whatever
}
class MyListener extends MouseAdapter
{
MyPanel ref;
MyListener(MyPanel ref)
{
this.ref = ref;
}
public void mousePressed(MouseEvent e)
{
ref.pass(e.getX(), e.getY());
}
}
}
Simple. 简单。 Do an e.getSource() which will give you the source on which the event occurred. 做一个e.getSource(),它将为您提供事件发生的源。 In your case it will be your Board class. 在您的情况下,这将是您的董事会课程。 Simply cast the instance to a Board instance and you are set. 只需将实例转换为Board实例即可完成设置。
public class Board extends JPanel {
int x; // Mouse pointer fields.
int y;
public Board() {
blah blah
MouseHandler handler = new MouseHandler();
addMouseMotionListener(handler);
}
static class MouseHandler implements MouseMotionListener {
int pointerX;
int pointerY;
public void mouseMoved(MouseEvent e) {
Board b = (Board) e.getSource();
b.setX(e.getX());
b.setY(e.getY());
}
}
}
Here's one way. 这是一种方法。 This technique uses the implicit reference to the outer class of MouseHandler
: 该技术使用对MouseHandler
外部类的隐式引用:
public class Board extends JPanel {
public Board() {
addMouseListener(new MouseHandler());
}
private void doSomething(int x, int y) {
// ...
}
private final class MouseHandler extends MouseAdapter {
public void mouseMoved(MouseEvent e) {
Board.this.doSomething(e.getX(), e.getY());
}
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.