简体   繁体   中英

Japplet shapes does not shown in Applet

Im working with eclipse.

My Code:

import javax.swing.JApplet;
import java.awt.*;

public class Einstein extends JApplet 
{
    public void pain(Graphics page)
    {
        page.drawRect(60,60,40,40); // Square
        page.drawString("Out of clutter, find simplicity. " , 110, 70);
    }
}

The rectangle and text does not shown in the applet. what could be the problem?

public void pain(Graphics page) - interesting choice of naming...

I believe the method you're looking for is paint

@Override
public void paint(Graphics g) {
    super.paint(g);
    //...
}

Make sure you call super.paint before performing any custom painting, otherwise you could end up with a bunch of nasty paint artefacts.

Having said that. Consider using a custom component, extending from JPanel for example, and override it's paintComponent method instead, then add this component to your applet.

You gain the benefit of the double buffering support of Swing for free and the freedom to move you component to another container, like a JFrame or other container for example, making it far more re-usable

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