我从这里修改了一些代码:https ://stackoverflow.com/a/13357269/1360074
由于 JScrollPane 存在已知错误,没有将 MouseEvent 向上传递,因此我使用 FakeMouseListener 进行了解决。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class LostMouseEvent {
private JPanel panel1;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new LostMouseEvent();
}
});
}
public LostMouseEvent() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
panel1 = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
};
JPanel panel2 = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 300);
}
};
JScrollPane pane = new JScrollPane(panel2);
panel1.setBorder(BorderFactory.createLineBorder(Color.blue));
panel2.setBorder(BorderFactory.createLineBorder(Color.green));
panel1.setLayout(new CircleLayout());
panel1.add(pane);
frame.add(panel1);
MouseListener rml = new RealMouseListener();
panel1.addMouseListener(rml);
MouseListener fml = new FakeMouseListener();
pane.addMouseListener(fml);
frame.pack();
frame.setVisible(true);
}
});
}
private class RealMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent me) {
System.out.println(me);
Point point = me.getPoint();
System.out.println(panel1.getComponentAt(point));
System.out.println(panel1.getComponent(0));
}
}
private class FakeMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent me) {
panel1.dispatchEvent(me);
}
}
}
现在,如果单击边框旁边左侧的绿色边框内,我会得到:
java.awt.event.MouseEvent[MOUSE_PRESSED,(9,169),absolute(66,248),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on javax.swing.JScrollPane[,49
LostMouseEvent$2$1[,0,0,600x400,layout=CircleLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@633d51
javax.swing.JScrollPane[,49,49,503x303,layout=javax.swing.ScrollPaneLayout$UIResource,alignmentX=0.0,alignmentY=0.0,
如果我点击中间,我会得到:
java.awt.event.MouseEvent[MOUSE_PRESSED,(247,147),absolute(304,226),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on javax.swing.JScrollPane[,
javax.swing.JScrollPane[,49,49,503x303,layout=javax.swing.ScrollPaneLayout$UIResource,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.metal.
javax.swing.JScrollPane[,49,49,503x303,layout=javax.swing.ScrollPaneLayout$UIResource,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.metal.
这是为什么?重新派送时是否需要修改 MouseEvent 的坐标?
CircleLayout 的代码是
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
public class CircleLayout implements LayoutManager {
private int heightGap;
public CircleLayout () {
heightGap = 0;
}
public CircleLayout (int heightGap) {
this.heightGap = heightGap;
}
/**
* Arranges the parent's Component objects in either an Ellipse or a Circle.
* Ellipse is not yet implemented.
*/
public void layoutContainer (Container parent) {
int x, y, w, h, s, c;
int childCompNum = parent.getComponentCount();
int parentWidth = (int)parent.getSize().width;
int parentHeight = (int)parent.getSize().height;
int centerX = (int) (parentWidth / 2);
int centerY = (int) (parentHeight / 2);
double angleOffset = 0.5 * Math.PI;
Component childComp = null;
for (int i = 0; i < childCompNum; i++) {
childComp = parent.getComponent(i);
w = childComp.getPreferredSize().width;
h = childComp.getPreferredSize().height;
if (childCompNum == 1) {
x = centerX - (int)w / 2;
y = centerY - (int)h / 2;
} else {
c = (int) (centerX * Math.cos((2 * i * Math.PI + angleOffset) / childCompNum));
s = (int) (centerY * Math.sin((2 * i * Math.PI + angleOffset) / childCompNum));
x = c + centerX - (int)w / 2;
y = s + centerY - (int)h / 2;
if (x + w > parentWidth) {x = (int)(parentWidth - w); }
if (y + h + heightGap > parentHeight) {y = (int)(parentHeight - h -heightGap); }
if (x < 0) {x = 0; }
if (y < 0) {y = 0; }
}
childComp.setBounds(x, y, w, h);
}
}
/** For compatibility with LayoutManager interface */
public void addLayoutComponent (String name, Component comp) {}
public Dimension preferredLayoutSize(Container target) {
return target.getSize();
}
public Dimension minimumLayoutSize(Container target) {
return target.getSize();
}
public void removeLayoutComponent(Component comp) {}
}
UPD:我已将 FakeMouseListener 修改为
public void mousePressed(MouseEvent me) {
JScrollPane pane = (JScrollPane)me.getSource();
MouseEvent newMe = SwingUtilities.convertMouseEvent(pane, me, pane.getViewport());
panel1.dispatchEvent(me);
}
但有相同的行为。