我正在使用 GlassPane 移动框架中的元素。当没有元素被拖动时,所有鼠标事件都以正常方式调度
public class MainFrame implements MouseListener {
private JFrame frame;
public MainFrame()
{
frame = new JFrame();
JPanel testPanel = new JPanel();
testPanel.addMouseListener(this);
JLabel testLabel = new JLabel("Test",JLabel.CENTER);
testPanel.add(testLabel);
frame.add(testPanel);
frame.pack();
frame.setVisible(true);
GlassPanel glassPanel = new GlassPanel(frame);
frame.setGlassPane(glassPanel);
// Without this line the click in the panel is mentioned.
// otherwise it is ignored.
frame.getGlassPane().setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e)
{
System.out.print("CLicked");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public class GlassPanel extends JPanel implements MouseMotionListener, MouseListener
{
private JFrame frame;
public GlassPanel(JFrame mainframe)
{
frame = mainframe;
setLayout(null);
setOpaque(false);
addMouseMotionListener(this);
addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent e)
{
redispatchMouseEvent(e, true);
}
@Override
public void mousePressed(MouseEvent e)
{
redispatchMouseEvent(e, true);
}
@Override
public void mouseReleased(MouseEvent e)
{
redispatchMouseEvent(e, true);
}
@Override
public void mouseEntered(MouseEvent e)
{
redispatchMouseEvent(e, true);
}
@Override
public void mouseExited(MouseEvent e)
{
redispatchMouseEvent(e, true);
}
@Override
public void mouseDragged(MouseEvent e)
{
redispatchMouseEvent(e, true);
}
@Override
public void mouseMoved(MouseEvent e)
{
redispatchMouseEvent(e, true);
}
private void redispatchMouseEvent(MouseEvent e, boolean repaint)
{
System.out.println("Dispatch");
Point glassPanePoint = e.getPoint();
Container container = frame.getContentPane();
Point containerPoint = SwingUtilities.convertPoint(frame.getGlassPane(),
glassPanePoint, container);
if (containerPoint.y < 0) { // we're not in the content pane
// Could have special code to handle mouse events over
// the menu bar or non-system window decorations, such as
// the ones provided by the Java look and feel.
} else {
// The mouse event is probably over the content pane.
// Find out exactly which component it's over.
Component component = SwingUtilities.getDeepestComponentAt(
container, containerPoint.x, containerPoint.y);
if (component != null) {
// Forward events to component below
Point componentPoint = SwingUtilities.convertPoint(
frame.getGlassPane(), glassPanePoint, component);
component.dispatchEvent(new MouseEvent(component, e
.getID(), e.getWhen(), e.getModifiers(),
componentPoint.x, componentPoint.y, e
.getClickCount(), e.isPopupTrigger()));
}
}
}
}
}
但这会导致鼠标事件处理中的异常行为。例如:我有一个带有鼠标侦听器的 JPanel。双击此面板会打开另一个框架。在这个面板上有一些标签。当没有 Glasspane 时,我可以单击此标签,并且由于标签不会作用于鼠标事件,因此将事件传递到面板并完成单击操作。当我使用我的分派例程时,我将鼠标事件直接分派给标签,这当然不会导致任何操作。
那么我该如何解决这个问题呢?当然,我可以创建自己的标签类并引用它的 Panel 并将鼠标事件再次发送到面板,但这看起来不是一个好的设计。第二个虽然是,我可以在 Glasspane 调度程序中确定,如果最深的面板对鼠标事件感兴趣,如果没有,加紧更高或类似的东西?
有任何想法吗?