我看了一个教程,试着做同样的事情,我写的代码完全一样但是没有显示任何东西。我认为这是因为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);
}
}
谢谢你!
paintComponents
带有s
是存在的,但是您想要覆盖的是paintComponent
。 - Arnaud