Search code examples
javaswingjapplet

JApplet window is blank in netbeans


I created Test.class that extends JApplet. When I run the code, the open window appears blank and doesn't show any string. How can I solve this issue?

package test;

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

public class Test extends JApplet {       

    @Override
    public void paint(Graphics g) {
        g.drawString("Hello World!", 0, 0);
    }

    @Override
    public void init() {
        repaint();
    }

}

Solution

  • In drawString(), x and y are the leftmost character's baseline. With no descenders, your text is entirely outside the rendering area. Try

    g.drawString("Hello World!", 5, g.getFontMetrics().getAscent());
    

    See also Initial Threads.