绘制在一个框架中的面板,我有标题,但到目前为止冒号不起作用

时间:2014-02-20 17:30:45

标签: java swing awt

总体目标是创建一个显示当前时间的静态数字时钟。我得到了我想要的工作头衔,但我不能让结肠适当地画出来。 这是我的框架代码。

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 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您没有在colonPanel中为stringwidth设置正确的值。

你有

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

你应该有像

这样的东西
int stringWidth= fm.stringWidth(":");