[英]paintComponents method not being called in Java
I watched a tutorial and tried to do same thing, I wrote the codes exactly the same but it shows nothing. 我看了一个教程并尝试做同样的事情,我编写的代码完全相同,但它没有显示任何内容。 I think it is because paintComponent method is not being called, I also tried to print something to console by paintComponent. 我认为这是因为没有调用paintComponent方法,我还试图通过paintComponent将某些内容打印到控制台。
Here is my code: 这是我的代码:
public class Line extends JPanel{
@Override
public void paintComponents(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(100, 10, 30, 40);
}
public static void main(String[] args) {
Line l =new Line();
JFrame myFrame = new JFrame("Line");
myFrame.setSize(600, 400);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.add(l);
myFrame.setVisible(true);
}
}
Thank you! 谢谢!
What you want to override is paintComponent
, not paintComponents
with a s
. 你要覆盖的是paintComponent
,而不是带有s
paintComponents
。
paintComponents
paints the child components of the current component (well it sort of tells the child components to paint themselves on the Graphics
object). paintComponents
绘制当前组件的子组件(它有点告诉子组件在Graphics
对象上绘制自己)。
paintComponent
paints the component itself, this is the method you want to override to do custom painting for your component. paintComponent
绘制组件本身,这是您要覆盖的方法,以便为组件执行自定义绘制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.