I’m trying to build a simple AWT application in Java. I want all of the containers in the main window to be separated by bit. I can accomplish this by setting the Hgap and Vgap in the BorderLayout constructor (see below.)
However, I can’t figure out how to put a cap between the containers and the edges of the main window. How do I add a few pixels of padding to the main window?
import java.awt.*;
import java.applet.Applet;
public class LayoutTest extends Applet {
public void init() {
BorderLayout layout = new BorderLayout(8, 8);
setLayout(layout);
add(new Button("Left"), BorderLayout.CENTER);
add(new Button("Right"), BorderLayout.EAST);
}
}
I agree with the other answers and would recommend using Swing (use JApplet instead), which would make all kinds of things easier (you could just call setBorder and use BorderFactory to create a border, for example), but in your case you can set insets by overriding getInsets:
Replace 10 with whatever you like.
There doesn’t appear to be a setter, or I would say to use that instead. If there is a better way to do this in the case of an AWT Applet, someone please correct me.
If you decide to use Swing, see: How to Use Borders