Paint Component Method不适用于JFrame [英] Paint Component Method not working for 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...
这篇关于Paint Component Method不适用于JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!