简体   繁体   中英

Paint in Swing not working

Why does this not work? It shows me the GUI but not the paint. How would I change this into two classes?

import java.awt.Graphics;
import javax.swing.JFrame;

public class runpaintgui extends JFrame{    

    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setSize(5000,2000);
        frame.setResizable(false);
        frame.setTitle("game");
        frame.setVisible(true);    
    }

    public void paint(Graphics g){
        super.paint(g);
        g.drawString("adsf",40,45);
        g.draw3DRect(50, 30, 600, 700, true);   

        repaint();
    }    
}

You are creating a generic JFrame in this line:

JFrame frame = new JFrame();

What you want to probably do is:

JFrame frame = new runpaintgui();

Then your paint() method will be called.

You have to instantiate your class and not the JFrame class.

change:

JFrame frame = new JFrame();

to

runpaintgui frame = new runpaintgui();

Then your paint() method will be called.

And do not call repaint() in paint . Because repaint() calls paint .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM