更改默认字体的JLabel
-
21-09-2019 - |
题
我怎么会去设置默认字体为所有JLabel
实例。而不是独立地设定各JLabel
的字体。
解决方案
使用 UIManager
定义JLabel的默认字体:
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
public class LabelFont {
public static void main(String[] args) {
Font oldLabelFont = UIManager.getFont("Label.font");
UIManager.put("Label.font", oldLabelFont.deriveFont(Font.PLAIN));
JFrame f = new JFrame("LabelFont Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new FlowLayout());
JLabel df = new JLabel("Default JLabel font");
f.getContentPane().add(df);
JLabel ef = new JLabel("Font explicitly set");
ef.setFont(oldLabelFont);
f.getContentPane().add(ef);
f.pack();
f.setVisible(true);
}
}
经: HTTP:// coding.derkeiler.com/Archive/Java/comp.lang.java.help/2005-04/msg00395.html
其他提示
这是你在寻找什么?
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
public class LabelFont {
public static void main(String[] args) {
Font oldLabelFont = UIManager.getFont("Label.font");
UIManager.put("Label.font", oldLabelFont.deriveFont(Font.PLAIN));
JFrame f = new JFrame("LabelFont Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new FlowLayout());
JLabel df = new JLabel("Default JLabel font");
f.getContentPane().add(df);
JLabel ef = new JLabel("Font explicitly set");
ef.setFont(oldLabelFont);
f.getContentPane().add(ef);
f.pack();
f.setVisible(true);
}
}
不隶属于 StackOverflow