0
有没有办法自动更新JPanel上的SimpleDateFormat数字时钟?SimpleDateFormat时钟自动更新
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
time.setText(sdf.format(new Date()));
如果我运行此,标签停留在剧本推出的时间...
UPDATE
我写的,到目前为止
import java.awt.*;
import java.awt.image.BufferedImage;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
public class test {
private JFrame frame = new JFrame("Time");
private JPanel panel = new JPanel();
private JLabel time = new JLabel();
private JButton exit = new JButton("exit");
private boolean exit_bool = false;
public test() {
panel.setLayout(new GridBagLayout());
panel.add(time);
panel.add(exit);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel);
frame.setLocationRelativeTo(null);
init(true, true);
createHandler();
startTime();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
test viewable = new test();
}
});
}
public void init(boolean fullscreen, boolean mouse) {
frame.setVisible(false);
frame.dispose();
frame.setUndecorated(true);
frame.setVisible(true);
if(fullscreen) {
frame.setExtendedState(frame.MAXIMIZED_BOTH);
}
if(!mouse) {
frame.setCursor(java.awt.Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(1,1,BufferedImage.TYPE_4BYTE_ABGR),new java.awt.Point(0,0),"NOCURSOR"));
}
}
public void startTime() {
while(!exit_bool) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
time.setText(sdf.format(new Date()));
}
}
public void createHandler() {
exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitActionPerformed(evt);
}
});
}
private void exitActionPerformed(java.awt.event.ActionEvent evt) {
exit_bool = true;
}
}
你有t o在你决定的任何时间间隔更新时间。 –
但是如何?我尝试了一个while循环,但然后脚本根本不会启动 – flexwie
这与'SimpleDateFormat'无关。你的问题在于你的GUI和/或更新的循环。没有更多的细节,我们不禁感慨。 –