Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JLabel spacing

843807May 15 2006 — edited May 15 2006
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 12 2006
Added on May 15 2006
0 comments
290 views