0

(extends JLabel)JPanel の JTextPane にストップウォッチを配置したいのですが、表示されません :/ class の object から classに objetct を配置できるかどうかわかりません(extends JPanel)。たぶん、これが理由です。

私のストップウォッチクラス:

    public class StopWatch extends JLabel implements ActionListener {

    private DecimalFormat df = new DecimalFormat("0.0");
    private Timer timer = new javax.swing.Timer(100, this);
    private long now = System.currentTimeMillis();

    public StopWatch() {
        this.setText(when());
    }

    public void actionPerformed(ActionEvent ae) {
        setText(when());
    }

    public void start() {
        timer.start();
    }

    public void stop() {
        timer.stop();
    }

    private String when() {
        return df.format((System.currentTimeMillis() - now) / 1000d);
    }  
}

これが私がそれを使用する方法です:

    public class Tlo extends JPanel {  
        ....       
        StopWatch stopwatch = new StopWatch();
        JTextPane time = new JTextPane();
        time.setFont(new Font("Arial", Font.PLAIN, 28));  
        time.setBounds(135,598,115,34);
        time.setBackground(new Color(182, 221, 232));
        time.setEditable(false);
        time.add(stopwatch);
        add(time);
        stopwatch.start(); 
        ...     
    }

どうすれば修正できますか?

4

1 に答える 1

0

問題は、クラスJTextPaneから継承している間、 addメソッドがのドキュメントにコンポーネントを追加しないことです。これらのドキュメント コンポーネントのみがレンダリングされます。ContainerJTextComponent

交換:

time.add(stopwatch);

time.insertComponent(stopwatch);
于 2013-01-19T16:10:30.427 に答える