简体   繁体   English

JLabel setText()方法不起作用

[英]JLabel setText() Method doesn't work

This code should create a frame, which contains a Label, that prints out a clock. 此代码应创建一个框架,其中包含一个可打印出时钟的Label。 The method zeitLaeuft() makes the clock work and with the Button 'start' the clock starts to run. 方法zeitLaeuft()使时钟工作,并通过“启动”按钮开始运行时钟。 When I call the method zeitLaeuft() the code fails. 当我调用zeitLaeuft()方法时,代码将失败。 I've tried a few things and now I know it's because of the Label jLUhr . 我已经尝试了一些方法,现在我知道是由于Label jLUhr In the method zeitLaeuft() the two orders which call the method jLUhr.setText() fail. 在该方法中zeitLaeuft()这两个命令,其调用方法jLUhr.setText()失败。 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
    }

}

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. 这是访问全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM