我写了一个Swing程序是,有一个Jwindow会从角落滑出提示框。
但是在滑动的过程中,他的父Jframe会由于反复的失焦点和得到焦点而出现闪动的样子,
如果这个Jframe失去焦点则不会闪动,但是Jwindow的到焦点的时候Jframe也同时的到焦点。
由于Jwindow的到焦点就会移动所以,无法避免Jframe的闪烁问题源代码:
public class NotifyInTime extends JFrame { /**
 * @param args
 */
private JFrame main=this;
private JWindow smallwinsow; private static NotifyInTime instance = null; public static void main(String[] args) { NotifyInTime cc = NotifyInTime.getInstance();
cc.pack();
cc.setVisible(true);
} private NotifyInTime() {
super("asd" );

// setSize(500, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Rectangle desktopBounds = Config.env.getMaximumWindowBounds();
smallwinsow = new JWindow();
ImageIcon imc=new ImageIcon(Config.res
.getString("notify.logo"));
JLabel jb=new JLabel(imc);

jb.addMouseListener(new MouseListener(){ public void mouseClicked(MouseEvent e) {
main.pack();
main.setVisible(true);
smallwinsow.setVisible(false);

}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}}); smallwinsow.getContentPane().add(jb);
smallwinsow.pack();
smallwinsow.setLocation(desktopBounds.width - smallwinsow.getSize().width-10,smallwinsow.getSize().height);
// System.out.println("finally"+window.getHeight());
smallwinsow.setVisible(true);
}
});

} public static NotifyInTime getInstance() { if (instance == null) {
instance = new NotifyInTime();
}
return instance;
}
}
//-----------------------------------------------------------------------------------------------public class SlideInNotification extends Object { JWindow window; JComponent contents;
Rectangle desktopBounds; Dimension tempWindowSize; Timer animationTimer; int showX, startY; long animationStart; int hight = 0; public SlideInNotification() {
initDesktopBounds();
} public SlideInNotification(JComponent contents) {
this();
setContents(contents);
} protected void initDesktopBounds() { // 返回居中 Windows 的最大边界
desktopBounds = Config.env.getMaximumWindowBounds();

//  System.out.println("max window bounds = " + desktopBounds);
} public void setContents(JComponent contents) {
this.contents = contents; window = new JWindow();
window.getContentPane().add(contents);
window.pack();
tempWindowSize = window.getSize(); window.getContentPane().add(contents); } public void showAt() {
// create a window with an animating sheet
// copy over its contents from the temp window
// animate it
// when done, remove animating sheet and add real contents showX = desktopBounds.width;
startY = desktopBounds.y + desktopBounds.height; ActionListener animationLogic = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// System.out.println(tempWindowSize.getHeight()+">=
// "+window.getSize().height); if (tempWindowSize.getHeight() <= hight) {
// put real contents in window and show
window.getContentPane().removeAll();
window.getContentPane().add(contents);
window.pack();
window.setLocation(showX - window.getSize().width, startY
- window.getSize().height);
// System.out.println("finally"+window.getHeight());
window.setVisible(true);
window.repaint(); animationTimer.stop();
animationTimer = null;

animationTimer=new Timer(10*1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(animationTimer!=null)
{
animationTimer.stop();
animationTimer = null;
dissolve();
}
}
});
animationTimer.start();
} else {
hight += Config.ANIMATION_STEPHIGHT; if (hight > tempWindowSize.getHeight())
hight = (int) tempWindowSize.getHeight(); window.pack();
window.setLocation(showX - window.getWidth(), startY
- hight);
// System.out.println(window.getHeight());
window.setVisible(true);
window.repaint();
}
}
};
animationTimer = new Timer(Config.ANIMATION_DELAY, animationLogic); animationTimer.start();
} // AnimatingSheet inner class listed below
public void dissolve() {
hight+=(java.awt.Toolkit.getDefaultToolkit().getScreenSize().height-startY); ActionListener animationLogic = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// System.out.println(tempWindowSize.getHeight()+">=
// "+window.getSize().height); if (hight <= 0) {
animationTimer.stop();
animationTimer = null;
//  System.out.println("<=0"+hight);
// window.getContentPane().removeAll();
window.setVisible(false);
window=null;
} else {
hight -= Config.ANIMATION_STEPHIGHT;
if (hight < 0)
hight = 0;
window.pack();
window.setLocation(showX - window.getWidth(), java.awt.Toolkit.getDefaultToolkit().getScreenSize().height
- hight);
//  System.out.println(startY
// - hight);
window.setVisible(true);
window.repaint();
}
}
};
animationTimer = new Timer(Config.ANIMATION_DELAY, animationLogic); animationTimer.start();
}
}
如何才能不让他看起来闪烁呢?