【发布时间】:2018-12-07 03:10:52
【问题描述】:
我对为什么 Paint Component 没有在这段代码中运行感到非常困惑:
public class GraphicsWindow extends JPanel {
public static final int Width = 1000, Height = 800;
GraphicsWindow(){
setPreferredSize(new Dimension(Width, Height));
}
public void PaintComponent(Graphics g){
super.paintComponents(g);
g.setColor(Color.red);
for(int i = 0; i < Width/10; i++){
g.drawLine(i * 10, 0, i*10, Height);
}
System.out.println("paint ran");
}
}
主要功能:
public static void main(String[] args) {
GraphicsWindow Graphics = new GraphicsWindow();
Graphics.setBackground(Color.green);
Graphics.setSize(1000, 800);
JFrame Window = new JFrame("Snake");
Window.add(Graphics);
Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Window.setBounds(650, 200, 1200, 1000);
Window.setVisible(true);
Graphics.repaint();
}
我已经看了大约一个小时,一个又一个论坛页面查看论坛页面,但一无所获。我可以说它是油漆组件没有运行,因为控制台永远不会得到“油漆运行”打印输出。如果这是一个非常愚蠢的错误并且可能是混乱的代码,我很抱歉,我对java有点陌生。
【问题讨论】:
标签: java swing graphics jframe jpanel