Why does Windows LookAndFeel make fonts too small?

The native Windows LookAndFeel in Java 6 seems to incorrectly size some fonts.

Test program:

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class Test {
  public static void main(String[] arg) throws Exception {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
          e.printStackTrace();
        }

        final JMenuBar mb = new JMenuBar();

        final JMenu file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);
        mb.add(file);

        final JToolBar toolbar = new JToolBar();
        final JButton button = new JButton("Button");
        toolbar.add(button);

        final JLabel label = new JLabel("Basic Colors");

        final JPanel panel = new JPanel(new BorderLayout());
        panel.add(toolbar, BorderLayout.PAGE_START);
        panel.add(label, BorderLayout.CENTER);       

        final JFrame frame = new JFrame();
        frame.setJMenuBar(mb);
        frame.add(panel);

        frame.setTitle("Test"); 
        frame.setSize(400,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    });
  }
}

Output, compared with a native Windows app on Vista:

对照

While the text in the test app menu bar is sized correctly, the rest of the text is smaller than in the native app next to it. Zoomed in, you can see that the text in the test app JLabel is 1px shorter than in the native app:

Is this a bug in the Windows LaF, or are we misusing it? If it's a bug, is there a known workaround?


Java 6 uses its own font renderer, including the implementation of subpixel antialiasing / hinting whatsit. While the output is meant to be similar to Windows' rendering, either the top of the B being at a pixel boundary, or it being rounded, or both, throws the Java renderer off. The Windows font renderer decides to place the top of the letter above the boundary, while the Java one places it below. The 'l' looks like it's at the same height in both samples, so it doesn't seem like the renderer gets the height of every letter wrong. Maybe try comparing with some letters where the top is a straight line, like a T or an E?

链接地址: http://www.djcxy.com/p/55240.html

上一篇: 伪装SVN结帐的深度

下一篇: 为什么Windows LookAndFeel使字体太小?