刷新JLabel

时间:2017-02-22 02:29:47

标签: java swing jframe

我试图以JFrame显示当前时间。如何更新JLabel中的文本,而无需在每次更新时打开单独的框架?

到目前为止我的所有代码......

Test

public class Test{

    static String timeDisplay = "";

    public static class time extends Thread{
        static int timeHours = 7;
        static int timeMins = 30;
        static int timeSecs = 0;
        @Override
        public void run(){

            while(true){
                try{

                    time.sleep(1000);
                    timeSecs++;

                    if(timeSecs == 60){
                        timeMins++;
                        timeSecs = 0;
                    }
                    if(timeMins == 60){
                        timeHours++;
                        timeMins = 0;
                    }

                    if(timeHours < 10){
                        if(timeMins < 10){
                            if(timeSecs < 10){
                                timeDisplay = "0" + timeHours + ":" + "0" + timeMins + ":" + "0" + timeSecs;
                            }
                            else{
                                timeDisplay = "0" + timeHours + ":" + "0" + timeMins + ":" + timeSecs;
                            }
                        }
                        else{
                            if(timeSecs < 10){
                                timeDisplay = "0" + timeHours + ":" + timeMins + ":" + "0" + timeSecs;
                            }
                            else{
                                timeDisplay = "0" + timeHours + ":" + timeMins + ":" + timeSecs;
                            }
                        }
                    }
                    else{
                        if(timeMins < 10){
                            if(timeSecs < 10){
                                timeDisplay = timeHours + ":" + "0" + timeMins + ":" + "0" + timeSecs;
                            }
                            else{
                                timeDisplay = timeHours + ":" + "0" + timeMins + ":" + timeSecs;
                            }
                        }
                        else{
                            if(timeSecs < 10){
                                timeDisplay = timeHours + ":" + timeMins + ":" + "0" + timeSecs;
                            }
                            else{
                                timeDisplay = timeHours + ":" + timeMins + ":" + timeSecs;
                            }
                        }
                    }

                    System.out.println(timeDisplay);
                    //CountDown time = new CountDown(timeDisplay);


                }
                catch(Exception e){
                    System.out.println("Something went wrong :(");
                }
            }

        }

    }

    public static void main(String[] args){

        time time = new time();
        time.start();
            try {
                TimeUnit.SECONDS.sleep(1);
                CountDown window = new CountDown(timeDisplay);
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                window.setSize(500, 500);
                window.setVisible(true);
            }
            catch (Exception e) {
                e.printStackTrace();
            }

    }

}

CountDown

public class CountDown extends JFrame{

    private static final long serialVersionUID = 1L;

    static JLabel label = new JLabel();

    public CountDown(String time){

        super("Title");
        setLayout(new FlowLayout());

        add(label);
        label.setText("Current Time: " + time);

        Handler eventHandler = new Handler();

    }

    private class Handler implements ActionListener{

        public void actionPerformed(ActionEvent event){

            String string = "";

            if(event.getSource()==""){
                string = String.format("label 1: %s", event.getActionCommand());
            }

        }

    }

}

我对这个程序的意图是制作一个显示当前时间的框架。它使用程序中的当地时间,而不是实际时间。在此先感谢,如果我应该更改代码中的任何内容以使其更好,请随时告诉我。

2 个答案:

答案 0 :(得分:0)

API文档将成为您的朋友。请查看JLabel的文档,尤其是方法setText()。您可以在事件处理程序中再次使用此方法来更改标签的文本。

但是你遇到的另一个问题是你既没有解雇也没有注册事件,因此你所写的actionPerformed函数永远不会被调用。如果第二准确度足够好,您可以使用javax.swing.Timer大大简化代码。如果不在不必要的地方更改代码,这应该会让您走上正确的道路:

public class CountDown extends JFrame{

    private static final long serialVersionUID = 1L;

    private JLabel label = new JLabel(); // note: understand static keyword before using it.

    private long startTime = System.currentTimeMillis(); // gets the current time in milliseconds, when your class is initialized.

    public CountDown(String time){

        super("Title");
        setLayout(new FlowLayout());

        add(label);
        // label.setText("Current Time: " + time); "time" was never in scope here.

        Handler eventHandler = new Handler();
        new Timer(1000, eventHandler).start(); // will execute ~1/sec
    }

    private class Handler implements ActionListener{

        public void actionPerformed(ActionEvent event){

              // NOTE: here, you could put all of your logic that was previously
              // in your Thread to determine the time, then use the result
              // with label.setText();
              long currentTime = System.currentTimeMillis();

              long upTime = currentTime - startTime; // this is how many milliseconds your JFrame has been up and running.

              // TODO: formate upTime however you desire.

              label.setText( <whatever_you_calculate_directly_above> );
            }

        }

    }

}

您还需要重写main方法来初始化Frame。应该这样做。

public static void main( String[] args){
  JFrame frame = new CountDown(); 
  frame.setVisible(true);
}

如果您的main方法位于单独的文件中,则需要在该文件的开头附近导入CountDown。

答案 1 :(得分:0)

首先应创建CountDown的实例,然后调用其中一个设置标签文本的方法。

CountDown中你必须调用JLabel.setText。设置标签文本的方法:

public void displayTime(String time){
   label.setText("Current Time: " + time);
}

...而不是System.out.println(time)致电

time.displayTime(timeDisplay);

您最好使用java.util.Timer,或者更好地使用更适合gui对象的javax.swing.Timer,而不是使用简单的线程。

另一个评论:类名以大写字母开头,而方法或变量以小写方法开头。

最后你的处理程序在这里做什么。你没有注册它,似乎没用。