我想刷新我的Java框架! (制作时钟节目)

时间:2018-06-09 18:23:25

标签: java swing user-interface awt

我想使用java语言刷新我的Frame程序。

看看这张图片:

enter image description here

我想像这样更新小时和分钟值:

enter image description here

但我不知道如何使用update()函数和repaint()函数。

这是我的源代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class MyGraphic extends JComponent {
    public int h = 2;
    public int m = 42;
    public String time = "Time -> " + Integer.toString(h) + " : " + Integer.toString(m);

    public void update(Graphics g) {    //this method is called by repaint() Method.
        m = m + 1;
        if(m == 60) {
            h += 1;
            m = 0;
        }
    } //how can I call this method? I want to update my int type variables!

    public void paintComponent(Graphics g) {
        //only for "write time"
        //I erased source of Drawing clock image. it is long.

        Font strFont = new Font("TimesRoman", Font.BOLD, 19);
        g.setFont(strFont);
        g.drawString(time, 20, 300);
        this.repaint();    
        //I thought this.repaint() Method will call update() Method, 
        //but It didn't work...
    }
}
//main thread
public class mainsrc {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int out = 0;
        JFrame frame = new JFrame();    
        MyGraphic mg = new MyGraphic();
        //creating object of JFrame and MyGraphic.

        final int FRAME_WIDTH = 300;
        final int FRAME_HEIGHT = 400;
        //declaring WIDTH and HEIGHT.

        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setTitle("2014305034 This is Clock");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //ETC doing Settings

        while(true) {
            frame.add(mg);
            frame.setVisible(true); 
        }       
    }

}

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

您可以使用Timer和TimerTask等任务调度程序类在一定时间间隔后调用更新函数。

答案 1 :(得分:0)

以下是更新属性和绘制'时间的实例。有关详细信息,请参阅代码中的注释。

代码中仍然存在大量错误。 BNI。

import javax.swing.*;
import java.awt.*;

class MyGraphic extends JComponent {

    public int h = 2;
    public int m = 42;
    // better done as a method? 
    //public String time = "Time -> " + Integer.toString(h) + " : " + Integer.toString(m);

    // DO NOT MESS WITH THIS METHOD! 
    /*
    public void update(Graphics g) {    //this method is called by repaint() Method.
        m = m + 1;
        if (m == 60) {
            h += 1;
            m = 0;
        }
    } //how can I call this method? I want to update my int type variables!
    */

    private void updateAttributes() {
        m = m + 1;
        if (m == 60) {
            h += 1;
            m = 0;
        }
    }

    private String getTime() {
        return "Time -> " + Integer.toString(h) + " : " + Integer.toString(m);

    }

    public void paintComponent(Graphics g) {
        //only for "write time"
        //I erased source of Drawing clock image. it is long.

        Font strFont = new Font("TimesRoman", Font.BOLD, 19);
        g.setFont(strFont);
        g.drawString(getTime(), 20, 300);
        updateAttributes(); // THIS is how you can call method to update them
        this.repaint();
        //I thought this.repaint() Method will call update() Method, 
        //but It didn't work...
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        MyGraphic mg = new MyGraphic();
        //creating object of JFrame and MyGraphic.

        final int FRAME_WIDTH = 300;
        final int FRAME_HEIGHT = 400;
        //declaring WIDTH and HEIGHT.

        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setTitle("2014305034 This is Clock");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //ETC doing Settings

        // while (true) { // WHAT THE..?
            frame.add(mg);
            frame.setVisible(true);
        //}
    }
}

答案 2 :(得分:0)

哇......我使用Thread和Success作为我的项目!

这是我的主题:

class MyThread extends Thread {
    public void run(MyGraphic mg) {
        while(true) {
            try {
                mg.m = mg.m + 1;
                System.out.println(mg.m);
                sleep(1000);
            } catch(InterruptedException e) {
                System.out.println("Error is occured: " + e.getMessage());
            } 

        }
    }
}

并在主src:

frame.add(mg);
frame.setVisible(true);         
t1.run(mg);
哈哈哈,感谢给我使用线程计时器的想法!