Paint Component Method not working for JFrame(绘制组件方法不适用于 JFrame)
问题描述
在尝试使用 JLabel 之后,我正在尝试在屏幕上绘制图像,现在正在尝试使用 paintComponent 方法.我在没有看到任何结果并且没有调用该方法后尝试插入断点,并且什么也没有出现.我该怎么办?这是我的重要代码-
I'm trying to paint an image on the screen after trying to use JLabel's and am now trying the paintComponent method. I tried inserting breakpoints after seeing no results and the method doesn't get called, and nothing appears. What should I do? Here is my important code-
`
public void createWindow(){
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setSize(xSize, ySize);
frame.setLocation(0, 0);
frame.addComponentListener(this);
//frame.add(im);
}
public void paintComponent(Graphics g) {super.paintComponent(g);g.drawImage(占位符, 0, 0, getWidth(), getHeight(), null);g.drawString("你好", 100, 100);}
如果有区别的话,我也会使用 JFrame 而不是 JPanel 或组件.
Also I'm using a JFrame instead of JPanel or component if that makes a difference.
推荐答案
JFrame
没有 paintComponent
方法.您应该避免直接在框架上绘画,而是使用 JPanel
并覆盖其 paintComponent
方法
JFrame
does not have a paintComponent
method. You should avoid painting directly to a frame and instead use a JPanel
and override its paintComponent
method
您还应该使用@Override 注解,如果父类没有您尝试覆盖的方法,它将引发编译器异常...
You should also make use of the @Override annotation, which will raise a compiler exception if the parent class does not have the method you are trying to override...
这篇关于绘制组件方法不适用于 JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:绘制组件方法不适用于 JFrame


基础教程推荐
- 验证是否调用了所有 getter 方法 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 大摇大摆的枚举 2022-01-01