简体   繁体   English

JTabbedPane摆动更新错误

[英]JTabbedPane swing update error

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(ie the one you can see) the updated panel appears on the screen. 我在JTabbedPane中有2个JPanels,并且即使在第二个面板是选定面板(即您可以看到的面板)上的第一个面板(它是动画)中的一个面板上调用update(g)时,更新的面板也会显示在屏幕。 Why is this and how can i circumvent this behaviour? 为什么会这样,我该如何规避这种行为?

The update() method of JComponent "doesn't clear the background," so you may need to do that explicitly. JComponentupdate()方法“不会清除背景”,因此您可能需要明确地执行此操作。 Typical examples of JTabbedPane don't usually require using update() . JTabbedPane典型示例通常不需要使用update() Perhaps an sscce showing your usage might help. 显示您的用法的脚本可能会有所帮助。

Addendum 1: It's not clear why you are calling update() . 附录1:不清楚为什么要调用update() Below is a simple animation that does not exhibit the anomaly. 以下是一个没有异常现象的简单动画。

Addendum 2: See Painting in AWT and Swing: paint() vs. update() . 附录2:请参阅AWT中的绘画和Swing:paint()与update() You may want to use repaint() in actionPerformed() instead. 您可能想在actionPerformed()使用repaint()

在此输入图像描述

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

when update(g) is called on a panel inside the first panel (Its an animation) 在第一个面板内的面板上调用update(g)时(其动画)

Overriding the update(...) method is an old AWT trick and should never be used with Swing. 重写update(...)方法是一个古老的AWT技巧,决不能与Swing一起使用。

Read the section from the Swing tutorial on Custom Painting for the proper way to do this. 阅读Swing教程中有关“ 自定义绘画”的部分,以获取正确的方法。 And for animation read up on "How to Use Swing Timer" found in the tutorial as well. 对于动画,还请阅读本教程中的“如何使用Swing计时器”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM