2

我有一个 JTable,用户可以在其中选择一行。如果发生这种情况,我想在短时间内“突出显示”页面的另一部分,以表明这是在用户交互后更改的页面部分。

所以我的问题是:实现这一目标的最佳方法是什么?目前,我通过设置该面板的背景颜色并启动一个 SwingWorker 来完成它,该 SwingWorker 在短暂延迟后将颜色设置回来。它按预期工作,但使用这样的 SwingWorker 是个好主意吗?这种方法有什么缺点吗?你会如何解决这个问题?

提前致谢。

4

2 回答 2

2

我想 Swing Timer 会是一个更好的选择,因为它为所有计划的事件重用一个线程并在主事件循环上执行事件代码。因此,在您的SelectionListener代码中,您可以:

// import javax.swing.Timer;

final Color backup = componentX.getBackground();
componentX.setBackground(Color.YELLOW);
final Timer t = new Timer(700, new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    componentX.setBackground(backup);
  }
});
t.setRepeats(false);
t.start();
于 2012-07-10T07:49:29.720 回答
0

我推荐一个摇摆定时器(javax.swing.Timer)。(不要在 Java.util 中使用 Timer 类)

这是您制作计时器的地方:

Timer t = new Timer(loopTime,actionListener)//loopTime is unimportant for your use of this
t.setInitialDelay(pause)//put the length of time between starting the timer and the color being reverted to normal
t.setRepeats(false);//by default, timer class runs on loop.
t.start();//runs the timer

保留对计时器的引用可能是有意义的,然后在需要时调用 t.start 。

您需要实现一个动作侦听器来处理计时器事件。如果您不知道该怎么做,我可以编辑它,但由于您已经在使用 Swing 做一些事情,我认为这应该不是问题。

于 2012-07-09T16:15:18.280 回答