Java - 如何每秒更新一个面板 [英] Java - How to update a panel every second

查看:252
本文介绍了Java - 如何每秒更新一个面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个显示当前时间的Java GUI。目前,我可以让它显示当前时间,但仅限于启动时。然后它永远保持在那个时间。原因是我无法弄清楚如何让它每秒自动加载到新的当前时间。这是我到目前为止的相关代码:

I'm trying to create a Java GUI which displays the current time. Currently, I can make it display the current time, but only on startup. It then simply remains on that time forever. The reason is that I cannot figure out how to make it automatically load in the new current time every second. Here is my relevant code thus far:

// Getting the current time...
long epoch = System.currentTimeMillis() / 1000;
String date = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date(epoch * 1000));

// Creating the panel...
JLabel lblThetime = new JLabel(date);
sl_panel.putConstraint(SpringLayout.NORTH, lblThetime, 55, SpringLayout.SOUTH, lblIBeA);
sl_panel.putConstraint(SpringLayout.WEST, lblThetime, 139, SpringLayout.WEST, panel);
lblThetime.setFont(new Font("Avenir Next", Font.PLAIN, 40));

// Adding the time to the panel
panel.add(lblThetime);

// My refresher which doesn't work
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        removeAll();
        validate();
        repaint();
    }
}, 1000, 1000);

我试图使用来自这个帖子,但无济于事,窗口(运行时)只是空白。然后,我尝试使用来自此主题的信息进行不同的复习,并创建了这个:

I attempted to make a refresher using information from this thread, but to no avail, the window (when run) is just blank. I then tried making a different refresher using information from this thread, and created this:

// My refresher which doesn't work
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        contentPane.remove(lblThetime);
        contentPane.add(lblThetime);
    }
}, 1000, 1000);

contentPane 已被定义为私有JPanel contentPane;

哪些也无效,但只有时间本身为空白,其余内容在窗口中(另一个JLabel(只是一些文本))保持正常。

Which also didn't work, however only the time itself is blank, the rest of the content in the window (One other JLabel (just some text)) remains as normal.

没有任何复习它的行为如上所述,它只显示它开始的时间并且永远留在那个时间。

Without any refresher it behaves as described above, whereby it just displays the time when it started and remains on that time forever.

我正在使用Eclipse和WindowBuilder。 (而且我(可能很明显)是一个完整的Java GUI东西xD)

I'm using Eclipse with WindowBuilder. (And I'm (probably evidently) a complete noob to Java GUI stuff xD)

推荐答案

首先你正在使用< a href =http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html\"rel =nofollow> java.util.Timer 而不是 javax.swing.Timer 。在使用Swing组件时,您需要使用第二个类,以确保在事件派遣线程。另请参阅 Swurrency中的并发教程。

First of all you are using java.util.Timer instead of javax.swing.Timer. You need use this second class when working with Swing components to ensure GUI updates are made on the Event Dispatch Thread. Also take a look to Concurrency in Swing tutorial.

正如其他答案中所建议的那样,每次要更新文本时都不需要删除/添加 JLabel 。只需致电 JLabel .setText()方法。

As suggested in other answers, there is no need to remove/add JLabel each time you want to update its text. Just call JLabel.setText() method.

如果您仍希望每次删除/添加 JLabel ,然后要注意这一点:

If you still want remove/add the JLabel each time, then be aware of this:

来自 Container.add() javadoc:

From Container.add() javadoc:


此方法更改与布局相关的信息,因此
使组件层次结构无效。 如果容器已显示
,则必须在此后验证层次结构,以便
显示添加的组件。

然后你需要调用 Component.revalidate()方法。像这样:

Then you'll need to call Component.revalidate() method. Like this:

contentPane.remove(lblThetime);
contentPane.add(lblThetime);
contentPane.revalidate();

这篇关于Java - 如何每秒更新一个面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆