简体   繁体   English

在NetBeans中运行代码时如何更改jlabel背景颜色

[英]how can i change jlabel background color when running the code in netbeans

I want to develop simulator using netbeans. 我想使用Netbeans开发模拟器。 so i choose insertion sort.i there i need to change the jlabel background color when running the code.that means i want to color line by line when running the code.for that case i use the below code segment.there are more codes.but i mention only relavent part.how do i change the jlabel color! 所以我选择插入排序。我在运行代码时需要更改jlabel背景颜色。这意味着我想在运行代码时逐行着色。在这种情况下,我使用下面的代码段。还有更多代码。但我只提到relavent part。我如何更改jlabel的颜色!

for (j = 1; j < 7; j++) { sleep(); for(j = 1; j <7; j ++){sleep();

        key = numArray[j];
        jLabel11Algo2.setBackground(Color.red);

        sleep();
        jLabel11Algo2.setBackground(Color.white);

        System.out.println(key);

First you need to make the label opaque so its background will be painted: 首先,您需要使标签不透明,以便其背景被绘制:

label.setOpaque(true);

Secondly to do your animation you need to use a Swing Timer to do the animation, not Thread.sleep(). 其次,要制作动画,您需要使用Swing Timer来制作动画,而不是Thread.sleep()。

You can't use Thread.sleep() because that will cause the Event Dispatch Thread to sleep which means the GUI can't repaint itself. 您不能使用Thread.sleep(),因为这将导致事件调度线程进入睡眠状态,这意味着GUI无法重绘自身。 Read the section from the Swing tutorial on Concurrency for more information. 阅读Swing 并发教程中的有关更多信息的部分。

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

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