0

我正在做一个数字时钟项目,我要制作一个看起来像这样的时钟:

数码时钟

一帧中有9个面板。一个班的小组,TitlePanel班的6个,DigitPanel班的2个ColonPanel。我遇到的问题出在DigitPanel课堂上。我基本上已经完成了这件事,但是当我执行程序时,由于setDigit某种原因该方法不会覆盖数字的默认值。

有人可以告诉我我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);
    }
}

数字面板:

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

1 回答 1

1
  1. JLabelinDigitPanel应该是一个类级别的变量。
  2. setDigit应该设置标签的文本属性

.

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);
    }
}
于 2013-03-12T04:51:33.970 回答