简体   繁体   中英

Java Frame shows my screen

I am starting to learn how to make graphics in Java. But if I draw something in my Frame and I run the program. those parts of the Frame where I did not draw anything there is the picture of the Frame running behind my Program. hw can I avoid this?

package de.sarah;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class Framemg extends JFrame{

public Framemg() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Zeichnen mit Java");
    setSize(400, 300);
    setBackground(Color.yellow);
    setVisible(true);
}
public void paint(Graphics g) {
     g.drawString( "Hellooo", 120, 60 );
}
}

package de.sarah;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;

public class Main{

public static void main(String[] args) {
    Framemg Frame = new Framemg();
  }

}

Don't override paint() of a JFrame.

Custom painting is done by overriding the paintComponent(...) method of a JPanel. Then you add the panel to the frame.

Read the section from the Swing tutorial on Custom Painting for more information and working examples to get you started.

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