JTabbedPane摆动更新错误 [英] JTabbedPane swing update error

查看:40
本文介绍了JTabbedPane摆动更新错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JTabbedPane中有2个JPanels,并且即使在第二个面板是选定面板(即您可以看到的面板)上,在第一个面板(其动画)中的一个面板上调用update(g)时,更新的面板出现在屏幕上.为什么会这样,我该如何规避这种行为?

I have 2 JPanels in a JTabbedPane and when update(g) is called on a panel inside the first panel (Its an animation) even if the second panel is the selected panel(i.e the one you can see) the updated panel appears on the screen. Why is this and how can i circumvent this behaviour?

推荐答案

update() .也许 sscce 显示您的用法可能会有所帮助.

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.

附录1:不清楚为什么要调用 update().下面是一个没有异常现象的简单动画.

Addendum 1: It's not clear why you are calling update(). Below is a simple animation that does not exhibit the anomaly.

附录2:请参见 AWT和摇摆:paint()与update() .您可能想在 actionPerformed()中使用 repaint().

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);
        }
    }
}

这篇关于JTabbedPane摆动更新错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆