如果我正确理解了这个问题...
您在 OO 环境中工作。您应该尽可能将设计分解为最小的可管理工作单元。
对我来说,这意味着每个数字(或时间单位)都是最小的工作单位。这将需要一个能够简单地显示 0 填充的 int 值的组件。
从那里,您可以构建一个时钟窗格,使用 3 位数窗格,依此类推。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DigitalClock {
public static void main(String[] args) {
new DigitalClock();
}
public DigitalClock() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private DigitPane hour;
private DigitPane min;
private DigitPane second;
private JLabel[] seperator;
private int tick = 0;
public TestPane() {
setLayout(new GridBagLayout());
hour = new DigitPane();
min = new DigitPane();
second = new DigitPane();
seperator = new JLabel[]{new JLabel(":"), new JLabel(":")};
add(hour);
add(seperator[0]);
add(min);
add(seperator[1]);
add(second);
Timer timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Calendar cal = Calendar.getInstance();
hour.setValue(cal.get(Calendar.HOUR_OF_DAY));
min.setValue(cal.get(Calendar.MINUTE));
second.setValue(cal.get(Calendar.SECOND));
if (tick % 2 == 1) {
seperator[0].setText(" ");
seperator[1].setText(" ");
} else {
seperator[0].setText(":");
seperator[1].setText(":");
}
tick++;
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
}
public class DigitPane extends JPanel {
private int value;
@Override
public Dimension getPreferredSize() {
FontMetrics fm = getFontMetrics(getFont());
return new Dimension(fm.stringWidth("00"), fm.getHeight());
}
public void setValue(int aValue) {
if (value != aValue) {
int old = value;
value = aValue;
firePropertyChange("value", old, value);
repaint();
}
}
public int getValue() {
return value;
}
protected String pad(int value) {
StringBuilder sb = new StringBuilder(String.valueOf(value));
while (sb.length() < 2) {
sb.insert(0, "0");
}
return sb.toString();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
String text = pad(getValue());
FontMetrics fm = getFontMetrics(g.getFont());
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = ((getHeight()- fm.getHeight()) / 2) + fm.getAscent();
g.drawString(text, x, y);
}
}
}
更新
基本上你可以做类似...
String min = String.valueOf(Calendar.getInstance().get(Calendar.MINUTE));
char[] digits = min.toCharArray();