Java automatically adjusting to the Windows 7 font size adjustment
In Windows 7, if you change the font size via the Control Panel->Appearance and Personalization -> Display "Make text and other items larger or smaller", it adjusts not only the menu sizes, but also the text content size of apps like Notepad, Wordpad, Firefox.
Is there a way to ge开发者_Go百科t Java to automatically scale the font without having to manually scale it?
There are two parts to this:
- Getting your components, fonts, etc to scale
- Getting your layouts to scale
For Swing, the first part is easy - it all starts with one call.
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
On Windows, this will cause it to respect your Small/Large fonts (DPI) setting.
Here are two screenshots from a quick test app I threw together, demonstrating how it looks on my machine in Windows 7 @ 96dpi (normal font size) and @ 144dpi (150%)
First the default font size sample:
Now with Larger (150%) font size set:
There is no code change between runs, only logging out & back in with new DPI settings. I set a fixed frame size on purpose to demonstrate that my container is not scaling in size, which caused my label to get pushed down in order to fit.
Here is my source code - cut & paste and run it yourself:
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class SwingFontTest
{
private static void createGUI()
{
JButton button = new JButton("my button with Some Text");
JLabel label = new JLabel("and a label");
JPanel panel = new JPanel(new FlowLayout());
panel.add(button);
panel.add(label);
JFrame frame = new JFrame("Title!");
frame.setContentPane(panel);
frame.setSize(300,125);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run()
{
createGUI();
}
});
}
}
The Look & Feel supplies the default sizing, but it is up to the GUI author to use scalable units in their layouts. It does take effort (scalable layouts are a pain on webpages too!), but it's definitely attainable.
I recommend using a layout like FormLayout that will let you define your layouts in Dialog Units (DLU), as these scale with DPI. That will enable you to make your containers scale in size and should help limit behaviors like the label moving to the next line due to sizing. If the size of the frame was determined using dialog units then it could be made to look the same, only larger.
It is late - so that's it for now.
This would depend entirely on what GUI toolkit you're using. I'm almost certain that SWT for instance would automatically pick up the new sizes (since it uses native widgets). AWT might as well for all I know. Can you specify what what toolkit you're using and whether you've determined that its not already doing what you want (and possibly how)?
精彩评论