使文本在 JPanel 中短暂显示
我试图让文本在消失之前短暂出现。它将沿着
的思路 1) 设置颜色为黑色
2) 等待 x 秒
3)设置颜色为背景色
我调用的方法是repaint(),然后调用paintComponent(Graphics Painter)。 repaint() 仅当我按下空格键时才会被调用。
我想过尝试 repaint();Thread.sleep(1000);repaint();
(我确实捕获了中断异常,只是没有显示),但它只调用 paintComponent 每个空格键一次。
有没有一种简单的方法可以做到这一点,或者这是否有点具有挑战性?
I am trying to make text appear briefly before it disappears. It would be along the lines of
1) Set color to black
2) wait x amount of seconds
3) set color to background color
The method I call is repaint(), which then calls paintComponent(Graphics painter). repaint() is called only if I press the space-bar.
I thought of trying repaint();Thread.sleep(1000);repaint();
(I do catch the Interrupt exception, just not shown), but it only calls paintComponent once per space-bar .
Is there an easy way to do this or is this something that is a bit challenging?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将使用 Swing Timer 来安排文本的重新绘制。
另外,我只使用 JLabel 来显示文本。然后,您只需使用 setText(...) 方法根据需要更改文本,组件就会重新绘制自身。
I would use a Swing Timer to schedule the repainting of the text.
Also, I would just use a JLabel to display the text. Then you just use the setText(...) method to change the text as you wish and the component will repaint itself.
您需要重写面板中的paint方法并使其实现
Runnable
,以便您可以在几秒钟后关闭文本。这是一些示例代码:You need to override the paint method in your panel and make it implement
Runnable
so that you can turn off the text after a few seconds. Here is some sample code: