【发布时间】:2015-05-29 21:33:21
【问题描述】:
在我将 JFrame GlassPane 设置为可见之前,我的 JProgressBar 运行良好。然后它随机决定在屏幕上拍摄并复制。
这是一个显示错误的动画 GIF。无论我的代码是什么,都会发生这种情况——即使是非常基本的 GUI
http://i.gyazo.com/bd70df610a3cad6bfff258b80f21c78a.gif
只有当我这样做时才会发生这种情况 this.getGlassPane().setVisible(true);
代码如下:
public void setProgress(final int percent) {
pb.setValue(percent);
if (percent == 100) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FadingMirror glassPane = new FadingMirror();
instance.setGlassPane(glassPane);
((FadingMirror)instance.getGlassPane()).startAnimation();
((FadingMirror)instance.getGlassPane()).setVisible(true);
}
});
}
}
@Override
public void addText(String text) {
System.out.println(text);
}
public static class FadingMirror extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 7341931028635559650L;
private float opacity = 0f;
private Timer fadeTimer;
public void startAnimation() {
fadeTimer = new javax.swing.Timer(75, this);
fadeTimer.setInitialDelay(0);
fadeTimer.start();
}
public void actionPerformed(ActionEvent e) {
opacity += .03;
if(opacity > 1) {
opacity = 1;
fadeTimer.stop();
fadeTimer = null;
}
repaint();
}
public void paintComponent(Graphics g) {
//super.paintComponent(g); //Cannot do this before setting composite without destroying the animation
((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight());
}
}
}
感谢阅读
【问题讨论】:
-
您正在为
glassPane使用 cusomt 组件,它覆盖了paint或paintComponent方法,但不首先将其称为超级方法 -
MadProgrammer - 看起来你是完全正确的。唯一的问题是我要覆盖它,因为如果我调用 super 它会破坏我的淡入淡出动画
-
考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个示例,它突出了您遇到的问题。这将导致更少的混乱和更好的响应
-
"这里是代码:" 不可编译的代码 sn-p 不是 MCVE 或可运行的示例,因为在 cmets 中已经为您链接了两次。尝试点击链接并阅读文档..
标签: java swing jprogressbar