Java中paintComponents方法未被调用

3
我看了一个教程,试着做同样的事情,我写的代码完全一样但是没有显示任何东西。我认为这是因为paintComponent方法没有被调用,我还尝试通过paintComponent打印一些内容到控制台。
以下是我的代码:
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);
    }
}

谢谢你!

你很棒!谢谢!@Berger - Ms.Sahin
抱歉,我刚刚删除了注释。paintComponents 带有 s 是存在的,但是您想要覆盖的是 paintComponent - Arnaud
Berger是个忍者吗?因为我看不到任何评论或回复。 :) - Steve Smith
@SteveSmith 现在你可以看到了 :) - Ms.Sahin
@Ms.Sahin,不要编辑标题,而是发布一个回答来展示问题是如何解决的,并接受它 :) - Frakcool
1个回答

3
您想要覆盖的是paintComponent,而不是带有spaintComponentspaintComponents绘制当前组件的子组件(它会告诉子组件在Graphics对象上绘制自己)。 paintComponent绘制组件本身,这是您想要覆盖以进行自定义绘制组件的方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接