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

标签 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                   

}

最佳答案

        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

关于java - 在框架中的面板中绘图,我有标题,但冒号到目前为止不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21915280/

相关文章:

java - 在字符串中使用 new 运算符创建两个对象有什么好处

java - 从.Net客户端调用Java Web服务来获取byte[](有Java示例)

java - 我在 JFrame 中添加容器时遇到问题

java - 使用默认表模型时调整 JTable 大小

java - setVisible 和 requestFocus on textField

java - 使用列名和列类初始化 "empty"DefaultTableModel

java.lang.NoClassDefFoundError : Failed resolution of: Landroid/support/v7/appcompat/R$drawable; 错误

java - Java 中的 RESTful Web 服务

java:如何将变量从起始 JFrame 传递到最后一个 JFrame,而不必将其传递到所有 JFrame?

java - Repaint() 没有在 Java while 循环中被调用