D
DragonPC_???
Unregistered / Unconfirmed
GUEST, unregistred user!
最近在写了一个类似 ICQ 的 Java 程序,上面要求软件具有换肤功能,我只有从JWindow
继承下来一个 CDragWindow作为我所有窗口的子类,因为Frame类有标题栏,Window类没有
,其中涉及窗口移动的代码主要是重载的 mouseDragged 函数
事实上窗口类可以使用鼠标拖动窗口,但是有时候窗口会产生跳跃,有时候非常正常。而
拖动Frame的标题栏移动窗口是很稳定的,研究Frame的源码,对FramePeer同位体感到不可
了解,请问各位高手如何解决我的窗口拖动时的闪烁问题。
继承下来一个 CDragWindow作为我所有窗口的子类,因为Frame类有标题栏,Window类没有
,其中涉及窗口移动的代码主要是重载的 mouseDragged 函数
事实上窗口类可以使用鼠标拖动窗口,但是有时候窗口会产生跳跃,有时候非常正常。而
拖动Frame的标题栏移动窗口是很稳定的,研究Frame的源码,对FramePeer同位体感到不可
了解,请问各位高手如何解决我的窗口拖动时的闪烁问题。
代码:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.* ;
import javax.swing.*;
public class CDragWindow extends JWindow implements MouseMotionListener, MouseListener, ActionListener
{
private int x = 0 ;
private int y = 0 ;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// System Screen Size , use to Locate window position
private boolean drag = false ;
private boolean isDrag = true ;
private boolean automaticLocate = false ;
public CDragWindow()
{
addMouseMotionListener(this) ;
addMouseListener(this) ;
}
public boolean getDragable()
{
return isDrag ;
}
public void setDragable(boolean yesOrNo)
{
isDrag = yesOrNo ;
}
public boolean getAntoLocate()
{
return automaticLocate ;
}
public void setAntoLocate(boolean bLocate)
{
automaticLocate = bLocate ;
} // this value use to override "setLocation()" function
public void mouseReleased(MouseEvent e)
{
drag = false ;
}
public void mousePressed(MouseEvent e)
{
if (!isDrag) return ;
drag = true ;
x = e.getX() ;
y = e.getY() ;
}
public void mouseDragged(MouseEvent e)
{
if (!isDrag) return ;
if (drag)
{
int w_x = this.getLocation().x ;
int w_y = this.getLocation().y ;
int m_x = e.getX() ;
int m_y = e.getY() ;
if ((m_x != x) || (m_y != y))
{
this.setLocation(w_x + m_x - x, w_y + m_y - y) ;
}
}
}
public void setLocation(int x, int y)
{
if (screenSize != null &&
automaticLocate)
{
// if automaticLocate is true, then
window cann't setLocation out of system screen
int w = this.getWidth() ;
int h = this.getHeight() ;
if (x<0) x = 0 ;
if (y<0) y = 0 ;
if ((x+w) > this.screenSize.width) x = this.screenSize.width - w ;
if ((y+h) > this.screenSize.height) y = this.screenSize.height - h ;
}
super.setLocation(x, y) ;
}
public void update(Graphics g)
{
paint(g) ;
}
public void mouseMoved(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited (MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void actionPerformed(ActionEvent e) { }
}