JScrollPane中的JPanel上的鼠标坐标偏移量

时间:2011-07-05 13:55:47

标签: java swing event-handling jpanel jscrollpane

我在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)的东西,而右下角(对于JPanel大小为600x400)则返回(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());
}
}

1 个答案:

答案 0 :(得分:3)

请您根据此示例重现您的问题吗?

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class MainViewPort {

    public static void main(String[] args) {
        JPanel viewportPanelTop = new JPanel();
        JPanel viewportPanelBottom = new JPanel();
        JPanel viewportPanel = new JPanel();
        viewportPanel.setBackground(Color.BLACK);
        viewportPanel.setPreferredSize(new Dimension(1000, 1000));
        JPanel viewportPanel1 = new JPanel();
        viewportPanel1.setBackground(Color.red);
        JPanel viewportPanel2 = new JPanel();
        viewportPanel2.setBackground(Color.blue);
        JPanel viewportPanel3 = new JPanel();
        viewportPanel3.setBackground(Color.cyan);
        JPanel viewportPanel4 = new JPanel();
        viewportPanel4.setBackground(Color.yellow);
        JPanel viewportPanel5 = new JPanel();
        viewportPanel5.setBackground(Color.LIGHT_GRAY);
        JPanel viewportPanel6 = new JPanel();
        viewportPanel6.setBackground(Color.magenta);
        viewportPanel.setLayout(new GridLayout(3, 2));
        viewportPanel.add(viewportPanel1);
        viewportPanel.add(viewportPanel2);
        viewportPanel.add(viewportPanel3);
        viewportPanel.add(viewportPanel4);
        viewportPanel.add(viewportPanel5);
        viewportPanel.add(viewportPanel6);

        viewportPanel.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println(e.getPoint());
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setPreferredSize(new Dimension(800, 600));
        scrollPane.setBackground(Color.BLUE);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.getViewport().add(viewportPanel);
        scrollPane.getViewport().setViewPosition(new Point(0, 0));
        scrollPane.getViewport().setBackground(Color.red);
        scrollPane.setBorder(new LineBorder(Color.black, 2));

        JFrame frame = new JFrame();
        frame.setTitle("Viewport JFrame");
        frame.setLocation(150, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(viewportPanelTop, BorderLayout.NORTH);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.add(viewportPanelBottom, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    private MainViewPort() {
    }
}