Java – getting rid of Border on Top

I had this problem that my applet was always showing this 5 pixel border at the top – like you can see in the left Screenshot…

…so I was looking for a resulotion for that. In the end it was hard to guess but simple: I just had to add the line setLayout(new BorderLayout()); to make it work according to the Image on the right. But why does it have this 5 pixel? I have no clue. Doesn’t really make sense. Do you know? Here is my Code for your testing:

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JLabel;
 
public class Main extends Applet{
 
	private static final long serialVersionUID = 1L;
	JLabel myJLabel;
 
	public void init()
	{
		this.setBackground(Color.green);
		myJLabel = new JLabel ("Test");
		myJLabel.setBackground(Color.yellow);
		myJLabel.setOpaque(true);
 
		// Prevent border on top
		setLayout(new BorderLayout());
 
		this.add("Center", myJLabel);
	}
}

PS: You may want to set the Background-Color to black – otherwise you will have ugly green flickers…

Dieser Beitrag wurde unter JAVA veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.