我是新手,我有以下问题:
我使用openslide-java库来查看整个幻灯片图像。有一个名为OpenSlideView的类可以扩展JPanel,其目的是查看图像。该类具有使用鼠标放大,缩小和导航图像的功能,因为它使用自己的鼠标监听器。我在JFrame中覆盖了两个OpenSlideView对象,其中一个是可见的而另一个是不可见的,我希望能够同时对它们执行鼠标操作,即使其中一个不可见。简单地说,我希望不可见的JPanel跟踪鼠标。
这样的事情可能吗?即使是JPanel类,也不是它的扩展名?
提前致谢。
P.S:我附加了处理OpenSlideView对象鼠标事件的方法代码,以防它有用。
private void registerEventHandlers() {
// mouse wheel
addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent e) {
double ds1 = zoomHelper(OpenSlideView.this, e.getX(), e.getY(),
e.getWheelRotation());
double ds2 = zoomHelper(otherView, e.getX(), e.getY(), e
.getWheelRotation());
zoomHelper2(OpenSlideView.this, ds1, e.getX(), e.getY());
zoomHelper2(otherView, ds2, e.getX(), e.getY());
zoomHelper3(OpenSlideView.this, ds1);
zoomHelper3(otherView, ds2);
repaintHelper(OpenSlideView.this);
repaintHelper(otherView);
}
});
// mouse drag
MouseAdapter ma = new MouseAdapter() {
private SelectionMode selectionMode;
private int oldX;
private int oldY;
private int slideStartX;
private int slideStartY;
private Path2D.Double freehandPath;
@Override
public void mousePressed(MouseEvent e) {
// System.out.println(e);
requestFocusInWindow();
if (!SwingUtilities.isLeftMouseButton(e)) {
return;
}
final int ellipseMask = MouseEvent.CTRL_DOWN_MASK
| MouseEvent.SHIFT_DOWN_MASK;
final int freehandMask = MouseEvent.CTRL_DOWN_MASK;
final int rectMask = MouseEvent.SHIFT_DOWN_MASK;
if ((e.getModifiersEx() & ellipseMask) == ellipseMask) {
selectionMode = SelectionMode.ELLIPSE;
} else if ((e.getModifiersEx() & freehandMask) == freehandMask) {
selectionMode = SelectionMode.FREEHAND;
} else if ((e.getModifiersEx() & rectMask) == rectMask) {
selectionMode = SelectionMode.RECT;
} else {
selectionMode = SelectionMode.NONE;
}
oldX = e.getX();
oldY = e.getY();
double ds = getDownsample();
slideStartX = (int) ((oldX + viewPosition.x) * ds);
slideStartY = (int) ((oldY + viewPosition.y) * ds);
}
@Override
public void mouseDragged(MouseEvent e) {
if (!SwingUtilities.isLeftMouseButton(e)) {
return;
}
int relX = oldX - e.getX();
int relY = oldY - e.getY();
double ds = getDownsample();
int dx = slideStartX;
int dy = slideStartY;
int dw = (int) ((e.getX() + viewPosition.x) * ds) - dx;
int dh = (int) ((e.getY() + viewPosition.y) * ds) - dy;
if (dw < 0) {
dx += dw;
dw = -dw;
}
if (dh < 0) {
dy += dh;
dh = -dh;
}
switch (selectionMode) {
case NONE:
translateHelper(OpenSlideView.this, relX, relY);
translateHelper(otherView, relX, relY);
repaintHelper(OpenSlideView.this);
repaintHelper(otherView);
break;
case RECT:
selectionBeingDrawn = new Rectangle(dx, dy, dw, dh);
// System.out.println(selection);
repaint();
break;
case FREEHAND:
if (selectionBeingDrawn == null) {
// new selection
freehandPath = new Path2D.Double();
selectionBeingDrawn = freehandPath;
freehandPath.moveTo(slideStartX, slideStartY);
}
freehandPath.lineTo((e.getX() + viewPosition.x) * ds, (e
.getY() + viewPosition.y)
* ds);
repaint();
break;
case ELLIPSE:
selectionBeingDrawn = new Ellipse2D.Double(dx, dy, dw, dh);
repaint();
break;
}
oldX = e.getX();
oldY = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
if (selectionMode == SelectionMode.FREEHAND) {
freehandPath.closePath();
}
selectionMode = SelectionMode.NONE;
if (selectionBeingDrawn != null) {
Rectangle bb = selectionBeingDrawn.getBounds();
if (bb.height != 0 && bb.width != 0) {
selections.add(new DefaultAnnotation(selectionBeingDrawn));
selectionBeingDrawn = null;
}
}
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
selectionsVisibleHelper(OpenSlideView.this, true);
selectionsVisibleHelper(otherView, true);
}
@Override
public void mouseExited(MouseEvent e) {
selectionsVisibleHelper(OpenSlideView.this, false);
selectionsVisibleHelper(otherView, false);
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
// keyboard
InputMap inputMap = new InputMap();
ActionMap actionMap = new ActionMap();
inputMap.put(KeyStroke.getKeyStroke("SPACE"), "center");
actionMap.put("center", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
centerHelper(OpenSlideView.this);
centerHelper(otherView);
repaintHelper(OpenSlideView.this);
repaintHelper(otherView);
}
});
inputMap.put(KeyStroke.getKeyStroke("UP"), "scroll up");
inputMap.put(KeyStroke.getKeyStroke("W"), "scroll up");
actionMap.put("scroll up", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
translateHelper(OpenSlideView.this, 0, -KEYBOARD_SCROLL_AMOUNT);
translateHelper(otherView, 0, -KEYBOARD_SCROLL_AMOUNT);
repaintHelper(OpenSlideView.this);
repaintHelper(otherView);
}
});
inputMap.put(KeyStroke.getKeyStroke("DOWN"), "scroll down");
inputMap.put(KeyStroke.getKeyStroke("S"), "scroll down");
actionMap.put("scroll down", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
translateHelper(OpenSlideView.this, 0, KEYBOARD_SCROLL_AMOUNT);
translateHelper(otherView, 0, KEYBOARD_SCROLL_AMOUNT);
repaintHelper(OpenSlideView.this);
repaintHelper(otherView);
}
});
inputMap.put(KeyStroke.getKeyStroke("LEFT"), "scroll left");
inputMap.put(KeyStroke.getKeyStroke("A"), "scroll left");
actionMap.put("scroll left", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
translateHelper(OpenSlideView.this, -KEYBOARD_SCROLL_AMOUNT, 0);
translateHelper(otherView, -KEYBOARD_SCROLL_AMOUNT, 0);
repaintHelper(OpenSlideView.this);
repaintHelper(otherView);
}
});
inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "scroll right");
inputMap.put(KeyStroke.getKeyStroke("D"), "scroll right");
actionMap.put("scroll right", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
translateHelper(OpenSlideView.this, KEYBOARD_SCROLL_AMOUNT, 0);
translateHelper(otherView, KEYBOARD_SCROLL_AMOUNT, 0);
repaintHelper(OpenSlideView.this);
repaintHelper(otherView);
}
});
inputMap.put(KeyStroke.getKeyStroke("L"), "rotate left");
actionMap.put("rotate left", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
}
});
inputMap.put(KeyStroke.getKeyStroke("R"), "rotate right");
actionMap.put("rotate right", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
}
});
inputMap.put(KeyStroke.getKeyStroke("PLUS"), "zoom in");
inputMap.put(KeyStroke.getKeyStroke("EQUALS"), "zoom in");
actionMap.put("zoom in", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
double d1 = zoomHelper(OpenSlideView.this, -1);
double d2 = zoomHelper(otherView, -1);
zoomHelper2(OpenSlideView.this, d1);
zoomHelper2(otherView, d2);
zoomHelper3(OpenSlideView.this, d1);
zoomHelper3(otherView, d2);
repaintHelper(OpenSlideView.this);
repaintHelper(otherView);
}
});
inputMap.put(KeyStroke.getKeyStroke("MINUS"), "zoom out");
actionMap.put("zoom out", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
double d1 = zoomHelper(OpenSlideView.this, 1);
double d2 = zoomHelper(otherView, 1);
zoomHelper2(OpenSlideView.this, d1);
zoomHelper2(otherView, d2);
zoomHelper3(OpenSlideView.this, d1);
zoomHelper3(otherView, d2);
repaintHelper(OpenSlideView.this);
repaintHelper(otherView);
}
});
inputMap.put(KeyStroke.getKeyStroke("Z"), "zoom to fit");
actionMap.put("zoom to fit", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
// System.out.println("zoom");
zoomToFit();
centerSlidePrivate();
paintBackingStore();
repaint();
}
});
inputMap.put(KeyStroke.getKeyStroke("1"), "zoom to 1:1");
actionMap.put("zoom to 1:1", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
// System.out.println("zoom 1:1");
double d1 = zoomHelper(OpenSlideView.this, Integer.MIN_VALUE);
double d2 = zoomHelper(otherView, Integer.MIN_VALUE);
zoomHelper2(OpenSlideView.this, d1);
zoomHelper2(otherView, d2);
zoomHelper3(OpenSlideView.this, d1);
zoomHelper3(otherView, d2);
repaintHelper(OpenSlideView.this);
repaintHelper(otherView);
}
});
inputMap.put(KeyStroke.getKeyStroke("BACK_QUOTE"), "toggle pins");
actionMap.put("toggle pins", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
selectionsAsPins = !selectionsAsPins;
if (otherView != null) {
otherView.selectionsAsPins = !otherView.selectionsAsPins;
}
repaintHelper(OpenSlideView.this);
repaintHelper(otherView);
}
});
// install as parents
InputMap oldInputMap = getInputMap();
ActionMap oldActionMap = getActionMap();
inputMap.setParent(oldInputMap.getParent());
oldInputMap.setParent(inputMap);
actionMap.setParent(oldActionMap.getParent());
oldActionMap.setParent(actionMap);
}
答案 0 :(得分:1)
我希望能够同时对它们进行鼠标操作
没有任何意义。如果组件不可见,您为什么要这样做。也许你需要让你的组件保持其当前状态。因此,当您更改顶部组件的状态时,您将更新隐藏组件的状态。如果隐藏的组件变得可见,它将自动以当前状态绘制。
简单地说,我希望不可见的JPanel跟踪鼠标。
仅将事件分派给单个组件。
但是,您可以使用Component.dispatchEvent(...)
方法将新事件分派给单独的组件。
答案 1 :(得分:0)
是的,你可以这样做,但你需要一个不同的设计。
首先,请注意the API docs's characterization of MouseEvent
s:
指示组件中发生鼠标操作的事件。 如果和,则认为鼠标操作发生在特定组件中 只有当鼠标光标位于未经遮挡的部分时 动作发生时组件的界限。对于轻量级组件, 比如Swing的组件,只发送鼠标事件 组件(如果已在组件上启用鼠标事件类型)。 [...] 如果鼠标事件类型没有 已在组件上启用,相应的鼠标事件是 调度到已启用鼠标事件的第一个祖先 类型。强>
(强调补充。)
因此,当您的某个面板位于另一个面板后面 - 并因此模糊 - 鼠标事件不会出现在模糊部分中。此外,虽然文档没有用很多单词说出来,但你应该从中得到的是MouseEvents
被传递给它们自身发生的组件,或者它接受鼠标的包容树中最近的祖先事件。 Swing不会将事件分派给组件,例如模糊的面板,这些组件不是发生事件的组件的包含祖先。
那么,如何使兄弟姐妹或表兄弟成员对同样的鼠标事件做出回应?至少有两种选择:
在最近的共享祖先容器上注册各种鼠标侦听器,而不是在面板本身上注册。确保面板配置为不自己接收鼠标事件。在事件发生时,让在祖先注册的监听器通知小组。
使用知道所有面板的单个侦听器对象。在每个面板上注册一个对象,并对每个面板采取适当的操作。
你会注意到那些实际上并没有太大的不同。共同的主题是,只有一个监听器将从Swing接收事件,它将指示相应的面板进行所需的响应。