Salve a tutti! Ho scritto questo mouse motion Listener all'interno di una classe che estende JDialog per poter creare una dialog non decorata in cui posso inserire delle informazini! Ho aggiunto questo listener per far si che questa jdialog si possa ridimensionare se il mouse e' posizionato nell'angolo in basso a destra e spostarla se il mouse si trova nella parte superiore!
Usando questo codice pero' ho dei comportamenti un po' anomali cioe' per esempio l'icoda del mouse non viene refreshata in maniera corretta oppure s provo a cliccare sui lati della finestra questa cambia posizione! Qualcuno di voi potrebbe darmi una dritta su come aggiustarla ed aiutarmi a capire dove sta' l'errore?? Grazie mille!!
codice:
addMouseMotionListener(new MouseMotionListener()
{
private boolean isResizeEnabled = false;
private int initialX = 0;
private int initialY = 0;
/**
* Invoked when a mouse button is pressed on a component and then
* dragged.
*
* @param e
* The mouse event
*/
public void mouseDragged(MouseEvent e)
{
if( isResizeEnabled)
{
setSize(new Dimension(e.getPoint().x, e.getPoint().y));
}
else
{
setLocation(e.getLocationOnScreen().x - initialX, e.getLocationOnScreen().y - initialY);
}
}
/**
* Invoked when the mouse cursor has been moved onto a component but
* no buttons have been pushed.
*
* @param e
* The mouse event
*/
public void mouseMoved(MouseEvent e)
{
if( e.getPoint().y < 9 && (e.getPoint().x > 0 || e.getPoint().x < this.)
{
initialX = e.getPoint().x;
initialY = e.getPoint().y;
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
isResizeEnabled = false;
}
else if( (e.getPoint().x > (getWidth() - 10)) && (e.getPoint().y > (getHeight() - 10)) )
{
setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
isResizeEnabled = true;
}
else
{
validate();
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
isResizeEnabled = false;
}
}
});