考虑我有以下代码:
public LSUIEditingPanel(LSView view) {
super();
parent = view;
controller = view.getController();
LayoutManager overlay = new OverlayLayout(this);
setLayout(overlay);
scribbleArea = new LSScribblePanel();
scribbleArea.setOpaque(false);
add(scribbleArea);
viewArea = new LSCanvasViewport(view);
add(viewArea);
setAutoscrolls(true);
drawListener = new SVGEditorDrawMouseAdaptor();
}
其中 scribbleArea 和 viewArea 都是自定义 JPanel。它们都是添加到主面板的子面板。
现在我想这样做,以便我可以将 MouseEvent 注册到父面板本身,这就是我可以对面板进行平移,因为父面板将在实例化后嵌入到 JScrollPane 中。
这是执行拖动的代码段:
public class LSPanMouseAdapter extends MouseAdapter {
/**
* Panning pivot point
*/
private final Point panPoint = new Point();
/**
* {@inheritDoc}<p>
* Set pan starting point to be the point where the mouse currently located, and change the
* current cursor to <code>HAND_CURSOR</code>.
*/
@Override
public void mousePressed(MouseEvent event) {
JViewport viewport = (JViewport) event.getSource();
JComponent component = (JComponent) viewport.getView();
panPoint.setLocation(event.getPoint());
component.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
/**
* {@inheritDoc}<p>
* Reset cursor to <code>DEFAULT_CURSOR</code>.
*/
@Override
public void mouseReleased(MouseEvent event) {
JViewport viewport = (JViewport) event.getSource();
JComponent component = (JComponent) viewport.getView();
component.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
/**
* {@inheritDoc}<p>
* Get the current viewport of the canvas and move the viewable area respective to the
* event cursor location.
*/
@Override
public void mouseDragged(MouseEvent event) {
JViewport viewport = (JViewport) event.getSource();
JComponent component = (JComponent) viewport.getView();
Point currentPoint = event.getPoint();
Point viewPoint = viewport.getViewPosition();
viewPoint.translate(panPoint.x - currentPoint.x, panPoint.y - currentPoint.y);
component.scrollRectToVisible(new Rectangle(viewPoint, viewport.getSize()));
panPoint.setLocation(currentPoint);
}
}
我已尝试注册该事件,但父面板没有触发该事件,关于我应该如何执行此操作的任何建议?