2014-02-20 21 views
0

總體目標是創建一個靜態數字時鐘,以顯示當前時間。我得到了我想要的標題,但我無法得到適當的結腸。 這是我的框架代碼。在框架中繪製面板,我有標題,但冒號目前不工作

package hw04; 

/** 
* 
* @author Jake 
*/ 
public class DigitalTimeUI extends javax.swing.JFrame { 

/** 
* Creates new form DigitalTimeUI 
*/ 
public DigitalTimeUI() { 
    initComponents(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    titlePanel1 = new hw04.TitlePanel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    javax.swing.GroupLayout titlePanel1Layout = new javax.swing.GroupLayout(titlePanel1); 
    titlePanel1.setLayout(titlePanel1Layout); 
    titlePanel1Layout.setHorizontalGroup(
     titlePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 700, Short.MAX_VALUE) 
    ); 
    titlePanel1Layout.setVerticalGroup(
     titlePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 100, Short.MAX_VALUE) 
    ); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addComponent(titlePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addComponent(titlePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap(209, Short.MAX_VALUE)) 
    ); 

    pack(); 
}// </editor-fold>       

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(DigitalTimeUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(DigitalTimeUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(DigitalTimeUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(DigitalTimeUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new DigitalTimeUI().setVisible(true);     
     } 
    }); 

} 
// Variables declaration - do not modify      
private hw04.TitlePanel titlePanel1; 
// End of variables declaration     

}

這是標題面板中的代碼,它可以完美運行:

package hw04; 

import java.awt.*; 

/** 
* 
* @author Jake 
*/ 
public class TitlePanel extends javax.swing.JPanel { 
private static int PANEL_WIDTH=700; 
private static int PANEL_HEIGHT=100; 
private Font TITLE_FONT= new Font("Arial", Font.BOLD, 72); 
/** 
* Creates new form TitlePanel 
*/ 
public TitlePanel() { 
    initComponents(); 
    setPreferredSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT)); 
} 
    @Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.setFont(TITLE_FONT); 
    FontMetrics fm = g.getFontMetrics(); 
    int stringWidth= fm.stringWidth("DIGITAL TIME"); 
    int stringAscent= fm.getAscent(); 
    int xCoordinate= getWidth()/2-stringWidth/2; 
    int yCoordinate = getHeight()/2+stringAscent/2-8; 
    g.drawString("DIGITAL TIME", xCoordinate, yCoordinate); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    setBackground(new java.awt.Color(255, 255, 255)); 

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

}

這裏是結腸面板的代碼,它不會畫任何東西:

package hw04; 

import java.awt.*; 

/** 
* 
* @author Jake 
*/ 
public class ColonPanel extends javax.swing.JPanel { 
private static int PANEL_WIDTH=40; 
private static int PANEL_HEIGHT=80; 
private Font DIGIT_FONT= new Font("Arial", Font.BOLD, 72); 

/** 
* Creates new form ColonPanel 
*/ 
public ColonPanel() { 
    initComponents(); 
    setPreferredSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT)); 

} 
@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.setFont(DIGIT_FONT); 
    g.setColor(Color.BLACK); 
    FontMetrics fm = g.getFontMetrics(); 
    int stringWidth= fm.stringWidth("DIGITAL TIME"); 
    int stringAscent= fm.getAscent(); 
    int xCoordinate= getWidth()/2-stringWidth/2; 
    int yCoordinate = getHeight()/2+stringAscent/2; 
    g.drawString(":", xCoordinate, yCoordinate); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    setBackground(new java.awt.Color(255, 255, 255)); 

    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>       
// Variables declaration - do not modify      
// End of variables declaration     

}

+2

我沒有看到那裏的ColonPanel被添加到幀。 – camickr

+1

爲了更快獲得更好的幫助,請發佈[MCTaRE](http://stackoverflow.com/help/mcve)(最小完整測試和可讀示例)。 –

回答

1
 you are not adding ColonPanel in JFrame. 
     private void initComponents() { 

     JPanel p=new JPanel(new BorderLayout()); 
     p.add(new ColonPanel(),BorderLayout.CENTER); 
     p.add(new TitlePanel(),BorderLayout.NORTH); 
     add(p); 
     } 

The image of Colon panel show ':' like that

0

您沒有設置在colonPanel stringwidth正確的值。

你有

int stringWidth= fm.stringWidth("DIGITAL TIME"); 

你應該有類似

int stringWidth= fm.stringWidth(":"); 
相關問題