I want to make a simple stopwatch (in the case of Shildt, the Calendar is used and the handler is tucked into the class with the buttons). I looked at the listing and decided to modify it.
The error takes off:
Exception in thread "java.lang.Error: Unresolved compilation problem: at Nanotime.main (Nanotime.java:24)"
import java.awt.FlowLayout; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class Nanotime { public static JLabel jlbl; public Nanotime() { JFrame jfrm = new JFrame("Секундомір"); jfrm.setBounds(500, 100, 200, 300); JButton Startbutton = new JButton("Start"); JButton Stopbutton = new JButton("Stop"); Nano_listener NL = new Nano_listener(); Startbutton.addActionListener(NL); Stopbutton.addActionListener(NL); jfrm.setLayout(new FlowLayout()); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel jlbl=new JLabel("Press 'Start'"); jlbl.setText("Press 'Start'"); } public static void main(String[] args) { //Ошибка тут SwingUtilities.invokeLater(new Runnable() { public void run() { new Nanotime(); } }); } }
Another file:
import java.awt.event.*; public class Nano_listener implements ActionListener { long l_temp; public void actionPerformed(ActionEvent ae) { if (ae.getActionCommand().equals("Start")) { l_temp = System.nanoTime(); Nanotime.jlbl.setText("Counting..."); } else { long l_result = System.nanoTime() - l_temp; Nanotime.jlbl.setText(String.valueOf(l_result)); l_temp = 0; l_result = 0; } } }
Please do not take it as a task for me, because I wrote all the code myself