The update()
method of JComponent
"doesn't clear the background," so you may need to do that explicitly. Typical examples of JTabbedPane
don't usually require using update()
. Perhaps an sscce showing your usage might help.
Addendum 1: It's not clear why you are calling update()
. Below is a simple animation that does not exhibit the anomaly.
Addendum 2: See Painting in AWT and Swing: paint() vs. update(). You may want to use repaint()
in actionPerformed()
instead.
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class JTabbedTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
//@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane jtp = new JTabbedPane();
jtp.setPreferredSize(new Dimension(320, 200));
jtp.addTab("Reds", new ColorPanel(Color.RED));
jtp.addTab("Greens", new ColorPanel(Color.GREEN));
jtp.addTab("Blues", new ColorPanel(Color.BLUE));
f.add(jtp, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
private static class ColorPanel extends JPanel implements ActionListener {
private final Random rnd = new Random();
private final Timer timer = new Timer(1000, this);
private Color color;
private int mask;
private JLabel label = new JLabel("Stackoverflow!");
public ColorPanel(Color color) {
super(true);
this.color = color;
this.mask = color.getRGB();
this.setBackground(color);
label.setForeground(color);
this.add(label);
timer.start();
}
//@Override
public void actionPerformed(ActionEvent e) {
color = new Color(rnd.nextInt() & mask);
this.setBackground(color);
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…