gpt4 book ai didi

java - JFrame 不允许我 drawString();

转载 作者:行者123 更新时间:2023-11-29 08:06:20 25 4
gpt4 key购买 nike

我需要帮助,因为我正在尝试用 Java 编写游戏代码。当我发现它不会向 JFrame 绘制字符串时,我停下了脚步。我已经尝试了几种方法来解决这个问题并进行了大量研究但一无所获。这是代码:-俄勒冈州(主要类(class)):

package com.lojana.oregon.client;

import java.awt.*;

import javax.swing.*;

import com.lojana.oregon.src.Desktop;
import com.lojana.oregon.src.Keyboard;
import com.lojana.oregon.src.Mouse;
import com.lojana.oregon.src.Paint;

public class Oregon extends JFrame {
private static final long serialVersionUID = 1L;

// Currently unused but there will be a use for it in the future
public Desktop desktop;

public String TITLE = "Oregon";

public Oregon() {
/* Window code */
setTitle(TITLE);
setSize(640, 640);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

/* Extra code for Window */
addKeyListener(new Keyboard());
addMouseListener(new Mouse());
}

public void paint(Graphics g) {
Paint.paint(g);
}
}

GuiButton(绘画类):

package com.lojana.oregon.src;

import java.awt.*;

public class GuiButton {
public GuiButton(Graphics g, String text, Font font, int coordX, int coordY,
int textX, int textY, int width, int height) {
Color border = Color.gray;
Color fill = Color.white;
Color textColor = Color.black;

Stroke borderSize = new BasicStroke(8);

g.setColor(border);
((Graphics2D) g).setStroke(borderSize);
g.drawRect(coordX, coordY, width, height);
g.setColor(fill);
g.fillRect(coordX, coordY, width, height);
g.setColor(textColor);
g.setFont(font);
g.drawString(text, textX, textY);
}
}

GuiMainMenu(使用 GuiButton 文件的文件):

package com.lojana.oregon.src;

import java.awt.*;

public class GuiMainMenu {
public static void paint(Graphics g) {
new GuiButton(g, "Start Game", new Font("Arial", Font.BOLD, 20), 60, 80, 20, 20, 240, 40);
}
}

如果您知道如何修复它,请发表评论。非常感谢:)

最佳答案

Swing 程序应该覆盖 paintComponent(Graphics g) 而不是 paint(Graphics g) 并直接覆盖 JFrame。详情请看这篇文章: http://java.sun.com/products/jfc/tsc/articles/painting/

此外,最好覆盖添加到 JFrame(的内容 Pane )的 JPanelpaintComponent 而不是JFrame 本身,因为您要绘制到此内容 Pane 中。有关详细信息,请参阅本教程: http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html

关于java - JFrame 不允许我 drawString();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10970962/

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