2012-06-07 22 views
2

我正在創建一種待辦事項列表應用程序。一方面,你有待辦事項列表應用程序,其中可以添加任務的框,另一方面,你有一個數字時鐘。待辦事項列表完美地工作和顯示。另一方面,時鐘根本不顯示。當我在一個測試程序中自己實例化一個Clock對象時,它完美運行,但是當我嘗試在帶有待辦事項列表的JFrame中實例化它時,它根本不顯示。我檢查過我的驅動程序,併爲我的時鐘對象創建了實例和聲明。我究竟做錯了什麼?顯示javax.swing組件的問題,特別是JPanel

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()))); 
    } 
} 
+0

你在哪裏你clockpanel添加到您的JFrame? – assylias

+0

爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

+0

另外,爲什麼兩個類都有'Timer'? –

回答

4

clockLabel從不添加到任何組的佈局。這個SSCCE修復了這個問題。

ClockPanel

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()); 
      } 
     }); 
    } 
} 
+3

@Linda Lay:'clockLabel.setVisible(true);'或者'JComponent'的任何其他'setVisible()'調用不會使它們會出現,並將它們添加到Swing的組件層次結構中(http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html#general) – Asaf