0

This code should create a frame, which contains a Label, that prints out a clock. The method zeitLaeuft() makes the clock work and with the Button 'start' the clock starts to run. When I call the method zeitLaeuft() the code fails. I've tried a few things and now I know it's because of the Label jLUhr. In the method zeitLaeuft() the two orders which call the method jLUhr.setText() fail. I've tried to set the Label text and commented out the method but it doesn't work.

What is the problem?

package uhr;

import javax.swing.*;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;

public class Uhr1 extends javax.swing.JFrame {

    public Uhr1() {
        super();
        initGUI();
    }

    private static JLabel jLUhr;
    private static JButton jBtnStart;
    private static int stunden = 0, minuten = 0;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Uhr1 uhr = new Uhr1();
                uhr.setVisible(true);
                uhr.setLocationRelativeTo(null);
            }
        });

    }

    public void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            this.setTitle("Uhr");
            getContentPane().setLayout(null);

            {
                JLabel jLUhr = new JLabel(); //Uhr = clock in german
                add(jLUhr);
                jLUhr.setBounds(49, 89, 300, 100);
                jLUhr.setHorizontalAlignment(SwingConstants.CENTER);
                jLUhr.setVerticalAlignment(SwingConstants.CENTER);

            }

            {
                JButton jBtnStart = new JButton();
                add(jBtnStart);
                jBtnStart.setBounds(49, 219, 80, 30);
                jBtnStart.setText("Start");
                jBtnStart.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        start(evt);
                    }
                });
            }

            pack();
            setSize(400,300);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void zeitLaeuft() {

        while(true) {

                if(minuten < 60) {
                    int i;

                    try {
                        for(i = 0; i < 60; i++){
                        jLUhr.setText(Integer.toString(stunden) + " : " + Integer.toString(minuten));   //this is where the code fails
                        TimeUnit.SECONDS.sleep(1);
                        jLUhr.setText(Integer.toString(stunden) + "  " + Integer.toString(minuten));   //this is where the code fails
                        }
                        minuten++;
                        i = 0;
                    } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                else if(minuten == 60) {
                    minuten = 0;
                    if(stunden < 24) {  
                        stunden ++;
                        }
                    else {
                        stunden = 0;
                    }
                }
            }
    }

    public void start(ActionEvent evt) {
        zeitLaeuft(); //this is where the code fails
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ad_am
  • 23
  • 4
  • 1
    1) *"..the code fails.."* Always copy/paste error and exception output! 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Oct 08 '15 at 16:05
  • 1
    3) `getContentPane().setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 08 '15 at 16:06

1 Answers1

2

This:

JLabel jLUhr = new JLabel(); //Uhr = clock in german

...declares & creates a local variable whose scope is lost at the end of the method. It should be:

jLUhr = new JLabel(); //Uhr = clock in german

This is accesing the global variable.

SapuSeven
  • 1,473
  • 17
  • 30
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433