I wrote a piece of code in Java about a countdown function in GUI, but I found that when I wanted to use timers and threads, time could not be refreshed in the GUI. Sleep () {I know this will be a function in the main thread, but later I ran it. It is not, and I don’t understand}. So how do I implement this countdown to refresh constantly in the GUI? The code is as follows,I only wrote a function to display the system time:
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class CurrentTime { public String SystemDate(){ //获取系统默认时区当前时间 //Get the current time of the system default time zone LocalDateTime now_time = LocalDateTime.now(); //对时间进行相应的格式化 //Format the time accordingly DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh时mm分ss秒"); //返回对应的信息 //Return the corresponding information return now_time.format(ofPattern); } } ---------------------------------------------------------------------------------------------------- import javax.swing.*; import java.awt.*; public class MainGUI { public void showGUI(){ // 创建一个庄口JFrame名为:Target_Countdown //Create a portal JFrame named Target_ Countdown JFrame frame = new JFrame("Target_Countdown"); //设置关闭窗口时的默认操作 //Set the default action when closing the window frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗口标题 //Set Window Title frame.setTitle("目标倒计时"); //设置窗口尺寸 //Set window size frame.setSize(600,400); //设置窗口初始显示位置 //Set the initial display position of the window frame.setLocation(500,150); //800 450 //使窗口显示状态为 True //Make the window display state true frame.setVisible(true); //创建一个Jpanel面板组件 //Create a Jpanel panel component JPanel jpanel = new JPanel(); //从currentTime获得数据,并导入组件名为showArea的JTextfield中 //Get the data from currentTime and import it into the JTextfield named showArea CurrentTime currentTime = new CurrentTime(); String ymd_hms = currentTime.SystemDate(); //使用JLabel来显示当前时间,可与JTextField替换 //Use JLabel to display the current time, which can be replaced with JTextField JLabel showLabel = new JLabel(ymd_hms); showLabel.setFont(new Font("宋体",0,20)); //将showLabel放置在jpanel的顶部 //Place showLabel on the top of the jpanel jpanel.add(showLabel,BorderLayout.PAGE_START); frame.add(jpanel); } } ---------------------------------------------------------------------------------------------------- import javax.swing.*; public class Main { public static void main(String[] args) { //生成mainGUI实例 //Generate mainGUI instance MainGUI mainGUI = new MainGUI(); //使用SwingUtilities工具调用createAndShowGUI()方法 //调用mainGUI中的showGUI方法 此处如果不想实例化,此处可以把mainGUI 加 static修饰 或者把showGUI()方法加static修饰 //Use the SwingUtilities tool to call the createAndShowGUI () method //Call showGUI method in mainGUI. If you don't want to instantiate, you can add static decoration to mainGUI or static decoration to showGUI () method SwingUtilities.invokeLater(mainGUI::showGUI); } }
I hope to see a function of continuous countdown in the GUI, hope mogul can solve my problems ,sincerely say thanks for you!
Advertisement
Answer
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the How to Use Swing Timers section.
I added a Swing Timer
to your MainGUI
class. The code in the actionPerformed
method is executed every 200 milliseconds, which should be accurate enough for a clock that displays seconds.
Here’s the complete runnable code. I made the additional classes inner classes so I could post this code as one block.
import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.Timer; public class CountdownTimerGUI { public static void main(String[] args) { //生成mainGUI实例 //Generate mainGUI instance MainGUI mainGUI = new CountdownTimerGUI().new MainGUI(); //使用SwingUtilities工具调用createAndShowGUI()方法 //调用mainGUI中的showGUI方法 此处如果不想实例化,此处可以把mainGUI 加 static修饰 或者把showGUI()方法加static修饰 //Use the SwingUtilities tool to call the createAndShowGUI () method //Call showGUI method in mainGUI. If you don't want to instantiate, you can add static decoration to mainGUI or static decoration to showGUI () method SwingUtilities.invokeLater(mainGUI::showGUI); } public class MainGUI { public void showGUI(){ // 创建一个庄口JFrame名为:Target_Countdown //Create a portal JFrame named Target_ Countdown JFrame frame = new JFrame("Target_Countdown"); //设置关闭窗口时的默认操作 //Set the default action when closing the window frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗口标题 //Set Window Title frame.setTitle("目标倒计时"); //设置窗口尺寸 //Set window size frame.setSize(600,400); //设置窗口初始显示位置 //Set the initial display position of the window frame.setLocation(500,150); //800 450 //使窗口显示状态为 True //Make the window display state true frame.setVisible(true); //创建一个Jpanel面板组件 //Create a Jpanel panel component JPanel jpanel = new JPanel(); //从currentTime获得数据,并导入组件名为showArea的JTextfield中 //Get the data from currentTime and import it into the JTextfield named showArea CurrentTime currentTime = new CurrentTime(); String ymd_hms = currentTime.SystemDate(); //使用JLabel来显示当前时间,可与JTextField替换 //Use JLabel to display the current time, which can be replaced with JTextField JLabel showLabel = new JLabel(ymd_hms); showLabel.setFont(new Font("宋体",0,20)); //将showLabel放置在jpanel的顶部 //Place showLabel on the top of the jpanel jpanel.add(showLabel,BorderLayout.PAGE_START); frame.add(jpanel); Timer timer = new Timer(200, new ActionListener() { @Override public void actionPerformed(ActionEvent event) { String ymd_hms = currentTime.SystemDate(); showLabel.setText(ymd_hms); } }); timer.start(); } } public class CurrentTime { public String SystemDate(){ //获取系统默认时区当前时间 //Get the current time of the system default time zone LocalDateTime now_time = LocalDateTime.now(); //对时间进行相应的格式化 //Format the time accordingly DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh时mm分ss秒"); //返回对应的信息 //Return the corresponding information return now_time.format(ofPattern); } } }