2013-04-03 91 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

對其進行註冊