我正在使用小程序在 NetBeans 中开发排序算法模拟器。我有一个计时器和一个 TimerTask。我想根据标签的排序方式更改标签的背景颜色。第一次运行会改变颜色,但之后颜色不会改变。我在调试模式下运行程序,发现第一次运行后 TimerTask 抛出了 InterruptedException。我不知道为什么会这样。
这是我的 TimerTask 方法,其中包含 run() 方法。对不起,如果它的冗长代码。
private class AnimationUpdator extends TimerTask {
JLabel[] values = {valuejLabel3, valuejLabel4, valuejLabel5, valuejLabel6, valuejLabel7,
valuejLabel8, valuejLabel9, valuejLabel10, valuejLabel11, valuejLabel12};
JLabel[] pointer = {pointerjLabel3, pointerjLabel4, pointerjLabel5, pointerjLabel6, pointerjLabel7,
pointerjLabel8, pointerjLabel9, pointerjLabel10, pointerjLabel11, pointerjLabel12};
JLabel[] okPointer = {okjLabel3, okjLabel4, okjLabel5, okjLabel6, okjLabel7,
okjLabel8, okjLabel9, okjLabel10, okjLabel11};
int runCount = 0;
public void run() {
if (order.equals("Ascending")) {
if (i == 10) {
pointer[i - 1].setVisible(false);
jLabel3.setText("Sorted!!!");
stop();
} else {
JLabel key = values[i];
key.setBackground(Color.red);
j = i - 1;
int init = 0;
pointer[i].setText("i-->");
pointer[j].setText("j-->");
pointer[i].setVisible(true);
pointer[j].setVisible(true);
while (j >= 0 && Integer.parseInt(values[j].getText()) > Integer.parseInt(key.getText())) {
values[j].setBackground(Color.blue);
insert.setLabel1x(values[j].getX());
insert.setLabel1y(values[j].getY());
if (swap) {
insert.setLabel2x(values[i].getX());
insert.setLabel2y(values[i].getY());
} else {
insert.setLabel2y(key.getY());
insert.setLabel2x(key.getX());
}
int xCounterForward = 0;
int xCounterBackward = 0;
int yCounter = 0;
for (int maincount = 0; maincount < 60; maincount++) {
onLoop = true;
if (xCounterForward < 20) {
insert.setLabel1x(values[j].getX() + 1);
if (init == 0) {
insert.setLabel2x(values[i].getX() - 1);
}
xCounterForward++;
} else if (xCounterForward == 20 && yCounter < 20) {
insert.setLabel1y(values[j].getY() + 1);
if (swap) {
insert.setLabel2y(values[i].getY() - 1);
} else {
insert.setLabel2y(key.getY() - 1);
}
yCounter++;
} else if (yCounter == 20 && xCounterBackward < 20) {
insert.setLabel1x(values[j].getX() - 1);
xCounterBackward++;
}
values[j].setLocation(insert.getLabel1x(), insert.getLabel1y());
if (swap) {
values[i].setLocation(insert.getLabel2x(), insert.getLabel2y());
} else {
key.setLocation(insert.getLabel2x(), insert.getLabel2y());
}
try {
Thread.sleep(MS_TO_WAIT2);
} catch (InterruptedException e) {
}
}
swap = false;
values[j + 1] = values[j];
init++;
pointer[j].setVisible(false);
j = j - 1;
if (j >= 0) {
pointer[j].setText("j-->");
pointer[j].setVisible(true);
}
}
if (j >= 0 && Integer.parseInt(values[j].getText()) < Integer.parseInt(key.getText())) {
values[j].setBackground(Color.green);
values[i].setBackground(Color.green);
okPointer[j].setText("Ok");
okPointer[j].setVisible(true);
}
if (j == -1) {
values[j + 1].setBackground(Color.green);
values[i].setBackground(Color.green);
okPointer[j + 1].setText("Ok");
okPointer[j + 1].setVisible(true);
}
if (onLoop) {
for (int maincount = 0; maincount < 20; maincount++) {
insert.setLabel2x(key.getX() + 1);
insert.setLabel2y(key.getY());
key.setLocation(insert.getLabel2x(), insert.getLabel2y());
try {
Thread.sleep(MS_TO_WAIT2);
} catch (InterruptedException e) {
}
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
values[j + 1] = key;
onLoop = false;
i++;
if (j >= 0) {
try {
Thread.sleep(MS_TO_WAIT2);
} catch (InterruptedException e) {
}
okPointer[j].setVisible(false);
pointer[j].setVisible(false);
} else if (j == -1) {
okPointer[j + 1].setVisible(false);
}
}
}
}
}