Java源码-字体的定义与修改
2016-07-24 00:27
537 查看
以下代码摘录于Java how to program, 第10版第12章。
代码如下:
测试类代码如下:
运行截屏:
![](http://img.blog.csdn.net/20160724002542741?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
代码如下:
//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
运行截屏:
相关文章推荐
- 【Leetcode】213. House Robber II 【动态规划】
- [置顶] JavaSE复习总结(一)
- Java的字体类详解(Font)-摘自Java API文档
- JavaWeb文件上传下载
- Netbeans安装教程
- java版 贪吃蛇小游戏 完整源码 (测试100%无误
- Spring Boot MyBatis 连接数据库
- Java中生成随机字符的方法总结
- java笔记--关于线程同步(5种同步方式)
- JAVA面向对象-----多态
- JAVA面向对象-----多态
- 旧瓶装新酒的 Java 8 StringJoiner
- Java函数为什么没有默认参数
- 获取所有根路径(磁盘分区)的空间详情
- Java双缓冲技术
- java语法基础
- java反射与代理
- Spring MVC一些常见注解的使用(一)关于url的一些注解
- Java单例模式深入详解
- Java集合框架(二)day_16