程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-求助動態時鐘的應用程序,時鐘不動

java-求助動態時鐘的應用程序,時鐘不動

編輯:編程綜合問答
求助動態時鐘的應用程序,時鐘不動

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);
}

}

最佳回答:


核心問題,你的日期沒有更新,其他問題,每次繪制前先清除以前的再繪制比較好,改好的代碼如下:

 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);
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved