java - 我在 java 中的 endTime 有问题

标签 java windows user-interface cmd

我正在尝试向我的 Java GUI 添加 StartTime 和 EndTime,因为将运行多个批处理文件,这将关闭一个 cmd,启动另一个 cmd,关闭此 cmd 并启动另一个直到执行所有批处理文件(批处理文件将根据用户的选项运行)。

当用户点击“开始”选项时,批处理文件将运行。然后 StartTime 已被记录但不会显示,直到所有 cmd 关闭(这是我实现后发现的)。 当进程结束时,显示的 StartTime 是正确的。但是显示的结束时间与开始时间相同,这是错误的。我该如何解决?

这是我的代码:

// Get system time
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

// startTime GUI texts
JLabel startTimeLabel = new JLabel("Start Time: ");
startTimeLabel.setFont(new Font("Arial", Font.BOLD, 12));
startTimeLabel.setBounds(100, 245, 180, 53); 
contentPane.add(startTimeLabel);
JStartTimeTextField = new JTextField(); 
JStartTimeTextField.setBounds(170,265,200,15);
contentPane.add(JStartTimeTextField);
JStartTimeTextField.setColumns(10);

// endTime label
JLabel endTimeLabel = new JLabel(" End Time: ");
endTimeLabel.setFont(new Font("Arial", Font.BOLD, 12));
endTimeLabel.setBounds(100, 265, 180, 53);
contentPane.add(endTimeLabel);
JEndTimeTextField = new JTextField(); 
JEndTimeTextField.setBounds(170,285,200,15);
contentPane.add(JEndTimeTextField);
JEndTimeTextField.setColumns(10);

//when start button is selected
btnStart.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent args)
    {
        //StartTime
        JStartTimeTextField.setText(dateFormat.format(date)); 

        try 
        {   
            //create new process    
            String[] command = new String[]{"cmd", "/c", "start", "/wait", DetectDrive+"\\Starting.bat", filePath}; 

            //run process
            Process p = Runtime.getRuntime().exec(command);

            //cause this process to stop until process p is terminated
            p.waitFor();
        } 
        catch (IOException | InterruptedException e1)
        {
            e1.printStackTrace();
        }

        if(checkbox.isSelected())
        {
            try
            {   
                String[] command = new String[]{"cmd", "/c", "start", "/wait", DetectDrive+"\\Stage1.bat", filePath}; 
                Process p = Runtime.getRuntime().exec(command);
                p.waitFor();
            } 
            catch (IOException | InterruptedException e1)
            {
                e1.printStackTrace();
            }
        }

        ... (more coding on checkbox options)

        //The processes end
        JEndTimeTextField.setText(dateFormat.format(date));
    }
});

我发现我的编码看起来不错。但是然后.. 为什么当所有进程结束时 StartTime 和 EndTime 相同?

最佳答案

//获取系统时间 日期 date = new Date(); DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

您正在创建 1 个日期变量并使用它: 此处:JStartTimeTextField.setText(dateFormat.format(date)); 和 这里 JEndTimeTextField.setText(dateFormat.format(date));

当您初始化日期时,它不会随着时间的推移而改变。

要使此代码按预期工作,您需要在程序结束时获取新的日期和时间,而不是使用在开始时初始化的 once。

//进程结束 日期 newDate = new Date(); JEndTimeTextField.setText(dateFormat.format(newDate));

关于java - 我在 java 中的 endTime 有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25299489/

相关文章:

java - 为什么 XPath last() 函数不能像我预期的那样工作?

java - Eclipse 插件外部资源

c++ - C++ express下搭建POCO具体说明

java - maven-jaxb2-plugin 不在 linux 机器上编译 xsd 但在 windows 上工作?

java - 如何在Swing应用程序中实现JProgressBar?

java - 无法解析的日期

java - 如何使用java读取文本文件中的最后一行

windows - IntelliJ IDEA 终端特殊字符

c++ - 在 C++ 中编程 GUI

c# - Windows 用户界面自动化