2013-04-03 92 views
0

这里是我的问题:我有一个jpanel,它有三个面板。一个verticalseperator面板,一个chatouput面板和chatinput面板。主要的JPanel,chatpanel,实现的MouseMotionListener,并具有以下代码:jtextarea鼠标事件覆盖容器鼠标事件

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语句中,当鼠标进入聊天输出时,e.getY()的值不会增加,它会保持在verticalseperatorpanel的范围内。它像chatoutput默认的mouselistener正在覆盖chatpanel上的那个,并且什么都不做。有人能告诉我这里发生了什么,以及如何解决这个问题?

回答

1

A MouseListener只会响应其容器的鼠标事件,只要没有其他容器在监视其上的鼠标事件。将鼠标事件想象成雨点。如果你在雨与雨滴之间放一把雨伞,雨滴将不会到达你。

我建议你应该使用mouseEntermouseExitMouseListener接口而不是直接在verticalPanelSeperator

对其进行注册