gpt4 book ai didi

JAVA自定义消息面板

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:09 25 4
gpt4 key购买 nike

我正在尝试创建其中包含自定义文本的面板,但这就是它显示的内容:

http://i.imgur.com/Vf76y5w.png

还尝试放置文本而不是点,但效果相同。这是我的代码:

public class Combo extends JPanel {
static int points = 1000;
public Combo(){
paintPoints p = new paintPoints();
p.setFont(new Font("Brush Script Std",Font.PLAIN,72));
add(p);

}

public static void main(String[] args) {

JFrame frame = new JFrame();
frame.add(new paintPoints()); //edit
frame.setVisible(true);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



}

class paintPoints extends JPanel{
protected void paintComponent(Graphics g){

super.paintComponent(g);
FontMetrics fm = g.getFontMetrics();
int StringWidth = fm.stringWidth(String.valueOf(points));
int StringAscent = fm.getAscent();
int xCoordinates = getWidth()/2 - StringWidth/2;
int yCoordinates = getHeight()/2 + StringAscent/2;
g.setColor(new Color(128,0,0));
g.drawString(String.valueOf(points), xCoordinates, yCoordinates);

}
}
}

最佳答案

我建议你可以直接添加你的paintPoints面板然后设置字体,不需要使用Combo JPanel。我复制了您的 paintPoints 代码,并且非常适合我。

请注意,您应该使用大写字母来命名您的 java 类。

编辑:您还需要调用 设置框架大小后使用 frame.setVisible(true); 方法,否则除非显式设置新的面板大小,否则您将看不到面板。
事实上,在设置框架大小之前调用 setVisible(true); 会将其所有组件的大小初始化为 0 宽度和 0 高度。

关于JAVA自定义消息面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28568294/

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