Java 关于Font类的字体设置
发布网友
发布时间:2022-04-23 00:09
我来回答
共2个回答
热心网友
时间:2023-06-22 05:31
import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Label;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;import javax.swing.JLabel;public class Test_24 extends Frame implements ItemListener { private static final long serialVersionUID = 1L;
Choice c;
JLabel l; public static void main(String[] args) {
new Test_24(); } public Test_24() {
super();
initialize();
} private void initialize() {
this.setSize(300, 200);
this.setTitle("Frame");
GraphicsEnvironment g = GraphicsEnvironment
.getLocalGraphicsEnvironment();
String fontName[] = g.getAvailableFontFamilyNames();
c = new Choice();
for (int i = 0; i < fontName.length; i++)
c.add(fontName[i]);
l = new JLabel("", JLabel.CENTER);
this.add(c, BorderLayout.NORTH);
this.add(l, BorderLayout.CENTER);
this.setVisible(true);
c.addItemListener(this);
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
} public void itemStateChanged(ItemEvent e) {
String name = (String) e.getItem();
System.out.println(name);
Font f = new Font(name, Font.ITALIC, 40);
l.setForeground(Color.red);
l.setFont(f);
l.setText("测试字体");
double x=l.getLocation().getX();
double y=l.getLocation().getY();
int h = l.getSize().height;
int w = l.getSize().width;
System.out.println(x+","+y);
System.out.println(h+","+w);
}}
热心网友
时间:2023-06-22 05:32
设计字体显示效果 Font mf = new Font(String 字体,int 风格,int 字号);
字体:TimesRoman, Courier, Arial等
风格:三个常量 lFont.PLAIN, Font.BOLD, Font.ITALIC
字号:字的大小(磅数)
设置组件当前使用的字体:setFont(Font fn)
获取组件当前使用的字体:getFont()