java 用可获取的字体、样式、字号修饰文字

本文介绍如何利用Java的Font类调整窗口文本的样式、大小和字体,通过选择字体、设置样式和输入文字大小,实现窗口内文本的个性化显示。包括获取本地可用字体、设置字体样式和平滑过渡效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)函数说明: 
字形类Font用于规范组件所使用的字形大小、样式和字体等。其构造函数: 
public Font(String name,int style,int size); 
name表示本地可获取字体名称 
style表示字体样式,包含Font.PLAIN,Font.BOLD,Font.ITALIC三种,分别对应平体、加粗和斜体。 

一个有用的方法用来获取本地可用字体 
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); 
String[] fa=ge.getAvailableFontFamilyNames(); 

通过从绘图环境中获取到本地可用的字体名数组。 


(2)代码  用指定的字型约束来显示窗体上的字。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class GraExp extends JFrame
{ 
CInstead c1=new CInstead();
Container c;
String[] zt;
String[] zx={ "平体" ,"加粗" ,"斜体" } ;
 
String name="Serif" ;
int type=0;
int size=12; 
JLabel lbl1=new JLabel("字体:" );
JLabel lbl2=new JLabel("字形:" );
JLabel lbl3=new JLabel("字号:" );
 
JLabel lbl=new JLabel("测试用字abcABC123" );
JComboBox cb1,
cb2=new JComboBox(zx);

JTextField tf1=new JTextField(25),
tf2=new JTextField(10);
 
public GraExp()
{ 
//获取可用字体名称数组 
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
zt=ge.getAvailableFontFamilyNames();
cb1=new JComboBox(zt);
setContentPane(c1);
c=getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT));
c.add(lbl1);
c.add(cb1);
c.add(lbl2);
c.add(cb2);
c.add(lbl3);
c.add(tf1);
lbl.setPreferredSize(new Dimension(200,60));
lbl.setForeground(Color.red);
c.add(lbl);
 
cb1.addItemListener(new ItemListener(){ 
public void itemStateChanged(ItemEvent event)
{ 
int state=event.getStateChange();
name=(String) event.getItem();
setCustomFont(name,type,size);
} 
} );
cb2.addItemListener(new ItemListener(){ 
public void itemStateChanged(ItemEvent event)
{ 
int state=event.getStateChange();
String s=(String) event.getItem();
if (s.equals("平体" )){ 
type=Font.PLAIN;
} else if (s.equals("加粗" )){ 
type=Font.BOLD;
} else { 
type=Font.ITALIC;
} 

setCustomFont(name,type,size);
} 
} );
tf1.addKeyListener(new KeyAdapter(){ 
public void keyPressed(KeyEvent e) 
{ 
int c=e.getKeyCode();
if (c==10)
{ 
String s=tf1.getText();
size=Integer.parseInt(s);
setCustomFont(name,type,size);
} 
} 
} ); 

setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(new Dimension(400,300));
show();
} 
public static void main(String[] args)
{ 
GraExp ge=new GraExp();
} 
 
private void setCustomFont(String name,int type,int size)
{ 
lbl.setFont(new Font(name,type,size));
lbl.revalidate();
} 
class CInstead extends JPanel
{ 
ImageIcon icon;
Image img;
public CInstead()
{ 
icon=new ImageIcon(CInstead.class.getResource("333.png" ));
img=icon.getImage();
} 
public void paintComponent(Graphics g)
{ 
super.paintComponent(g);
g.drawImage(img,0,0,null );
} 
} 
} 


程序效果:




import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.font.*; public class Clipping extends Canvas { public Clipping() { setBackground(Color.white); } public void paint(Graphics g) { Graphics2D g2; g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); int w = getSize().width; int h = getSize().height; FontRenderContext frc = g2.getFontRenderContext(); Font f = new Font("Helvetica",Font.BOLD,w/8); String s = new String("By"); TextLayout tl = new TextLayout(s, f, frc); float sw = (float) tl.getBounds().getWidth(); AffineTransform transform = new AffineTransform(); transform.setToTranslation(w/2-sw/2,h/2); Shape shape = tl.getOutline(transform); g2.setClip(shape); g2.setColor(Color.blue); g2.fill(shape.getBounds()); g2.setColor(Color.yellow); for (int j = shape.getBounds().y; j < shape.getBounds().y + shape.getBounds().height; j=j+3) { Line2D line = new Line2D.Float( 0.0f, (float) j, (float) w, (float) j); g2.draw(line); } } public static void main(String s[]) { WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowClosed(WindowEvent e) {System.exit(0);} }; Frame f = new Frame("2D Text"); f.addWindowListener(l); f.add("Center", new Clipping()); f.pack(); f.setSize(new Dimension(400, 300)); f.show(); } } *********************************************
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值