I have no idea why I can’t find a solution for this… I am trying to layout some AWT components in a flow layout. The only problem is the ‘padding’ between the components (Panels) when using a flow layout. This is what the applet currently looks like: https://i.stack.imgur.com/2KZgD.png
I need a way to set the Applet/Panels so that the two panels (black boxes) are touching (no ‘padding’). The entire program is Swing free, all AWT, and I plan on keeping it that way. I feel this is a very simple solution, but I have not been able to find an answer.
This is the init() code from the applet class:
public void init() {
setLayout(new FlowLayout());
c1 = new TestPanel();
c2 = new TestPanel();
c1.setPreferredSize(new Dimension(640, 480));
c2.setPreferredSize(new Dimension(100, 480));
add(c1);
add(c2);
}
This is the TestPanel class I’m using:
public class TestPanel extends Panel {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getPreferredSize().width, this.getPreferredSize().height);
}
}
The default horizontal (and vertical) gap of
FlowLayoutis set to5. Therefore, you must explicitly set the horizontal gap to0.FIRST APPROACH
Invoke
setHgap(...)on the component’s layout. Since the default layout of aJPanelisFlowLayout, simply do the following:SECOND APPROACH
Use another
FlowLayoutconstructor. That is,FlowLayout(int align, int hgap, int vgap). And simply do the following: