gui时钟
时间: 2023-07-27 17:07:14 浏览: 34
Here's an example of how to create a simple GUI digital clock using Java Swing:
```java
import javax.swing.*;
import java.awt.*;
import java.text.*;
import java.util.*;
public class DigitalClock extends JFrame {
private JLabel timeLabel;
public DigitalClock() {
setTitle("Digital Clock");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200, 100);
setLocationRelativeTo(null);
timeLabel = new JLabel();
timeLabel.setFont(new Font("Arial", Font.BOLD, 36));
timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().add(timeLabel);
Timer timer = new Timer(1000, (e) -> updateTime());
timer.setRepeats(true);
timer.start();
}
private void updateTime() {
Date now = new Date();
DateFormat df = new SimpleDateFormat("HH:mm:ss");
timeLabel.setText(df.format(now));
}
public static void main(String[] args) {
DigitalClock clock = new DigitalClock();
clock.setVisible(true);
}
}
```
This code creates a `JFrame` window with a centered `JLabel` that displays the current time in hours, minutes, and seconds. The time is updated every second using a `javax.swing.Timer`. You can customize the appearance of the clock by adjusting the font, size, and alignment of the `JLabel`.