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



我在JScrollPane中有一个JPanel。JPanel附带了一个mousellistener来响应鼠标事件。当我将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)。知道这个线性偏移是怎么来的吗?

Edit:没关系,我把它简化为一个简单的测试用例,它像预期的那样工作。所以一定是我周围的代码中有什么东西导致了这种行为。

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());
}
}

请问您能根据这个例子重现您的问题吗?

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() {
    }
}

相关内容

  • 没有找到相关文章

最新更新