Java – 如何每秒更新一个面板
我正在尝试创建一个显示当前时间的Java GUI。 目前,我可以让它显示当前时间,但仅限于启动时。 然后它永远保持在那个时间。 原因是我无法弄清楚如何让它每秒自动加载到新的当前时间。 这是我迄今为止的相关代码:
// 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);
我尝试使用来自此线程的信息进行复习,但无济于事,窗口(运行时)只是空白。 然后我尝试使用来自此线程的信息进行不同的复习,并创建了:
// 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
定义为private JPanel contentPane;
这也没有用,但只有时间本身是空白的,窗口中的其他内容(另一个JLabel(只是一些文本))仍然正常。
没有任何复习,它的行为如上所述,它只是显示它开始的时间并永远保持在那个时间。
我正在使用Eclipse和WindowBuilder。 (而且我(可能很明显)是Java GUI的完整菜鸟xD)
首先,您使用的是java.util.Timer而不是javax.swing.Timer 。 在使用Swing组件时,您需要使用第二个类,以确保在事件调度线程上进行GUI更新。 另请参阅Swing中的Concurrency教程。
正如其他答案中所建议的那样,每次要更新其文本时都无需删除/添加JLabel
。 只需调用JLabel.setText()方法即可。
如果您仍希望每次都删除/添加JLabel
,请注意以下事项:
从Container.add() javadoc:
此方法更改与布局相关的信息,因此使组件层次结构无效。 如果已显示容器,则必须在此后validation层次结构以显示添加的组件。
然后你需要调用Component.revalidate()方法。 喜欢这个:
contentPane.remove(lblThetime); contentPane.add(lblThetime); contentPane.revalidate();
我发现了解决方案!
我尝试了所有解决方案作为答案在这里,没有一个给代码完全工作,但所有答案指出我正确的方向。 我在一个网站上找到了问题的解决方案,我已经忘记了它的名字,但是我使用了它的建议并提出了最终的解决方案:
// New timer which works! int delay = 1000; //milliseconds ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { String date = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date(System.currentTimeMillis())); lblThetime.setText(date); } }; new Timer(delay, taskPerformer).start();
感谢所有回答没有说出答案的人我可能无法找到这个解决方案:D
您应该每秒更改标签的字符串,而不是删除标签并将其添加到面板。
Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { String date = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date() ); lblThetime.setText(date); } }, 1000, 1000); // Creating the panel... JLabel lblThetime = new JLabel(date);
您可以尝试使用Swing Timer执行该任务,例如:
private static JLabel l; public static void main(String[] args) { Timer timer = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { l.setText(new Date().toString()); } }); timer.start(); JFrame f = new JFrame(); l=new JLabel(new Date().toString()); f.getContentPane().add(l); f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
该示例每秒更新JLabel的新日期
Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { label.setText(new Date().toString()); } }, 1000, 1000);
这不简单吗?
线索1:真正在Java中搜索Timer类。 你选错了吗?
线索2:改为更新标签文字。
HIH
KL