用swing创建数字时钟的Java

用swing创建数字时钟的Java,java,swing,clock,digital,Java,Swing,Clock,Digital,我正在做一个项目,也是其中的一部分——数字时钟。我希望时间像:17:3:5会像17:03:05那样出现。我的代码在下面(工作)。但是它有一些问题:我不确定我是否使用了正确的计时器,而且我的程序看起来很复杂,也许你可以帮助简化代码或者给出一些如何用简单的方式编写它的想法,谢谢 import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.aw

我正在做一个项目,也是其中的一部分——数字时钟。我希望时间像:17:3:5会像17:03:05那样出现。我的代码在下面(工作)。但是它有一些问题:我不确定我是否使用了正确的计时器,而且我的程序看起来很复杂,也许你可以帮助简化代码或者给出一些如何用简单的方式编写它的想法,谢谢

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.WindowConstants;

public class SimpleDigitalClock {

    public static void main(String[] args) {

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        DigitalClock myClock = new DigitalClock();
        f.add(myClock);
        f.pack();
        f.setVisible(true);
    }

    static class DigitalClock extends JPanel {

        String stringTime;
        int hour, minute, second;

        String correctionHour = "";
        String correctionMinute = "";
        String correctionSecond = "";

        public void setStringTime(String xyz) {
            this.stringTime = xyz;
        }

        public int findMinimumBetweenTwoNumbers(int a, int b) {
            return (a <= b) ? a : b;
        }

        DigitalClock() {

            Timer t1 = new Timer(1000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    repaint();
                }
            });
            t1.start();
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            Calendar now = Calendar.getInstance();
            hour = now.get(Calendar.HOUR_OF_DAY);
            minute = now.get(Calendar.MINUTE);
            second = now.get(Calendar.SECOND);

            if (hour < 10) {
                this.correctionHour = "0";
            }
            if (hour >= 10) {
                this.correctionHour = "";
            }

            if (minute < 10) {
                this.correctionMinute = "0";
            }
            if (minute >= 10) {
                this.correctionMinute = "";
            }

            if (second < 10) {
                this.correctionSecond = "0";
            }
            if (second >= 10) {
                this.correctionSecond = "";
            }
            setStringTime(correctionHour + hour + ":" + correctionMinute+ minute + ":" + correctionSecond + second);
            g.setColor(Color.BLACK);
            int length = findMinimumBetweenTwoNumbers(this.getWidth(),this.getHeight());
            Font myFont = new Font("SansSerif", Font.PLAIN, length / 5);
            g.setFont(myFont);
            g.drawString(stringTime, (int) length/6, length/2);

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.Calendar;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.Timer;
导入javax.swing.WindowConstants;
公共类SimpleDigitalClock{
公共静态void main(字符串[]args){
JFrame f=新的JFrame();
f、 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
DigitalClock myClock=新的DigitalClock();
f、 添加(myClock);
f、 包装();
f、 setVisible(真);
}
静态类数字时钟扩展JPanel{
字符串时间;
整小时,分,秒;
字符串更正时间=”;
字符串更正分钟=”;
字符串更正秒=”;
公共void setStringTime(字符串xyz){
this.stringTime=xyz;
}
两个数字之间的公共整数findminimumb(整数a、整数b){
返回(a=10){
this.correctionHour=“”;
}
如果(分钟<10){
此参数为.correctionMinute=“0”;
}
如果(分钟>=10){
this.correctionMinute=“”;
}
如果(秒<10){
此参数为.correctionSecond=“0”;
}
如果(秒>=10){
此项。更正秒=”;
}
设置字符串时间(校正小时+小时+”:“+校正分钟+分钟+”:“+校正秒+秒);
g、 设置颜色(颜色为黑色);
int length=findMinimumbetween两个数字(this.getWidth(),this.getHeight());
Font myFont=新字体(“SansSerif”,Font.PLAIN,length/5);
g、 setFont(myFont);
g、 抽绳(stringTime,(int)长度/6,长度/2);
}
@凌驾
公共维度getPreferredSize(){
返回新维度(200200);
}
}
}

格式化日期是
SimpleDateFormat
设计的目的:

DateFormat format = new SimpleDateFormat("hh:mm:ss");
String stringTime = format.format(new Date());

完全修改类:

static class DigitalClock extends JPanel {

    String stringTime;

    public void setStringTime(String xyz) {
        this.stringTime = xyz;
    }

    public int findMinimumBetweenTwoNumbers(int a, int b) {
        return (a <= b) ? a : b;
    }

    DigitalClock() {

        Timer t1 = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                repaint();
            }
        });
        t1.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Calendar now = Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);

        String correctionHour = (hour < 10) ? "0" : "":
        String correctionMinute = (minute < 10) ? "0" : "";
        String correctionSecond = (second < 10) ? "0" : "";
        setStringTime(correctionHour + hour + ":" + correctionMinute+ minute + ":" + correctionSecond + second);
        g.setColor(Color.BLACK);
        int length = findMinimumBetweenTwoNumbers(this.getWidth(),this.getHeight());
        Font myFont = new Font("SansSerif", Font.PLAIN, length / 5);
        g.setFont(myFont);
        g.drawString(stringTime, (int) length/6, length/2);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }
}
静态类数字时钟扩展JPanel{
字符串时间;
公共void setStringTime(字符串xyz){
this.stringTime=xyz;
}
两个数字之间的公共整数findminimumb(整数a、整数b){

return(a其他答案对代码的其他部分进行了注释

要居中显示,可以使用以下图形代码

        g.setColor(Color.BLACK);
        int length = Math.min(this.getWidth(), this.getHeight());
        Font myFont = new Font("SansSerif", Font.PLAIN, length / 5);
        g.setFont(myFont);
        Graphics2D g2d = (Graphics2D) g;
        FontMetrics fm = g2d.getFontMetrics();
        Rectangle2D r = fm.getStringBounds(stringTime, g2d);
        int x = (this.getWidth() - (int) r.getWidth()) / 2;
        int y = this.getHeight() / 2;
        g.drawString(stringTime, x, y);

可能codereview.stackexchange.com中的codereview更好?您可以使用SimpleDateFormat“HH:mm:ss”()来安排您的时间。在1行中。感谢大家的评论和回答,这很有帮助。只是想知道-调用new Date()会不会有点过火每秒钟?比我的答案更优雅。@TJ-你说得很对。创建对象会耗费应用程序的时间和CPU精力。垃圾收集和内存回收会耗费更多的时间和CPU精力。这里的一个优化可能是每分钟而不是每秒钟更新时间。在这种情况下,我想有一种方法可以使用本地计数器,并使用数学来显示时间。(假设时间仅由单个线程更新,则在应用程序启动后不会修改系统时间,并且其他任务的处理时间不重要)。如果处理时间很长,计数器将出错。是的,正如您所建议的,每分钟更新一次实际时间应该是可以接受的折衷方案。