Java源码-字体的定义与修改

以下代码摘录于Java how to program, 第10版第12章。

代码如下:

//Fig. 12.19: RadioButtonFrame.java
//Creating radio buttons using ButtonGroup and JRadioButton.
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

public class RadioButtonFrame extends JFrame 
{
private JTextField textField; // used to display font changes
private Font plainFont; // font for plain text
private Font boldFont; // font for bold text
private Font italicFont; // font for italic text
private Font boldItalicFont; // font for bold and italic text
private JRadioButton plainJRadioButton; // selects plain text
private JRadioButton boldJRadioButton; // selects bold text
private JRadioButton italicJRadioButton; // selects italic text
private JRadioButton boldItalicJRadioButton; // bold and italic
private ButtonGroup radioGroup; // buttongroup to hold radio buttons

// RadioButtonFrame constructor adds JRadioButtons to JFrame
public RadioButtonFrame()
{
   super("RadioButton Test");
   setLayout(new FlowLayout()); 

   textField = new JTextField("请选择字体,查看效果。", 25);
   add(textField); // add textField to JFrame

   // create radio buttons
   plainJRadioButton = new JRadioButton("Plain", true);
   boldJRadioButton = new JRadioButton("Bold", false);
   italicJRadioButton = new JRadioButton("Italic", false);
   boldItalicJRadioButton = new JRadioButton("Bold/Italic", false);
   add(plainJRadioButton); // add plain button to JFrame
   add(boldJRadioButton); // add bold button to JFrame
   add(italicJRadioButton); // add italic button to JFrame
   add(boldItalicJRadioButton); // add bold and italic button

   // create logical relationship between JRadioButtons
   radioGroup = new ButtonGroup(); // create ButtonGroup
   radioGroup.add(plainJRadioButton); // add plain to group
   radioGroup.add(boldJRadioButton); // add bold to group
   radioGroup.add(italicJRadioButton); // add italic to group
   radioGroup.add(boldItalicJRadioButton); // add bold and italic

   // create font objects
   plainFont = new Font("Serif", Font.PLAIN, 14);
   boldFont = new Font("Serif", Font.BOLD, 14);
   italicFont = new Font("Serif", Font.ITALIC, 14);
   boldItalicFont = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
   textField.setFont(plainFont);

   // register events for JRadioButtons
   plainJRadioButton.addItemListener(
      new RadioButtonHandler(plainFont));
   boldJRadioButton.addItemListener(
      new RadioButtonHandler(boldFont));
   italicJRadioButton.addItemListener(
      new RadioButtonHandler(italicFont));
   boldItalicJRadioButton.addItemListener(
      new RadioButtonHandler(boldItalicFont));
} 

// private inner class to handle radio button events
private class RadioButtonHandler implements ItemListener 
{
   private Font font; // font associated with this listener

   public RadioButtonHandler(Font f)
   {
      font = f; 
   } 

   // handle radio button events
   @Override
   public void itemStateChanged(ItemEvent event)
   {
      textField.setFont(font); 
   } 
} 
} // end class RadioButtonFrame 


测试类代码如下:

//Fig. 12.20: RadioButtonTest.java
//Testing RadioButtonFrame.
import javax.swing.JFrame;

public class RadioButtonTest  
{
public static void main(String[] args)
{
   RadioButtonFrame radioButtonFrame = new RadioButtonFrame();
   radioButtonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   radioButtonFrame.setSize(300, 100); 
   radioButtonFrame.setVisible(true); 
} 
} // end class RadioButtonTest 


运行截屏:



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、付费专栏及课程。

余额充值