java按钮添加线程,Swing,Java和多线程以及着色按钮

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

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.

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.

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.

........ 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:

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;

}

}

}

}

}

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

}

}

}

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值