我正在研究一些项目,以及它的一部分 - 数字时钟。我希望那个时间像:17:3:5会出现像17:03:05。我的代码在下面(工作)。但是有些问题:我不确定我是否使用了正确的计时器,而且我的程序看起来很复杂,也许你可以帮助简化代码或者给出一些想法如何以简单的方式编写它,谢谢。 / p>
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);
}
}
}
答案 0 :(得分:3)
格式化日期是SimpleDateFormat
的目的:
DateFormat format = new SimpleDateFormat("hh:mm:ss");
String stringTime = format.format(new Date());
答案 1 :(得分:1)
其他答案已对您代码的其他部分发表了评论。
要使显示屏居中,您可以使用以下图纸代码。
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);
答案 2 :(得分:0)
完成修改后的课程:
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);
}
}
}