JScrollPane 内 JPanel 上的鼠标坐标偏移
我在 JScrollPane 中有一个 JPanel。 JPanel 附加了一个 MouseListener 以对鼠标事件做出反应。当我将 JPanel 直接添加到父 JComponent (中间没有 JScrollPane)时,一切正常。将 JScrollPane 添加到混合中后,发送到 JPanel 上的事件处理程序的鼠标坐标都沿 x 轴和 y 轴都有正偏移。
示例(JPanel 上的鼠标侦听器):
public void mousePressed(MouseEvent ev) {
System.out.println(ev.getPoint());
System.out.println(this.getMousePosition());
}
当我单击 JPanel 的左上角而不是 (0,0) 时,我从两个函数中得到类似 (5,60) 的内容,而右下角(对于尺寸为 600x400 的 JPanel)返回 (605,460)。知道这个线性偏移来自哪里吗?
编辑:没关系,我将其简化为一个简单的测试用例,并且它按预期工作。因此,一定是我周围的代码中的某些内容导致了这种行为。
public class JScrollPaneTest {
public static void main(String[] args) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setLayout(new BorderLayout());
MyPanel panel = new MyPanel();
window.getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER);
window.setSize(new Dimension(600, 400));
window.setVisible(true);
}
}
public class MyPanel extends JPanel implements MouseListener, Scrollable {
public MyPanel() {
setLayout(null);
addMouseListener(this);
this.setAutoscrolls(true);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(2000, 2000);
}
@Override
public void mousePressed(MouseEvent ev) {
System.out.println("Event mouse position: " + ev.getPoint());
System.out.println("Panel mouse position: " + this.getMousePosition());
}
}
I have a JPanel inside a JScrollPane. The JPanel has a MouseListener attached to react to mouse events. When I add the JPanel directly to the parent JComponent (without the JScrollPane in between) it all works correctly. Once I add the JScrollPane to the mix, the mouse coordinates sent to the event handlers on the JPanel all have a positive offset along both the x and y axis.
Example (mouse listener on JPanel):
public void mousePressed(MouseEvent ev) {
System.out.println(ev.getPoint());
System.out.println(this.getMousePosition());
}
When I click on the upper left corner of the JPanel instead of (0,0) I get something like (5,60) from both functions while the lower right corner (for a JPanel sized 600x400) returns (605,460). Any idea where this linear offset comes from?
Edit: Nevermind, I reduced it to a simple testcase and it works as expected. So it must be something in my surrounding code that is causing this behavior.
public class JScrollPaneTest {
public static void main(String[] args) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setLayout(new BorderLayout());
MyPanel panel = new MyPanel();
window.getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER);
window.setSize(new Dimension(600, 400));
window.setVisible(true);
}
}
public class MyPanel extends JPanel implements MouseListener, Scrollable {
public MyPanel() {
setLayout(null);
addMouseListener(this);
this.setAutoscrolls(true);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(2000, 2000);
}
@Override
public void mousePressed(MouseEvent ev) {
System.out.println("Event mouse position: " + ev.getPoint());
System.out.println("Panel mouse position: " + this.getMousePosition());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请问您能根据这个例子重现您的问题吗?
please could you reproduce your problem based on this example?