java更新面版,Java - 如何每秒更新一个面板

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);

With contentPane having been defined as private JPanel contentPane;

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.

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

解决方案

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.

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.

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

This method changes layout-related information, and therefore,

invalidates the component hierarchy. If the container has already been

displayed, the hierarchy must be validated thereafter in order to

display the added component.

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

contentPane.remove(lblThetime);

contentPane.add(lblThetime);

contentPane.revalidate();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值