import java.awt.*; import java.awt.event.*; import java.time.LocalTime; import javax.swing.*; class KlokkePreKode { private static JLabel klokkeslett = new JLabel(naa()); private static String naa(){ //Henter den lokale tiden naa paa format "12:34:56" LocalTime t = LocalTime.now(); return String.format("%02d:%02d:%02d", t.getHour(), t.getMinute(), t.getSecond()); } public static void main(String[] args) { JFrame vindu = new JFrame("Klokke"); vindu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); vindu.add(panel); panel.add(klokkeslett); vindu.pack(); vindu.setVisible(true); class SekundTeller implements Runnable{ public void run(){ boolean fortsett = true; while(fortsett){ try{ Thread.sleep(1000); }catch(InterruptedException e){ fortsett = false; } klokkeslett.setText(naa()); } } } new Thread(new SekundTeller()).start(); } }