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

Java 在框架中绘制面板时,我有标题,但冒号到目前为止不起作用,java,swing,awt,Java,Swing,Awt,总体目标是创建一个显示当前时间的静态数字时钟。我得到了我想要的工作标题,但我不能让冒号画得合适。 这是我的帧码 package hw04; /** * * @author Jake */ public class DigitalTimeUI extends javax.swing.JFrame { /** * Creates new form DigitalTimeUI */ public DigitalTimeUI() { initComponents(); } /**

总体目标是创建一个显示当前时间的静态数字时钟。我得到了我想要的工作标题,但我不能让冒号画得合适。 这是我的帧码

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                   
包装hw04;
/**
*
*@作者杰克
*/
公共类DigitalTimeUI扩展了javax.swing.JFrame{
/**
*创建新表单DigitalTimeUI
*/
公共DigitalTimeUI(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
titlePanel1=新的hw04.TitlePanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout titlePanel1Layout=新的javax.swing.GroupLayout(titlePanel1);
titlePanel1.setLayout(titlePanel1布局);
titlePanel1Layout.setHorizontalGroup(
titlePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,700,短。最大值)
);
titlePanel1Layout.setVerticalGroup(
titlePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,100,短。最大值)
);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(标题面板1、javax.swing.GroupLayout.PREFERRED\u大小、javax.swing.GroupLayout.DEFAULT\u大小、javax.swing.GroupLayout.PREFERRED\u大小)
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(标题面板1、javax.swing.GroupLayout.PREFERRED\u大小、javax.swing.GroupLayout.DEFAULT\u大小、javax.swing.GroupLayout.PREFERRED\u大小)
.addContainerGap(209,简称最大值))
);
包装();
}//                         
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(DigitalTimeUI.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(DigitalTimeUI.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(DigitalTimeUI.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(DigitalTimeUI.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新的DigitalTimeUI().setVisible(true);
}
});
}
//变量声明-不修改
私人hw04.TitlePanel titlePanel1;
//变量结束声明
}

这是标题面板的代码,它工作得非常完美:

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                   
包装hw04;
导入java.awt.*;
/**
*
*@作者杰克
*/
公共类TitlePanel扩展了javax.swing.JPanel{
专用静态int面板_宽度=700;
专用静态int面板_高度=100;
私有字体TITLE_Font=新字体(“Arial”,Font.BOLD,72);
/**
*创建新表单标题面板
*/
公共标题板(){
初始化组件();
setPreferredSize(新尺寸(面板宽度、面板高度));
}
@凌驾
受保护组件(图形g){
超级组件(g);
g、 setFont(标题字体);
FontMetrics fm=g.getFontMetrics();
int stringWidth=fm.stringWidth(“数字时间”);
int stringAscent=fm.getAscent();
int xCoordinate=getWidth()/2-stringWidth/2;
int yCoordinate=getHeight()/2+stringAscent/2-8;
g、 抽绳(“数字时间”,xCoordinate,yCoordinate);
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
setBackground(新java.awt.Color(255、255、255));
javax.swing.GroupLayout=newjavax.swing.GroupLayout(this);
这个.setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,700,短。最大值)
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,100,短。最大值)
);
}//                         
//变量声明-不修改
//变量结束声明
}

下面是冒号面板的代码,它不会绘制任何内容:

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                   
包装hw04;
导入java.awt.*;
/**
*
*@作者杰克
*/
公共类ColonPanel扩展了javax.swing.JPanel{
专用静态int面板_宽度=40;
专用静态int面板_高度=80;
专用字体数字字体=新字体(“Arial”,Font.BOLD,72);
/**
*
        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);
         }
int stringWidth= fm.stringWidth("DIGITAL TIME");
int stringWidth= fm.stringWidth(":");