如何使用JLabel和Swing Timer来向用户发送简短的通知?

huangapple 未分类评论48阅读模式
英文:

How to use JLabel and Swing Timer to briefly notify the user?

问题

我正在为课程制作一个小应用程序。我想要其中一个功能是,当点击按钮时,根据他们填写的字段将一个对象添加到ArrayList中。当他们点击按钮时,我希望他们能够得到视觉确认已执行该操作。然而,仅更新JLabel会导致在按钮被点击后它将永远停留在那里。

  • 我希望标签最开始显示为“”。
  • 当点击按钮时,我希望它显示为“Character Added!”
  • ..持续几秒钟,然后切换回“”。

我查找了一些关于使用计时器更新标签的问题,但大多数问题都是使用计时器来显示倒计时钟,它们通常使用计时器的启动方法。我对是否需要计时器的停止方法来重置标签感到困惑。

有什么指导意见吗?

英文:

I'm working on a small app for a class. One of the functions I want is when a button is clicked, it adds an object to an ArrayList based on the fields that they filled. When they click they button, I want them to have visual confirmation that the action has been performed. However, just updating a JLabel would cause it to sit there forever after the button was clicked.

  • I would like the label to start off displaying "".
  • When the button is clicked, I want it to say "Character Added!"
  • ..for a few seconds. and then switch back to "".

I looked up some questions about label updating using a timer, but most of them have been using it to display a counting down clock, and they usually use a timer start method. I'm confused about if you need a timer stop method to reset the label.

Any pointers?

答案1

得分: 0

我最终做的是,不再使用更新的 JLabel,而是简单地在按钮被按下并且字符被添加到 ArrayList 时,调用

JOptionPane.showMessageDialog(null, "已添加字符!");

这样,不再是一个小小的 JLabel 来确认,而是得到一个非常明显的通知,需要用户确认。比起使用整个 Timer 的方法要简单得多。

英文:

What I ended up doing is, instead of using a JLabel that updates, I simply made it so when the button is pressed and a character is added to the ArrayList, I called

JOptionPane.showMessageDialogue("Character Added!");

That way instead of a small little JLabel confirming it, they get a very apparent notification that requires them to acknowledge it. Tons more simple than doing the whole Timer thing.

huangapple
  • 本文由 发表于 2020年5月3日 14:46:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/61570625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定