Java 图形无法显示
这是我的代码:
package survival;
import javax.swing.*;
import java.awt.*;
public class Survival extends JFrame {
private static int applicationWidth = 1400;
private static int applicationHeight = 900;
public Survival() {
setTitle("Survival");
setResizable(false);
setSize(applicationWidth, applicationHeight);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
g.drawString("Test", 0, 0);
}
public static void main(String[] args) {
new Survival();
}
}
为什么没有显示“测试”?
Here is my code:
package survival;
import javax.swing.*;
import java.awt.*;
public class Survival extends JFrame {
private static int applicationWidth = 1400;
private static int applicationHeight = 900;
public Survival() {
setTitle("Survival");
setResizable(false);
setSize(applicationWidth, applicationHeight);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
g.drawString("Test", 0, 0);
}
public static void main(String[] args) {
new Survival();
}
}
Why isn't "Test" showing up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要覆盖
paint
。每当您自定义组件时,请覆盖paintComponent
。示例 -
Do not override
paint
. Whenever you customize a component, overridepaintComponent
.Example -
您需要调用超类的paint()方法。 (文章 - 在 AWT 和 Swing 中绘画)
覆盖 JPanel 的 PaintComponent 。
You need to invoke paint() method of super class. (Article - Painting in AWT and Swing)
Override the paintComponent of JPanel.