2

私は一種のto-doリストアプリケーションを作成しています。一方には、タスクを追加できるボックスを備えた To Do リスト アプリケーションがあり、もう一方にはデジタル時計があります。やることリストは機能し、完全に表示されます。一方、時計はまったく表示されません。テスト プログラムで Clock オブジェクトを単独でインスタンス化すると、完全に実行されますが、to-do リストを使用して JFrame 内でインスタンス化しようとすると、単に表示されません。ドライバーをチェックしたところ、クロック オブジェクトのインスタンス化と宣言が行われました。私は何を間違っていますか?

ClockPanel オブジェクト:

 import java.awt.Color;
 import java.awt.Font;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import javax.swing.Timer;

 public class ClockPanel extends javax.swing.JPanel implements ActionListener{
    private SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    private Timer timer;
    private javax.swing.JLabel clockLabel = new javax.swing.JLabel();
    public ClockPanel() {
     super();

     clockLabel.setText(sdf.format(new Date(System.currentTimeMillis())));
     clockLabel.setFont(new Font("Monospaced", Font.BOLD, 100));
     clockLabel.setOpaque(true);
     clockLabel.setBackground(Color.black);
     clockLabel.setForeground(Color.white);

     timer = new Timer(500, this);
     timer.setRepeats(true);
     timer.start();

     clockLabel.setVisible(true);

     initComponents();
  }
  @Override
  public void actionPerformed(ActionEvent e){
     if(e.getSource().equals(timer))
        clockLabel.setText(sdf.format(new Date(System.currentTimeMillis())));
  }
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
     this.setLayout(layout);
     layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
        );
     layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
        );
  }// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
 }

時計オブジェクト自体:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Clock extends JLabel implements ActionListener{
  private SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
  private Timer timer;
  public Clock(){
     super();

     setText(sdf.format(new Date(System.currentTimeMillis())));
     setFont(new Font("Monospaced", Font.BOLD, 100));
     setOpaque(true);
     setBackground(Color.black);
     setForeground(Color.white);

     timer = new Timer(500, this);
     timer.setRepeats(true);
     timer.start();

     setVisible(true);
  }
  public void actionPerformed(ActionEvent e){
     if(e.getSource().equals(timer))
        setText(sdf.format(new Date(System.currentTimeMillis())));
  }
 }
4

1 に答える 1

4

clockLabelレイアウトのどのグループにも追加されません。このSSCCEはそれを修正します。

クロックパネル

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.*;
import javax.swing.GroupLayout.ParallelGroup;

public class ClockPanel extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;
    private SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    private Timer timer;
    private JLabel clockLabel = new JLabel("Ha Ha");

    public ClockPanel() {
        super();

        clockLabel.setText(sdf.format(new Date(System.currentTimeMillis())));
        clockLabel.setFont(new Font("Monospaced", Font.BOLD, 100));
        clockLabel.setOpaque(true);
        clockLabel.setBackground(Color.black);
        clockLabel.setForeground(Color.white);

        timer = new Timer(500, this);
        timer.setRepeats(true);
        timer.start();

        initComponents();
    }

    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getSource().equals(timer))
            clockLabel.setText(sdf.format(
                new Date(System.currentTimeMillis())));
    }

    private void initComponents() {

        GroupLayout layout = new GroupLayout(this);
        ParallelGroup parallelGroupH = layout.createParallelGroup(
                GroupLayout.Alignment.LEADING);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                parallelGroupH
                .addGap(0, 400, Short.MAX_VALUE)
                );
        ParallelGroup parallelGroupV = layout.createParallelGroup(
                GroupLayout.Alignment.LEADING);
        layout.setVerticalGroup(
                parallelGroupV
                .addGap(0, 30, Short.MAX_VALUE)
                );
        parallelGroupH.addComponent(clockLabel);
        parallelGroupV.addComponent(clockLabel);
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(null, new ClockPanel());
            }
        });
    }
}
于 2012-06-07T18:01:45.413 に答える