我正在做一个数字时钟项目,我要制作一个看起来像这样的时钟:
一帧中有9个面板。 TitlePanel
类的一个面板,DigitPanel
类的6个面板和ColonPanel
类的2个面板。我遇到的问题是DigitPanel
类。我已经基本完成了这件事,但是当我执行程序时,出于某些原因,setDigit
方法不会覆盖digit的默认值。
有人可以告诉我DigitPanel
类的方法有什么问题吗?
主班:
public class DigitPane extends JPanel {
DigitPanel[] hourDigit = new DigitPanel[]{new DigitPanel(), new DigitPanel()};
DigitPanel[] minDigit = new DigitPanel[]{new DigitPanel(), new DigitPanel()};
DigitPanel[] secDigit = new DigitPanel[]{new DigitPanel(), new DigitPanel()};
private String hour = pad(Calendar.getInstance().get(Calendar.HOUR));
private String min = pad(Calendar.getInstance().get(Calendar.MINUTE));
private String sec = pad(Calendar.getInstance().get(Calendar.SECOND));
private char[] hours = hour.toCharArray();
private char[] minutes = min.toCharArray();
private char[] seconds = sec.toCharArray();
public DigitPane() {
JPanel digitsPanel = new JPanel();
digitsPanel.setLayout(new BoxLayout(digitsPanel, BoxLayout.X_AXIS));
digitsPanel.setSize(new Dimension(605, 100));
digitsPanel.add(new panelPadding());
hourDigit[0].setDigit(hours[0]);
digitsPanel.add(hourDigit[0]);
digitsPanel.add(new panelPadding());
hourDigit[1].setDigit(hours[1]);
digitsPanel.add(hourDigit[1]);
digitsPanel.add(new panelPadding());
digitsPanel.add(new ColonPanel());
digitsPanel.add(new panelPadding());
minDigit[0].setDigit(minutes[0]);
digitsPanel.add(minDigit[0]);
digitsPanel.add(new panelPadding());
minDigit[1].setDigit(minutes[1]);
digitsPanel.add(minDigit[1]);
digitsPanel.add(new panelPadding());
digitsPanel.add(new ColonPanel());
digitsPanel.add(new panelPadding());
secDigit[0].setDigit(seconds[0]);
digitsPanel.add(secDigit[0]);
digitsPanel.add(new panelPadding());
secDigit[1].setDigit(seconds[1]);
digitsPanel.add(secDigit[1]);
digitsPanel.add(new panelPadding());
this.add(digitsPanel);
}
protected String pad(int value) {
StringBuilder sb = new StringBuilder(String.valueOf(value));
while (sb.length() < 2) {
sb.insert(0, "0");
}
return sb.toString();
}
public static void main(String[] args) {
new DigitalTimeUI();
}
public class panelPadding extends JPanel {
public panelPadding() {
this.setPreferredSize(new Dimension(5, 80));
}
}
}
标题面板:
public class TitlePanel extends JPanel {
JLabel title;
public TitlePanel() {
this.setBackground(Color.WHITE);
this.setPreferredSize(new Dimension(605, 100));
this.setLayout(new GridBagLayout());
title = new JLabel("DIGITAL CLOCK");
title.setFont(new Font("Tahoma", Font.BOLD, 72));
title.setForeground(Color.BLACK);
this.add(title);
}
}
DigitPanel:
public class DigitPanel extends JPanel {
private char digit = '0';
public DigitPanel() {
JLabel label = new JLabel(String.valueOf(digit));
label.setFont(new Font("Tahoma", Font.BOLD, 72));
label.setForeground(Color.WHITE);
this.setLayout(new GridBagLayout());
this.setPreferredSize(new Dimension(80, 80));
this.setBackground(Color.BLACK);
this.add(label);
}
public final char getDigit() {
return digit;
}
public void setDigit(char digit) {
this.digit = digit;
}
}
最佳答案
JLabel
中的DigitPanel
应该是类级别的变量。setDigit
应设置标签的text属性
。
public class DigitPanel extends JPanel {
private JLabel label = new JLabel(String.valueOf(digit));
public DigitPanel() {
label = new JLabel(String.valueOf(0));
label.setFont(new Font("Tahoma", Font.BOLD, 72));
label.setForeground(Color.WHITE);
this.setLayout(new GridBagLayout());
this.setPreferredSize(new Dimension(80, 80));
this.setBackground(Color.BLACK);
this.add(label);
}
public final char getDigit() {
return label.getText().charAt(0);
}
public void setDigit(char digit) {
label.setText(Character.toString(digit);
}
}