'GridBagLayout Pixel Offset from Anchor Position
How do I remove the pixel offset from the SW-Button corner?
Some sizes will work, while others won't I'm just looking for a reliable way to always have the button be in the very bottom-right corner.
I've tried different values for weightx/y and ipadx/y which fix it for specific sizes but just create the issue again on others.
Is this just a general problem with GridBagLayout or am I missing something?
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestFrame extends JFrame {
public static void main(String[] args) {
new TestFrame();
}
public TestFrame() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(501, 500);
this.getContentPane().setBackground(Color.MAGENTA);
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTHWEST;
JButton nw = new JButton("NW");
this.add(nw, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.SOUTHEAST;
JButton se = new JButton("SW");
this.add(se, gbc);
this.setVisible(true);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

