dom*_*nic 2 java swing jtabbedpane
我在JTabbedPane中有2个JPanel,当在第一个面板内的面板上调用update(g)时(即动画),即使第二个面板是所选面板(即你可以看到的面板),更新的面板也出现在屏幕.为什么这样,我怎么能规避这种行为呢?
"不清除背景" 的update()方法JComponent,因此您可能需要明确地这样做.典型的例子中JTabbedPane通常不需要使用update().也许显示您的使用情况的sscce可能有所帮助.
附录1:目前尚不清楚你打电话的原因update().下面是一个没有异常的简单动画.
附录2:在AWT和Swing中看绘画:paint()与update().您可能需要使用repaint()的actionPerformed()替代.

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);
}
}
}
Run Code Online (Sandbox Code Playgroud)