当前位置: 技术问答>java相关
窗体拖动问题!!
来源: 互联网 发布时间:2015-01-19
本文导语: 我用java编了一个应用程序.我想通过鼠标拖动该程序窗体(JFrame)到处移动. 请问该如何实现? 我试着用MousePressed,MouseDragged等事件,发现行不通. 请各位高手赐教!! 谢谢!! | class Test extends JFrame im...
我用java编了一个应用程序.我想通过鼠标拖动该程序窗体(JFrame)到处移动.
请问该如何实现?
我试着用MousePressed,MouseDragged等事件,发现行不通.
请各位高手赐教!! 谢谢!!
请问该如何实现?
我试着用MousePressed,MouseDragged等事件,发现行不通.
请各位高手赐教!! 谢谢!!
|
class Test extends JFrame implements MouseListener,MouseMotionListener
{
Point preMousePos ;
public Test()
{
super();
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
}
public void mouseEntered(MouseEvent me)
{}
public void mouseExited(MouseEvent me)
{}
public void mousePressed(MouseEvent me)
{
if(me.getModifiers()==me.BUTTON1_MASK)
preMousePos = me.getPoint();
}
public void mouseReleased(MouseEvent me)
{}
public void mouseDragged(MouseEvent me)
{
//move this window
Point mousenowpos = me.getPoint();
Point prewndpos=jfThis.getLocationOnScreen();
prewndpos.x = prewndpos.x+mousenowpos.x-preMousePos.x;
prewndpos.y = prewndpos.y+mousenowpos.y-preMousePos.y;
jfThis.setLocation(prewndpos);
}
public void mouseMoved(MouseEvent me)
{}
}//end of class Test
{
Point preMousePos ;
public Test()
{
super();
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
}
public void mouseEntered(MouseEvent me)
{}
public void mouseExited(MouseEvent me)
{}
public void mousePressed(MouseEvent me)
{
if(me.getModifiers()==me.BUTTON1_MASK)
preMousePos = me.getPoint();
}
public void mouseReleased(MouseEvent me)
{}
public void mouseDragged(MouseEvent me)
{
//move this window
Point mousenowpos = me.getPoint();
Point prewndpos=jfThis.getLocationOnScreen();
prewndpos.x = prewndpos.x+mousenowpos.x-preMousePos.x;
prewndpos.y = prewndpos.y+mousenowpos.y-preMousePos.y;
jfThis.setLocation(prewndpos);
}
public void mouseMoved(MouseEvent me)
{}
}//end of class Test