I have a component with a JLabel above it. Both of these components are centered relative to the component that is next to it. However, it is not coming out quite right. It appears that the JLabel has 4 pixels of extra space above it. This spacing I believe is due to the font and not from the insets of the GridBagLayout. If I double the size of the JLabel's font, the spacing increases to 8 pixels. I also tested this with a small program that adds a single JLabel to a JFrame:
public class MainClass {
public static void main(String[] args) {
javax.swing.JFrame frame = new javax.swing.JFrame();
javax.swing.JLabel label = new javax.swing.JLabel("Environment-gy");
label.setBackground(java.awt.Color.RED);
frame.getContentPane().add(label);
frame.getContentPane().setBackground(java.awt.Color.RED);
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
If you run this, you will notis that the "gy" in the label has only a single pixel of spacing below, but there are 4 pixels of spacing above the text. Is there any way to remove these 4 pixels? The components do not center correctly with them in there. Even in the test program, the label does not appear to be centered exactly. It appears to be too far down from the top, albeit subtely.
I cannot just kludge this, however, because this application will be run on a number of different environments and there is no way to know what exact font and font size will be used.