您创建了一个静态方法,该方法不会覆盖paint方法。现在,其他人已经指出,您需要覆盖paintComponent等。但是,为了快速修复,您需要执行以下操作:
public class Myframe extends Jframe { public Myframe() { super("My frame"); // You can set the content pane of the frame to your custom class. setContentPane(new DrawPane()); setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); setSize(400, 400); setVisible(true); } // Create a component that you can actually draw on. class DrawPane extends JPanel { public void paintComponent(Graphics g) { g.fillRect(20, 20, 100, 200); // Draw on g here e.g. } } public static void main(String args[]){ new Myframe(); }}但是,正如其他人指出的那样,在Jframe上进行绘制非常棘手。最好使用JPanel。



