"I want to make something with Java, but I can't think of a practical application."
"I want to try making an app that works in real time."
For those who have such concerns, we recommendCreating a Clock Appis.
In JavaA simple class and GUI combinationYou can then create an app that displays the current time in real time.
Even beginners can enjoy learning how to get the time and update the display.It's the subject.
In this article, we will show you how anyone can make it.Detailed explanation with easy-to-understand codeWe will continue to do so.
- What is the point of creating a clock app?
- How to Get the Current Time in Java
- How to display the current time in a GUI
- How to update every second with a Timer
- Completed code for the Java clock app
- What to do when it doesn't work properly
- Application ideas and step-up
- Summary and configuration of the completed code
- Summary: The next step in learning Java
What is the point of creating a clock app?
Why are clock apps great for learning?
Conclusion: Because you can learn the basics of real-time processing and GUI updates at the same time.
The next step for Java beginners is to:
It makes a lot of sense to create an app that runs in real time.
What you'll learn by building a clock app:
- Get the current time (
LocalTime
class, etc.) - Looping and waiting (
Timer
(How to use, etc.) - Swing GUI display and update
For example, the Ministry of Education, Culture, Sports, Science and Technology's "Development of Information Utilization Skills"
Emphasis on "developing computational thinking by linking it to real life"It has been
(Reference: https://www.mext.go.jp/a_menu/shotou/zyouhou/detail/1375607.htm).
The clock app is the gatewayThe perfect subjectis.
How to Get the Current Time in Java
How to use LocalTime and DateTimeFormatter
Conclusion: Java 8 and abovejava.time
This can be easily achieved using the package.
1 | import java.time.LocalTime; import java.time.format.DateTimeFormatter; LocalTime now = LocalTime.now(); String time = now.format(DateTimeFormatter.ofPattern( "HH:mm:ss" )); |
With this code, you can always get the latesthours: minutes: secondsYou can get the time as a string.
How to display the current time in a GUI
Steps to display on the screen using Swing
Conclusion:JLabel
You can set it to a time string and display it on the screen.
First, the basic label:
1 | JLabel label = new JLabel( "Current time: 00:00:00" ); frame.add(label); |
and,How the time is updated every secondAs,javax.swing.Timer
Use.
How to update every second with a Timer
Update the time every second with javax.swing.Timer
In conclusion, you can use a Timer to set up periodic event processing.
Below is the code for updating using a Timer.
1 | Timer timer = new Timer( 1000 , new ActionListener() { public void actionPerformed(ActionEvent e) { LocalTime now = LocalTime.now(); String time = now.format(DateTimeFormatter.ofPattern( "HH:mm:ss" )); label.setText( "Current time:" + time); } }); timer.start(); |
This will update the screen every second.A real-time clockIt will be.
Completed code for the Java clock app
1 | import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class ClockApp extends JFrame { private JLabel label; public ClockApp() { label = new JLabel(); label.setFont( new Font( "Arial" , Font.BOLD, 32 )); label.setHorizontalAlignment(SwingConstants.CENTER); getContentPane().add(label); setTitle( "Clock app" ); setSize( 300 , 150 ); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible( true ); Timer timer = new Timer( 1000 , new ActionListener() { public void actionPerformed(ActionEvent e) { LocalTime now = LocalTime.now(); String time = now.format(DateTimeFormatter.ofPattern( "HH:mm:ss" )); label.setText(time); } }); timer.start(); } public static void main(String[] args) { new ClockApp(); } } |
What to do when it doesn't work properly
Common mistakes and how to fix them
Common mistakes:
java.time
is not importedTimer
andjava.util.Timer
Make a mistakeSwing
The components are not positioned correctly.
How to improve:
javax.swing.Timer
Explicitly usemain
Within a methodnew ClockApp();
Just run- Font size and frame size can also be adjusted (
setFont()
orsetSize()
)
Application ideas and step-up
Examples of features we would like to try next
To use the Clock app:
- In addition to seconds, the date is also displayed.
- World Clock (
ZoneId
to display the time in other countries) - Add an alarm function (set the alarm time)
JOptionPane
display)
these are,Perfect for those who want to use Java more practicallyThis is an advanced topic.
Summary and configuration of the completed code
1 | ClockApp.java // Single file |
It's a simple GUI-based app,A sense of accomplishment for Java beginnersContents:
Summary: The next step in learning Java
In this article,How to make a clock app in JavaWe explained the following.
Lessons learned:
LocalTime
How to useSwing
Screen display byTimer
Real-time updates using