English 中文(简体)
java for differences in time
原标题:java for difference in time

below in the sample code which i wrote where the user gets an interface with 2 buttons. when the user click on start button the timer starts and when the end button is clicked the timer stops and the difference in time is displayed. But the difference in time is not being output:(

一个人能够帮助。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Timer2 extends JFrame {

    private JButton start;
    private JButton end;

    public Timer2() {
        super("Test Timer");
        setLayout(new FlowLayout());
        start = new JButton("START");
        add(start);
        end = new JButton("END");
        add(end);

        ButtonHandler handler = new ButtonHandler();
        start.addActionListener(handler);
        end.addActionListener(handler);
    }

    private class ButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            long s_time = 0;
            long e_time = 0;
            long diff = 0;
            String name = ((JButton) event.getSource()).getText();
            if (name.equals("start")) {
                s_time = System.currentTimeMillis();
            } else {
                e_time = System.currentTimeMillis();
            }
            diff = (e_time - s_time) / 1000;
            JOptionPane.showMessageDialog(null, diff);
        }
    }

    public static void main(String[] args) {
        Timer2 timer2 = new Timer2();
        timer2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        timer2.setSize(200, 200);
        timer2.setVisible(true);
    }
}
问题回答

您在“UPPERCASE”中给出了你的子群,但是在你的活动手里,你正在寻找较低的案例。

您也正在制定<条码>_time和e_time0, 载于<条码>内的代号(方法,系指将其编为<条码>0。 每次你点击。 这两条都需要在<代码>中成为领域。 ButtonHandler。

此外,请参阅<代码>。 JOptionPane.showMessageDialog() 将在你点击either纽芬兰时发射。

Edit: To solve the last problem, move your diff calculation and JOptionPane.showMessageDialog() call to inside the else block it follows; you only want it when the "end" button is pressed.

我假定,只有在用户点击“End”时,才会显示结果。 你写这封信的方式,将显示任何一种方式。 为此,将<代码>移至此处。 JOptionPane.showMessageDialog(null, diff) within the else .

另一种情况是,“Brian Roach”如此有益地解释,计算机非常敏感(例如“THUS”并不等于“thus”。) 因此,确保你提及正确的项目。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签