Hello, everyone, I’m grey ape!
Today and we share a use of Java multithreading development of electronic watch project, can realize the real-time display of time in the electronic watch, modify and stopwatch function.
The design sequence of Java electronic table is from the front end interface to the back end class and thread design, and then combine the front and back ends. The following is the development process of electronic watch:
1. Design of front-end interface
The front interface of electronic watch is designed according to JFrame form and Container, and the controls such as time display, time modification and stopwatch display are rationally arranged by using absolute positioning method, so as to make the interface beautiful and simple.
2, add control event listener
After the completion of interface design, the work is to add function listener to the corresponding control. Here, the ActionListener interface is called, and the actionPerformed method is rewritten to add listener to the three buttons of “confirm modification”, “start stopwatch” and “pause”. And add events to the corresponding listener so that the corresponding event can be triggered when the button is clicked. The following is a rewrite of the actionPerformed method
@Override
public void actionPerformed(ActionEvent e) {
// If you click the confirm modification button
if (e.getSource() == amend_JB) {
// Get the value of the drop-down box
String hour_amend = hourAmend.getSelectedItem().toString();
String minute_amend = minuteAmend.getSelectedItem().toString();
String second_amend = secondAmend.getSelectedItem().toString();
/ / JOptionPane. ShowMessageDialog (null, "modify success!" );
isThreadShow = false; // Set the thread flag to False to abort the thread
// Display the modified value
hourShow.setText(hour_amend);
minuteShow.setText(minute_amend);
secondShow.setText(second_amend);
System.out.println("The modified time is:" + hour_amend + ":" + minute_amend + ":" + second_amend);
threadAmend.start();// Start the thread with the modified running time
}
// If the stopwatch button is clicked
if (e.getSource() == second_JB) {
// If the current stopwatch is in the start state, it shows the stop time
if (second_JB.getText() == "Stop the timer") {
second_JB.setText("Start the stopwatch");
second_JB.setBackground(Color.yellow);
//threadSecond.stop();
isStartSecond = false;
}
else {
// If the current stopwatch is off
second_JB.setText("Stop the timer");
second_JB.setBackground(Color.RED);
threadSecond.start();// Start the stopwatch thread
isStartSecond = true; }}// If the pause button is clicked
if (e.getSource() == pause_JB) {
if (pause_JB.getText() == "Pause") {
pause_JB.setText("Continue");
pause_JB.setBackground(Color.cyan);
threadSecond.suspend(); // Suspend the thread
}
else {
pause_JB.setText("Pause");
pause_JB.setBackground(Color.RED);
threadSecond.resume(); // Let the thread continue}}}Copy the code
3. Real-time display of time through the main thread
After adding the listening function to the button control, it is the display of the current time, the display of time is using the main thread, and in the main thread every second to update the display of data, here the use of Date class to read the system time, and the use of SimpleDateFormat to obtain the time for standardized processing, Then the processed year, month, day, week and current time will be displayed on the interface.
@Override
public void run(a) {
while (true) {
// TODO Auto-generated method stub
Date date = new Date(); // instantiate the timeclass object
// Normalize the currently obtained time
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
SimpleDateFormat weekFormat = new SimpleDateFormat("EEEE");
SimpleDateFormat hourFormat = new SimpleDateFormat("HH");
SimpleDateFormat minuteFormat = new SimpleDateFormat("mm");
SimpleDateFormat secondFormat = new SimpleDateFormat("ss");
// Get the current year, month, day, hour, minute, second
year = yearFormat.format(date);
month = monthFormat.format(date);
day = dayFormat.format(date);
week = weekFormat.format(date);
hour = hourFormat.format(date);
minute = minuteFormat.format(date);
second = secondFormat.format(date);
hourShow.setText(hour);
minuteShow.setText(minute);
secondShow.setText(second);
timeShow.setText(year + "Year" + month + "Month" + day + "Day" + week);
try {
Thread.sleep(1000); // Make the thread execute once every second
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// If the thread aborts, break out of the loop
if(! isThreadShow) {break; }}}Copy the code
4. Enable the thread to modify the time
After displaying the system time, it is the operation of modifying the current time. The design idea here is to obtain the modified time first, and then use break to break out of the loop of the main thread, thus ending the operation of the main thread. After that, the modified time that has been read will be displayed in the label of displaying the time. At the same time, the threadAmend thread, which keeps running after modification, is started and updated every second so that time can continue to execute on the modified basis. The effect is shown below:
// Set the threadAmend thread
threadAmend = new Thread(new Runnable() {
@Override
public void run(a) {
while (true) {
int hourAmend = Integer.parseInt(hourShow.getText());
int minuteAmend = Integer.parseInt(minuteShow.getText());
int secondAmend = Integer.parseInt(secondShow.getText());
secondAmend++;
// Determine and process the obtained time
if (secondAmend == 60) {
minuteAmend++;
secondAmend = 0;
}
if (minuteAmend == 60) {
hourAmend++;
minuteAmend=0;
}
if (hourAmend == 24) {
hourAmend = 0;
minuteAmend = 0;
secondAmend = 0;
}
// Display the time
hourShow.setText(Integer.toString(hourAmend));
minuteShow.setText(Integer.toString(minuteAmend));
secondShow.setText(Integer.toString(secondAmend));
try {
threadAmend.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch blocke.printStackTrace(); }}}});Copy the code
5. Thread synchronization realizes stopwatch function
When the time is changed, the threadSecond thread is set to run the stopwatch. This thread is used to start the stopwatch after clicking the “Start stopwatch” button. It also starts the timer in the background, updates the data every second, and then adds an event to the “pause” button. You can pause threadSecond by clicking pause, and the button becomes Continue. After clicking Continue, the threadSecond thread will continue to execute until it breaks out of the loop by clicking Stop Timing. The effect is shown below:
// Set the threadSecond stopwatch thread
threadSecond = new Thread(new Runnable() {
@Override
public void run(a) {
// Initialize the data to 0 each time you start the stopwatch
hourSecond = 0;
minuteSecond = 0;
secondSecond = 0;
while (true) {
secondSecond++; // The number of seconds is increased by 1
// Determine and process the obtained time
if (secondSecond == 60) {
minuteSecond++;
secondSecond = 0;
}
if (minuteSecond == 60) {
hourSecond++;
minuteSecond=0;
}
String SecondText = Integer.toString(hourSecond) + ":" + Integer.toString(minuteSecond) + ":" + Integer.toString(secondSecond);
second_Time.setText(SecondText);
try {
threadSecond.sleep(1000); // Let the thread rest one second at a time
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// If the StartSecond of the stopwatch thread is falsh, break the loop
if(! isStartSecond) { isStartSecond =true;
break; }}}});Copy the code
6. Run and debug
After the last stopwatch thread is completed, the development of the entire electronic table is completed, and then the class can be run successfully by directly calling the class in the main function.
public static void main(String[] args) {
ElectronicClock eClock = new ElectronicClock();
}
Copy the code
The effect is as follows:
Feel good remember to like attention yo!