简体   繁体   English

不断变化的JLabel背景颜色

[英]Constantly changing background color of JLabel

I'm new to GUI programming and I tried out some code and wanted to constantly change the background of a JPanel called "HeaderPanel". 我是GUI编程的新手,我尝试了一些代码,并希望不断更改名为“HeaderPanel”的JPanel的背景。

Why isnt this working as I wished? 为什么这不是我希望的? (Color stays the same...) (颜色保持不变......)

private void changeColors() {
    int r = 0;
    int g = 155;
    int b = 12;

    while(true) {
        r = (r+1)%255;
        g = (g+1)%255;
        b = (b+1)%255;

        Color color = new Color(r,g,b);
        HeaderPanel.setBackground(color);
    }
}

Your while loop is stopping the repaint manager from ever getting around to changing the colors. 你的while循环正在阻止重绘管理器不断改变颜色。

You need to, some how, execute the request in the background, something like this 您需要,某些方法,在后台执行请求,类似这样

public class TestLabel extends JLabel {

    private Timer timer;

    private int r = 0;
    private int g = 155;
    private int b = 12;

    public TestLabel() {

        setText("Hello world");
        setOpaque(true);

        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                System.out.println("tick");

                r = (r + 1) % 255;
                g = (g + 1) % 255;
                b = (b + 1) % 255;

                Color color = new Color(r, g, b);
                setBackground(color);

                System.out.println(color);

                if (r == 0 && b == 0 && g == 0) {

                    r = 0;
                    g = 155;
                    b = 12;

                }

                invalidate();
                revalidate();
                repaint();

            }
        });

        timer.setRepeats(true);
        timer.setCoalesce(true);
        timer.start();

    }
}

You might like to read up on 你可能想读一读

UPDATED with Extended Example 使用扩展示例更新

public class TestLabel extends JLabel {

    private Timer timer;

    private Object[][] colors = {{"Black", Color.BLACK},
        {"Blue", Color.BLUE},
        {"Cyan", Color.CYAN},
        {"Dark Gray", Color.DARK_GRAY},
        {"Gray", Color.GRAY},
        {"Green", Color.GREEN},
        {"Light Gary", Color.LIGHT_GRAY},
        {"Mangenta", Color.MAGENTA},
        {"Orange", Color.ORANGE},
        {"Pink", Color.PINK},
        {"Red", Color.RED},
        {"White", Color.WHITE},
        {"Yellow", Color.YELLOW}};

    public TestLabel() {

        setText("Hello world");
        setOpaque(true);

        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                System.out.println("tick");

                int index = (int) Math.round((colors.length - 1) * Math.random());

                Object[] group = colors[index];

                setBackground((Color)group[1]);
                setText((String)group[0]);

            }
        });

        timer.setInitialDelay(0);
        timer.setRepeats(true);
        timer.setCoalesce(true);
        timer.start();

    }
}

I'm not a swing developer, but don't you have to "repaint" or something like that when you change the color property? 我不是摇摆开发者,但是当你改变颜色属性时,你不需要“重画”或类似的东西吗? In the other hand, you will need another thread to keep updating the colors. 另一方面,您需要另一个线程来继续更新颜色。

Summarizing 总结

  • A code to change the panel background color (include repaint) 用于更改面板背景颜色的代码(包括重绘)
  • A thread to invoke the above code. 一个调用上面代码的线程。

你需要告诉你的JPanel自己绘画,可能是通过调用invalidate()

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

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