jtextarea鼠标事件覆盖容器鼠标事件

时间:2013-04-03 01:15:07

标签: java swing resize mouseevent jtextarea

这是我的问题:我有一个jpanel,里面有三个面板。 verticalseperator面板,chatouput面板和chatinput面板。主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();
    }
}

问题在于,由于某些原因,当您将鼠标移到verticalpanelseperator上时,它会更改为调整大小光标,但是当您向上移动它而不拖动时,它将保持为调整大小光标。此外,从print语句开始,当鼠标进入chatoutput时,e.getY()的值不会增加,它会保持在verticalseperatorpanel的范围内。它就像chatoutput默认的mouselistener覆盖了聊天面板上的那个并且什么都不做。谁能告诉我这里发生了什么以及如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

MouseListener只响应其容器的鼠标事件,只要没有其他容器监视其上方的鼠标事件。将鼠标事件视为雨滴。如果你把雨伞放在你和雨水之间,雨水就不会落到你身上。

我建议您使用mouseEnter界面中的mouseExitMouseListener,而直接在verticalPanelSeperator

上注册
相关问题