3
我看了一篇教程并试图做同样的事情,我编写的代码完全一样,但没有显示任何内容。我认为这是因为paintComponent方法没有被调用,我也尝试通过paintComponent打印某些内容到控制台。未在Java中调用paintComponents方法
这里是我的代码:
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 –
对不起,我删除了评论,存在's'''paintComponents',但你想重写的是'paintComponent'。 – Berger
Berger是忍者,因为我不能看到任何评论或回复。 :) –