gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:23:27 24 4
gpt4 key购买 nike

总体目标是创建一个显示当前时间的静态数字时钟。我得到了标题,可以按照我想要的方式工作,但我无法正确绘制冒号。这是我的框架代码。

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com