0

这是我的问题:我有一个 jpanel,里面有三个面板。垂直分隔面板、聊天输入面板和聊天输入面板。主jpanel,chatpanel,实现mousemotionlistener,代码如下:

 public void mouseMoved(MouseEvent e) {
   int y = e.getY();

   //change cursor look if hovering over vertical spacer
   if(y >= (int)(verticalPanelSeperator.getLocation().getY()) && y <=    (int)(verticalPanelSeperator.getLocation().getY() + verticalPanelSeperator.getHeight())) {
       setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
   }
   else {
       setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR ));
   }
}

public void mouseDragged(MouseEvent e) {
//vertical spacer can be draged up/down to change height of input/output areas
    if(getCursor().getType() == Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR).getType()) {
        int edge = (int)this.getLocation().getY();
        int cur = e.getY();
        int height = this.getHeight();
        output.setHeightPercent((((double)(cur-edge))/height));
        output.componentResized(new ComponentEvent(this, 1));
        input.setHeightPercent((((double)(height-cur-edge))/height));
        input.componentResized(new ComponentEvent(this, 2));
        e.consume();
    }
}

问题是,由于某种原因,当您将鼠标移动到垂直面板分隔符上时,它会变为调整大小的光标,但是当您向上移动它时,无需拖动,它就会保持为调整大小的光标。此外,从打印语句来看,当鼠标向上进入聊天输出时,e.getY() 的值不会增加,它保持在垂直分隔面板的范围内。它就像聊天输出默认鼠标监听器覆盖了聊天面板上的鼠标监听器并且什么都不做。有人可以告诉我这里发生了什么以及如何解决这个问题吗?

4

1 回答 1

1

AMouseListener只会响应其容器的鼠标事件,只要没有其他容器在其上方监视鼠标事件。将鼠标事件想象成雨滴。如果你在你和雨滴之间撑起一把伞,雨滴就不会接触到你。

我建议您应该使用mouseEnterand mouseExitfrom the MouseListenerinterface 而是直接在verticalPanelSeperator

于 2013-04-03T01:25:45.767 回答