更改JButtons背景的最佳方法 [英] Best way of Changing the background of JButtons

查看:89
本文介绍了更改JButtons背景的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我通过使用

button.setBackground(Color.WHITE);

那是一个例子.

但是当我在jbuttons(1000+)中有一个巨大的网格时,仅运行for循环来更改每个按钮的背景就非常非常慢.您可以看到网格逐渐变成白色,一格一格.我真的不想要这个

But when i have a massive grid out of jbuttons (1000+), just running a for loop to change every buttons background is very, very slow. You can see the grid slowly turning white, box by box. I really don't want this

是否有更好的方法可以同时将网格上的每个JButton更改为相同的颜色?

Is there a better way of changing every JButton on the grid to the same color at the same time?

这是我制作网格的方式,使用的数字仅是示例...

This is how i am making the grid, the numbers used are only for example...

grid = new JPanel(new GridLayout(64, 64, 0, 0));

这是4096个按钮,大约需要30秒钟以上才能将每个按钮更改为相同的颜色.

That's 4096 buttons, takes about 30+ seconds to change every button to the same color.

我需要按钮是可单击的,例如当我单击按钮时,它会变成蓝色.单击所有按钮后,将每个按钮的颜色更改为白色.现在我可以正常工作,但是更改每个按钮的颜色只是很慢.

Edit 1: I need the buttons to be clickable, like when i click a button it turns blue for example. when all of the buttons are clicked, change the color of every button to white. Right now i have that working fine, but it is just slow to change the color of every button.

这就是我更改按钮的方式:

Edit 2: this is how i am changing the buttons:

    new javax.swing.Timer(300, new ActionListener() {
        int counter = 0;
        public void actionPerformed(ActionEvent e) {
            if (counter >= counterMax) {
                ((Timer) e.getSource()).stop();
            }
            Color bckgrndColor = (counter % 2 == 0) ? flashColor : Color.white;
            for (JButton button : gridButton) {
                button.setBackground(bckgrndColor);
            }
            counter++;
        }
    }).start();

推荐答案

您看到的框分别被重涂的事实表明关闭了双缓冲,或者在按钮UI使用paintImmediately().

The fact that you see the boxes being repainted individually indicates that either double buffering is turned off, or that the paint code in the button UI makes use of paintImmediately().

我使用64x64 JButton测试了您的设置,确保所有UI操作均在EDT(事件调度线程)中执行.我可以确认您看到的效果,更改所有按钮的背景大约需要1200毫秒,并且每个框都会立即重新粉刷. 您可以通过将网格设置为不可见之前和更改背景之后可见的方式来绕过立即重绘:

I tested your setup with 64x64 JButtons, an made sure that all UI operations were executed in the EDT (Event Dispatch Thread). I can confirm the effect you saw, changing the background of all buttons took about 1200 ms, with every box repainted immediately. You can bypass the immediate repaints by setting the grid to non-visible before, and to visible after you changed the backgrounds:

grid.setVisible(false);
for (Component comp : grid.getComponents()) {
   comp.setBackground(color);
}
grid.setVisible(true);

这导致网格仅执行一次重绘,并将时间减少到〜300ms(因数4).

This caused the grid to do only one repaint, and reduced the time to ~300ms (factor 4).

对于频繁更新而言,这仍然太慢,因此最好使用绘制网格的自定义组件,或者使用重量较轻的容器(问题注释中建议的垃圾桶),如果您希望允许网格单元是任意组件.

This is still too slow for frequent updates, so you're better off with a custom component which draws the grid, or a flyweight container (what trashgod suggested in the comment to your question) if you want allow the grid cells to be arbitrary components.

这篇关于更改JButtons背景的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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