Swing,Java和多线程以及着色按钮 [英] Swing, Java and multithreading, and coloring buttons

查看:177
本文介绍了Swing,Java和多线程以及着色按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,这是家庭作业.是的,我完全被卡住了.

Yes, this is homework. Yes, I am completely stuck.

这是要点.我已经创建了一个JFrame.有3个面板(顶部,中间,底部).底部面板中有3个按钮,分别是:红色,绿色和蓝色.顶部面板中的3个文本字段为我们提供了单击相应按钮的次数.每个按钮最多可容纳10个.中间的面板中是一个8 x 8的Jbutton网格,编号为0到63.到目前为止,还不错.

Here is the gist. I have created a JFrame. There are 3 panels (top, middle, bottom). In the bottom panel are 3 buttons called: Red, Green, and Blue. In the top Panel are 3 text fields that give us the number of times we have clicked the respective button. The max we are allowed is 10 for each button. In the middle Panel is an 8 x 8 grid of Jbuttons numbered from 0 to 63. So far, so good.

每次我们单击一个按钮时,都会启动一个线程.没有线程消亡当线程启动时,会随机选择一个0到63之间的数字.对应于该数字的JButton被涂上单击的颜色.因此,如果单击红色按钮,我们应该会看到一个白色背景的框变为红色.但是随后,该JButton的颜色开始褪色,直到变为白色为止.该过程大约需要8秒钟.

Every time we click a button, a thread starts. No thread ever dies When a thread starts, a number from 0 to 63 is randomly chosen. The JButton corresponding to that number is painted the color that was clicked on. So if the red button was clicked, we should see a box with a white background turn red. But then the color of that JButton starts to fade until it becomes white. The process should take about 8 seconds.

您创建的线程不应访问任何Swing组件.相反,必须维护数据结构并根据线程的执行周期进行更新.另一方面,定期从主线程调用repaint()方法以邀请Swing Event Dispatcher线程最终访问数据结构的内容并相应地显示GUI组件.

Threads that you create should not have access to any Swing components. Rather, one has to maintain a data structure and update by threads according to their cycle of execution. On the other hand, periodically invoke repaint() methods from main thread to invite Swing Event Dispatcher thread to eventually visit the content of the data structure and display the GUI components accordingly.

....我已经创建并显示了所有对象.您不能在一个按钮上单击10次以上.这是我的位置:

........ I have gotten all the objects created and displayed. You can't click more than 10 times on a button. Here is where I am:

我有两个数组:一个是大小为64的字符串数组.它们表示按钮.我也有一些整数.这样我就知道了创建线程的顺序.单击按钮后,我已经创建了线程,并且已经启动了它们.这是我的线程运行方法:

I have two arrays: one is an array of strings with size 64. They represent the buttons. I also have an array of ints. This is so that I know the order by which the threads were created. I have created the threads as a button is clicked, and I have started them. Here is my run method for the threads:

public void run() {
    Random num = new Random(new Date().getTime());
    while (true) {
        Thread j = Thread.currentThread();
        int randInt = num.nextInt(64);
        synchronized (lock) {

            if ((array[randInt].compareTo("red") == 0
                 || array[randInt].compareTo("blue")
                 == 0 || array[randInt].compareTo("green") == 0))
            {
                randInt = num.nextInt(64);
            }
            for (int k = 0; k < 10; k++) {
                if (threadarray[k] == -1) {
                    threadarray[k] = randInt;
                    break;
                }
            }

        }
    }
}

即使我们没有介绍它,我也尝试使用Timer对象,该对象会在锁部分之外立即消失.这将我带到actionPerformed方法.我已经添加了所有适当的注册.

Even though we haven't covered it, I have tried to use a Timer object that immediately goes off right outside of the lock section. This takes me to the actionPerformed method. I have added all the appropriate registration.

public void actionPerformed(ActionEvent arg0) {
    for (int i = 0; i < threadarray.length; i++) {

        int num = threadarray[i];
        if (num != -1) {
            System.out.println(num);
            String s = array[num];
            System.out.println(s + "is ");
            if (s.compareTo("red") == 0) {
                button[num].setOpaque(true);
                button[num].setBackground(Color.red);
                while (button[num].getBackground() != Color.white) {
                    System.out.println("not white yet");
                    int g = button[num].getBackground().getGreen();
                    int b = button[num].getBackground().getBlue();
                    if (255 - (g + 1) >= 0) {
                        Color c = new Color(255, g + 1, b + 1, 1);
                        button[num].setOpaque(true);
                        button[num].setBackground(c);
                        System.out.println(c + " " + " c is");
                    } else {
                        button[num].setBackground(Color.white);
                    }
                }
            }

            System.out.println(i + " i is " + button[num].getBackground()); //just some debugging info
            threadarray[i] = -1;   //clear the thread array
            array[num] = "0"; //clear the string array

        }
    }
}

actionPerformed方法由事件调度线程处理. (请注意,上面的代码仅用于红色线程.其想法是通过逐渐增加绿色和蓝色直到变成白色来淡化颜色.

The actionPerformed method is handled by the Event Dispatch Thread. (Note the code immediately above is for the red threads only. The idea is to fade the color by inclemently increasing the green and blue colors till it becomes white.

问题:当我单击底部的红色按钮时,没有按钮会改变颜色(是的,已经完成了适当的注册.)我也不知道如何控制多个线程的计时.我什至在这里走正确的路吗?

Problems: no button ever changes color when I click the red button on bottom (yes, appropriate registration has been done.) I also have no idea how to control the timing with a multitude of threads going. Am I even going down the right road here?

推荐答案

这个示例 在不付出太多的情况下处理颜色和忽略setBackground()的按钮的方法.示例此处此处演示如何淡化颜色.由于两者都依赖于javax.swing.Timer线程,因此两者都不是解决方案,但是这些技术可能证明是有用的.

Without giving away too much, this example illustrates a way to deal with color and buttons that ignore setBackground(). Examples here and here demonstrate how to fade the color. As both rely on a javax.swing.Timer thread, neither is a solution, but the techniques may prove useful.

这篇关于Swing,Java和多线程以及着色按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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