Mik*_*rin 17
这是一个简单的方法,允许您将Font指定给任何Container下的整个组件树(或者只是一个简单的Component,无所谓):
public static void changeFont ( Component component, Font font )
{
component.setFont ( font );
if ( component instanceof Container )
{
for ( Component child : ( ( Container ) component ).getComponents () )
{
changeFont ( child, font );
}
}
}
Run Code Online (Sandbox Code Playgroud)
只需将您的面板和特定字体传递给此方法,您就可以重构所有孩子.
Kum*_*tra 10
-你可以UIManager
用来做这个....
例如:
public class FrameTest {
public static void setUIFont(FontUIResource f) {
Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
FontUIResource orig = (FontUIResource) value;
Font font = new Font(f.getFontName(), orig.getStyle(), f.getSize());
UIManager.put(key, new FontUIResource(font));
}
}
}
public static void main(String[] args) throws InterruptedException {
setUIFont(new FontUIResource(new Font("Arial", 0, 20)));
JFrame f = new JFrame("Demo");
f.getContentPane().setLayout(new BorderLayout());
JPanel p = new JPanel();
p.add(new JLabel("hello"));
p.setBorder(BorderFactory.createTitledBorder("Test Title"));
f.add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 300);
f.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
设置UIManager
要更改的组件的字体值。例如,您可以通过执行以下操作来设置标签使用的字体:
Font labelFont = ... ;
UIManager.put("Label.font", labelFont);
Run Code Online (Sandbox Code Playgroud)
请注意,不同的外观 (L&F) 可能具有不同的类属性UIManager
,因此,如果您从一种 L&F 切换到另一种,可能会遇到问题。
归档时间: |
|
查看次数: |
11122 次 |
最近记录: |