2 louissisi Louissisi 于 2015.06.03 22:05 提问

求助动态时钟的应用程序,时钟不动

package Java_10;

import java.awt.Color;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Clockone extends JFrame implements Runnable {
private JPanel JP = new JPanel();
private Color C1 = new Color(123, 154, 45);
private SimpleDateFormat FM = new SimpleDateFormat(
"yyyy EEE MM dd HH:mm:ss");
private Date DT = new Date();
private Calendar C = Calendar.getInstance();
private int s, m, h;
private int XC = 101, YC = 101;
private int xs, ys, xm, ym, xh, yh;
private JLabel JL = new JLabel();

public Clockone(String title) {
    super(title);
    setLayout(null);
    add(JP);
    JP.setBounds(0, 0, 300, 300);
    JP.setLayout(null);
    JP.add(JL);
    JL.setBounds(50, 50, 100, 50);

}

public void paint(Graphics g) {
    g.setColor(Color.BLUE);
    g.drawOval(50, 50, 100, 100);
    g.drawOval(100, 100, 5, 5);
    g.fillOval(XC, YC, 3, 3);
    g.drawString("3", 143, 105);
    g.drawString("6", 98, 150);
    g.drawString("9", 51, 105);
    g.drawString("12", 95, 62);
    g.drawString("北京时间:" + String.valueOf((FM.format(DT))), 20, 200);
    setBackground(C1);
    s = C.get(Calendar.SECOND);
    m = C.get(Calendar.MINUTE);
    h = C.get(Calendar.HOUR_OF_DAY);
    xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + XC);
    ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + YC);
    xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + XC);
    ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + YC);
    xh = (int) (Math.cos((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + XC);
    yh = (int) (Math.sin((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + YC);
    g.drawLine(xs, ys, XC, YC);
    g.drawLine(xm, ym, XC - 1, YC);
    g.drawLine(xm, ym, XC, YC - 1);
    g.drawLine(xh, yh, XC - 1, YC);
    g.drawLine(xh, yh, XC, YC - 1);
}

public void run() {
    while (true) {
        try {
            Thread.currentThread().sleep(100);
        } catch (InterruptedException e) {
        }
        repaint();
    }
}

public static void main(String[] args) {

    Clockone CO = new Clockone("Clock");
    CO.setSize(300, 300);
    CO.setVisible(true);
    Thread thread = new Thread(CO);
    thread.start();
    thread.setPriority(MAXIMIZED_BOTH);
}

}

1个回答

tongyi55555
tongyi55555   2015.06.03 22:19
已采纳

核心问题,你的日期没有更新,其他问题,每次绘制前先清除以前的再绘制比较好,改好的代码如下:

 import java.awt.Color;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Clockone extends JFrame implements Runnable {
    /**
     * 
     */
    private static final long serialVersionUID = 8754113577355207407L;
    private JPanel JP = new JPanel();
    private Color C1 = new Color(123, 154, 45);
    private SimpleDateFormat FM = new SimpleDateFormat(
            "yyyy EEE MM dd HH:mm:ss");
    private Date DT = new Date();
    private Calendar C = Calendar.getInstance();
    private int s, m, h;
    private int XC = 101, YC = 101;
    private int xs, ys, xm, ym, xh, yh;
    private JLabel JL = new JLabel();

    public Clockone(String title) {
        super(title);
        setLayout(null);
        add(JP);
        JP.setBounds(0, 0, 300, 300);
        JP.setLayout(null);
        JP.add(JL);
        JL.setBounds(50, 50, 100, 50);

    }

    public void paint(Graphics g) {
        g.clearRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(Color.BLUE);
        g.drawOval(50, 50, 100, 100);
        g.drawOval(100, 100, 5, 5);
        g.fillOval(XC, YC, 3, 3);
        g.drawString("3", 143, 105);
        g.drawString("6", 98, 150);
        g.drawString("9", 51, 105);
        g.drawString("12", 95, 62);
        g.drawString("北京时间:" + String.valueOf((FM.format(DT))), 20, 200);
        setBackground(C1);
        s = C.get(Calendar.SECOND);
        m = C.get(Calendar.MINUTE);
        h = C.get(Calendar.HOUR_OF_DAY);
        xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + XC);
        ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + YC);
        xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + XC);
        ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + YC);
        xh = (int) (Math.cos((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + XC);
        yh = (int) (Math.sin((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + YC);
        g.drawLine(xs, ys, XC, YC);
        g.drawLine(xm, ym, XC - 1, YC);
        g.drawLine(xm, ym, XC, YC - 1);
        g.drawLine(xh, yh, XC - 1, YC);
        g.drawLine(xh, yh, XC, YC - 1);
    }

    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
                C = Calendar.getInstance();
                DT = C.getTime();
            } catch (InterruptedException e) {
            }
            repaint();
        }
    }

    public static void main(String[] args) {

        Clockone CO = new Clockone("Clock");
        CO.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CO.setLocationRelativeTo(null);
        CO.setSize(300, 300);
        CO.setVisible(true);
        Thread thread = new Thread(CO);
        thread.start();
        thread.setPriority(MAXIMIZED_BOTH);
    }
}
Louissisi
Louissisi 非常感谢你!
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!