想用java做一个像windows里一样的txt编辑软件,涉及到字体设置选项卡,在网上找了很久都没找到,就生气啦自己写一个,现在贴这里分享一下,下次再遇到这样的问题就不用自己亲自打代码啦!

  1 package 实验;
  2
  3 import java.awt.*;
  4 import java.awt.event.ActionEvent;
  5 import java.awt.event.ActionListener;
  6 import java.awt.event.MouseAdapter;
  7 import java.awt.event.MouseEvent;
  8
  9 import javax.swing.*;
 10 import javax.swing.border.BevelBorder;
 11
 12 /**
 13  * 字体格式设置对话框
 14  */
 15
 16 public class FontFormat extends JDialog {
 17
 18     private JLabel nameLb;
 19     private JLabel styleLb;
 20     private JLabel sizeLb;
 21     private JLabel presLb;
 22     private JTextField nameTx;
 23     private JTextField styleTx;
 24     private JTextField sizeTx;
 25     private JTextField presTx;
 26     private JList nameLt;
 27     private JList styleLt;
 28     private JList sizeLt;
 29     private JScrollPane jScrollPane1;
 30     private JScrollPane jScrollPane2;
 31     private JScrollPane jScrollPane3;
 32     private JButton approve;
 33     private JButton cancel;
 34     private JButton chose;
 35     private JRadioButton[] language = new JRadioButton[2];
 36     private ButtonGroup languageg;
 37     private String Slanguage[] = { new String("李涛"), new String("ABC") };
 38
 39     private static JFrame frame;
 40     public Font font, newFont;// 字体类
 41     private Color color;// 颜色类
 42     Color newColor;
 43
 44     private JFileChooser fileChoose = new JFileChooser();// 文件选择类实例
 45     private JDialog colorDlg;// 颜色对话框
 46     private JColorChooser colorChoose = new JColorChooser();// 颜色选择类实例
 47
 48     private GraphicsEnvironment environment; // 该类中又获取系统字体的方法;
 49     private String[] fontNameSet;// 字体‘逻辑名’集
 50     // 字体‘样式’集的字符串数组
 51     private String[] fontStyleSet = { "常规", "倾斜", "加粗", "倾斜 加粗" };
 52     // 字体‘样式’集的常量数组
 53     private Integer[] fontCon = { Font.PLAIN, Font.ITALIC, Font.BOLD,
 54             Font.BOLD | Font.ITALIC };
 55     // 字体‘大小’集
 56     private String[] fontSizeSet = { "6", "7", "8", "9", "10", "11", "12",
 57             "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72" };
 58
 59     public static void main(String args[]) {// 主函数
 60         FontFormat a = new FontFormat();
 61         a.setVisible(true);
 62     }
 63
 64     public FontFormat() {// 无参构造函数
 65         super(frame, "李涛—字体设置窗口", true);
 66         frame = new JFrame();
 67         initGUI();
 68     }
 69
 70     public FontFormat(JFrame frame) {// 含参构造函数
 71         super(frame, "李涛—字体设置窗口", true);
 72         this.frame = frame;// 父窗口中必须有一个public的Font对象
 73         // setAlwaysOnTop(true);
 74         initGUI();
 75     }
 76
 77     private void initGUI() {// 字体格式选择器的界面初始化
 78         try {
 79             getContentPane().setLayout(null);
 80             environment = GraphicsEnvironment.getLocalGraphicsEnvironment();// GraphicsEnvironment是一个抽象类,不能实例化,只能用其中的静态方法获取一个实例
 81             fontNameSet = environment.getAvailableFontFamilyNames();// 获取系统字体
 82             addMenu();// 加入菜单
 83             initFont();// 初始化字体
 84             // pack();
 85             setSize(380, 337);
 86             setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
 87             setWindowPos();// 使窗口屏幕居中
 88             setResizable(false);// 大小不可变
 89         } catch (Exception e) {
 90             e.printStackTrace();
 91         }
 92     }
 93
 94     private void initFont() {// 初始化字体
 95         // 设置默认字体格式为父窗口font对向的字体格式
 96         if (frame.getFont() == null) {
 97             nameTx.setText(fontNameSet[0]);
 98             styleTx.setText(fontStyleSet[0]);
 99             sizeTx.setText("12");
100             nameLt.setSelectedValue(fontNameSet[0], true);
101             styleLt.setSelectedIndex(0);
102             sizeLt.setSelectedValue("12", true);
103             font = new Font(fontNameSet[0], fontCon[0], 12);
104             newFont = font;// 保存原来的字体格式
105             presTx.setFont(font);
106             // JOptionPane.showMessageDialog(null, "ccac");
107         } else {
108             int idxStyle = 0;
109             for (int i = 0; i < fontCon.length; i++) {
110                 if (fontCon[i] == frame.getFont().getStyle())
111                     idxStyle = i;
112             }
113             nameTx.setText(frame.getFont().getName());// 改text
114             styleTx.setText(fontStyleSet[idxStyle]);
115             sizeTx.setText("" + frame.getFont().getSize());
116             nameLt.setSelectedValue(frame.getFont().getName(), true);// 改list显示
117             styleLt.setSelectedIndex(idxStyle);
118             sizeLt.setSelectedValue("" + frame.getFont().getSize(), true);
119             font = new Font(fontNameSet[0], fontCon[0], 12);// 保存当前格式
120             newFont = font;// 保存原来的字体格式
121             presTx.setFont(font);// 预览中设为当前模式
122         }
123     }
124
125     private void addMenu() {// 加入菜单
126         // 4个lable---------------------------------------------------------------------------------
127         nameLb = new JLabel();
128         getContentPane().add(nameLb);
129         nameLb.setText("字体:");
130         nameLb.setBounds(10, 14, 120, 26);
131         nameLb.setFont(new java.awt.Font("SimSun", 1, 14));
132
133         styleLb = new JLabel();
134         getContentPane().add(styleLb);
135         styleLb.setText("字型:");
136         styleLb.setBounds(151, 14, 120, 23);
137         styleLb.setFont(new java.awt.Font("SimSun", 1, 14));
138
139         sizeLb = new JLabel();
140         getContentPane().add(sizeLb);
141         sizeLb.setText("大小:");
142         sizeLb.setBounds(275, 14, 79, 24);
143         sizeLb.setFont(new java.awt.Font("SimSun", 1, 14));
144
145         presLb = new JLabel();
146         getContentPane().add(presLb);
147         presLb.setText("预览:");
148         presLb.setBounds(151, 150, 120, 80);
149         presLb.setFont(new java.awt.Font("SimSun", 1, 14));
150
151         // 4个textfield---------------------------------------------------------------------------------
152         nameTx = new JTextField();
153         nameTx.setEditable(false);
154         getContentPane().add(nameTx);
155         nameTx.setBounds(10, 42, 120, 22);
156
157         styleTx = new JTextField();
158         styleTx.setEditable(false);
159         getContentPane().add(styleTx);
160         styleTx.setBounds(151, 42, 100, 21);
161
162         sizeTx = new JTextField();
163         sizeTx.setEditable(false);
164         getContentPane().add(sizeTx);
165         sizeTx.setBounds(275, 42, 79, 22);
166
167         presTx = new JTextField();
168         presTx.setEditable(false);
169         getContentPane().add(presTx);
170         presTx.setBounds(151, 200, 203, 61);
171         presTx.setText(Slanguage[1]);
172
173         // 3个下拉条--+监听-----------------------------------------------------------------------------
174         jScrollPane1 = new JScrollPane();
175         getContentPane().add(jScrollPane1);
176         jScrollPane1.setBounds(10, 74, 120, 210);
177         {
178             ListModel fontNameModel = new DefaultComboBoxModel(fontNameSet);
179             nameLt = new JList();
180             jScrollPane1.setViewportView(nameLt);
181             nameLt.setModel(fontNameModel);
182             nameLt.setBounds(274, 193, 90, 86);
183             nameLt.setBorder(BorderFactory
184                     .createEtchedBorder(BevelBorder.LOWERED));
185             nameLt.addMouseListener(new MouseAdapter() {
186                 public void mouseClicked(MouseEvent evt) {
187                     nameLtMouseClicked(evt);
188                 }
189             });
190         }
191
192         jScrollPane2 = new JScrollPane();
193         getContentPane().add(jScrollPane2);
194         jScrollPane2.setBounds(151, 74, 100, 103);
195         {
196             ListModel fontStyleModel = new DefaultComboBoxModel(fontStyleSet);
197             styleLt = new JList();
198             jScrollPane2.setViewportView(styleLt);
199             styleLt.setModel(fontStyleModel);
200             styleLt.setBounds(310, 215, 70, 102);
201             styleLt.setBorder(BorderFactory
202                     .createEtchedBorder(BevelBorder.LOWERED));
203             styleLt.addMouseListener(new MouseAdapter() {
204                 public void mouseClicked(MouseEvent evt) {
205                     styleLtMouseClicked(evt);
206                 }
207             });
208         }
209
210         jScrollPane3 = new JScrollPane();
211         getContentPane().add(jScrollPane3);
212         jScrollPane3.setBounds(275, 75, 79, 100);
213         {
214             ListModel fontSizeModel = new DefaultComboBoxModel(fontSizeSet);
215             sizeLt = new JList();
216             jScrollPane3.setViewportView(sizeLt);
217             sizeLt.setModel(fontSizeModel);
218             sizeLt.setBounds(300, 218, 54, 102);
219             sizeLt.setBorder(BorderFactory
220                     .createEtchedBorder(BevelBorder.LOWERED));
221             sizeLt.addMouseListener(new MouseAdapter() {
222                 public void mouseClicked(MouseEvent evt) {
223                     sizeLtMouseClicked(evt);
224                 }
225             });
226         }// -------------------------------------------------------------------------------------
227
228         // 中英选项(---------------------------------------------------------------------------------
229         languageg = new ButtonGroup();
230         language[0] = new JRadioButton("中");
231         getContentPane().add(language[0]);
232         language[0].setSelected(false);// 初始化显示
233         language[0].setBounds(271, 179, 40, 20);
234         language[0].setFont(new java.awt.Font("SimSun", 1, 12));
235         languageg.add(language[0]);
236         language[0].addActionListener(new ActionListener() {
237             public void actionPerformed(ActionEvent evt) {
238                 presTx.setText(Slanguage[0]);
239             }
240         });
241
242         language[1] = new JRadioButton("英");
243         getContentPane().add(language[1]);
244         language[1].setSelected(true);
245         language[1].setBounds(321, 179, 40, 20);
246         language[1].setFont(new java.awt.Font("SimSun", 1, 12));
247         languageg.add(language[1]);
248         language[1].addActionListener(new ActionListener() {
249             public void actionPerformed(ActionEvent evt) {
250                 presTx.setText(Slanguage[1]);
251             }
252         });
253
254         // 3个按钮+监听---------------------------------------------------------------------------------
255         // 确定按钮
256         approve = new JButton();
257         getContentPane().add(approve);
258         approve.setText("确定");
259         approve.setBounds(151, 265, 67, 20);
260         approve.setFont(new java.awt.Font("KaiTi_GB2312", 1, 12));
261         approve.addActionListener(new ActionListener() {
262             public void actionPerformed(ActionEvent evt) {
263                 approveActionPerformed(evt);
264             }
265         });
266
267         // 取消按钮
268         cancel = new JButton();
269         getContentPane().add(cancel);
270         cancel.setText("取消");
271         cancel.setBounds(219, 265, 67, 20);
272         cancel.setFont(new java.awt.Font("KaiTi_GB2312", 1, 12));
273         cancel.addActionListener(new ActionListener() {
274             public void actionPerformed(ActionEvent evt) {
275                 cancelActionPerformed(evt);
276             }
277         });
278
279         // 颜色选择按钮
280         chose = new JButton();
281         getContentPane().add(chose);
282         chose.setText("颜色");
283         chose.setBounds(287, 265, 67, 20);
284         chose.setFont(new java.awt.Font("KaiTi_GB2312", 1, 12));
285         chose.addActionListener(new ActionListener() {
286             public void actionPerformed(ActionEvent evt) {
287                 choseActionPerformed(evt);
288             }
289         });// -------------------------------------------------------------------------
290     }
291
292     private void setWindowPos() {// 窗口居中
293         Toolkit kit = Toolkit.getDefaultToolkit();// 抽象类,通过静态方法获取实例
294         Dimension frameSize = new Dimension(), screenSize = kit.getScreenSize(); // 获取屏幕的大小
295         getSize(frameSize); // 获取窗口大小
296         setLocation((screenSize.width - frameSize.width) / 2,
297                 (screenSize.height - frameSize.height) / 2);
298     }
299
300     private void nameLtMouseClicked(MouseEvent evt) {// 字体逻辑名列表的鼠标单击事件
301         nameTx.setText(nameLt.getSelectedValue().toString());
302         font = new Font(nameTx.getText(), font.getStyle(), font.getSize());
303         presTx.setFont(font);
304     }
305
306     private void styleLtMouseClicked(MouseEvent evt) {// 字体样式列表的鼠标单击事件
307         String temp = styleLt.getSelectedValue().toString();
308         styleTx.setText(temp);
309         int index = 0;
310         while (index < 4 && !fontStyleSet[index].equals(temp)) {
311             index++;
312         }
313         font = new Font(font.getName(), fontCon[index], font.getSize());
314         presTx.setFont(font);
315     }
316
317     private void sizeLtMouseClicked(MouseEvent evt) {// 字体大小列表的鼠标点击事件
318         sizeTx.setText(sizeLt.getSelectedValue().toString());
319         font = new Font(font.getName(), font.getStyle(),
320                 Integer.parseInt(sizeTx.getText()));
321         presTx.setFont(font);
322     }
323
324     private void approveActionPerformed(ActionEvent evt) {// 确定按钮的触发事件
325         String name = nameTx.getText();
326         int style = fontCon[styleLt.getSelectedIndex()];
327         int size = Integer.parseInt(sizeTx.getText());
328         font = new Font(name, style, size);
329         frame.setFont(font); // 父窗口的Font对象
330         newFont = font;// 更新原来保存格式
331         newColor = color;// 更新颜色
332         this.dispose();
333     }
334
335     private void cancelActionPerformed(ActionEvent evt) {// 取消按钮的触发事件
336         this.dispose();
337     }
338
339     private void choseActionPerformed(ActionEvent evt) {// 颜色选择触发事件
340         if (colorDlg == null) {
341             colorDlg = JColorChooser.createDialog(FontFormat.this,
342                     "Select Text Color", true, colorChoose,
343                     new ColorOKListener(), null);
344         }
345         colorChoose.setColor(color = presTx.getForeground());
346         colorDlg.setVisible(true);
347     }
348
349     class ColorOKListener implements ActionListener {// 重写颜色按钮点击监听类覆盖接口ActionListener
350         public void actionPerformed(ActionEvent e) {
351             Color c = colorChoose.getColor();
352             color = c;
353             presTx.setForeground(c);
354             presTx.repaint();
355         }
356     }
357 }
本文转自beautifulzzzz博客园博客,原文链接:http://www.cnblogs.com/zjutlitao/p/3554815.html,如需转载请自行联系原作者

[JAVA] java仿windows 字体设置选项卡相关推荐

  1. matlab怎么恢复默认字体,如何重置Windows字体设置并恢复默认字体 | MOS86

    与之前版本的操作系统相比, Microsoft大大提高了Windows 8的启动时间. 这是伟大的,这并不意味着启动时间不会增加.如果您安装了自动启动程序,或者安装了太多的字体,那么您可能会注意到,无 ...

  2. java动态生成ppt字体设置

    我用java动态生成ppt,设置文本的字体为"华文楷体",如果文本是英文,则英文的字体就是华文楷体,但是如果是中文,中文的字体还是宋体,没有发生变化.请教高手们,这个该怎么修改呢? ...

  3. 记事本改字体的代码java_求java记事本代码(带字体设置功能)?

    java中没有自带的字体对话框,这需要自己来编写. text.setFond("字体名字",字形(如,fond.bold),大小) import java.awt.*; impor ...

  4. 《游戏学习》Java版仿windows扫雷小游戏源码

    项目代码目录结构 启动类 StartFrame  代码展示 import java.awt.BorderLayout; import java.awt.Font;import javax.swing. ...

  5. ubuntu使用windows字体设置

    转自 https://blog.csdn.net/ranweizheng/article/details/82993081 本篇文章主要介绍怎样在ubuntu系统中安装和windows相同的字体! u ...

  6. 在linux下修改emacs颜色字体,Emacs的字体设置方法总结

    Emacs的字体设置方法总结 在 X 下使用 emacs 时,如果字体配置不好,emacs 就会显得十分丑陋.而作为 emacs 的用户,每天相当长的时间都在盯着这个编辑器,如果字体不好看,对眼 睛是 ...

  7. Emacs字体设置方法总结

    在 X 下使用 emacs 时,如果字体配置不好,emacs 就会显得十分丑陋.而作为 emacs 的用户,每天相当长的时间都在盯着这个编辑器,如果字体不好看,对眼 睛是一种严重的伤害.除非有严重的自 ...

  8. Java——速学界面设计之仿Windows记事本的字体窗口

    通过这学期伍老师的教学以及布置的作业之猜数字和计算器,对Java的界面设计颇为感兴趣,基于此,我就想做点更有意思的东西,所以仿照Windows字体窗口的设计做了一点点东西,中间出现了许多为本人也是新学 ...

  9. java 仿照windows的记事本_Java仿Windows记事本源代码分享

    本文实例为大家分享了Java仿Windows记事本的具体代码,供大家参考,具体内容如下 先上截图: 源代码: import java.awt.*; import javax.swing.*; impo ...

最新文章

  1. SCCM 2007系列4 播发操作系统上
  2. 线程:方法join的使用
  3. centos7安装mysql,安装easy install、pip、scrapy等
  4. OpenCASCADE绘制测试线束:几何命令之Intersections
  5. Js String转Int(Number与parseInt的区别)
  6. hadoop二次排序
  7. 花2.9元买一包头绳,收到一张3元好评返现卡,我凌乱了……
  8. 天梯—是不是太胖了(C语言)
  9. NYOJ260 - 数数小木块
  10. Spring框架配置文件 application.xml 示例
  11. 在Windows中玩转Docker Toolbox
  12. TextCNN pytorch实现
  13. 脉冲压缩原理以及实验代码详解
  14. 计算机读研的收获和遗憾
  15. Spring Boot2.0 - 玩转logback日志
  16. html图片排版技巧,【CSS技巧】多图片的垂直居中排版
  17. 世界易学大会副主席孙志华斩获非全日制易学博士,倾情分享易学奥妙
  18. excel不显示0_Excel数值为0不显示的三种解决方法
  19. android实现悬停效果代码,Android StickListView实现悬停效果
  20. 如何制订工作计划与工作目标?

热门文章

  1. 【经验】对一个合格C++高级工程师(音视频方向)的要求
  2. java的关键字和保留字_「Java」详解常见的53个关键字
  3. linux ps命令大全,Linux ps命令例子汇总
  4. c语言中变量有什么作用是什么,C语言里面局部变量和临时变量有什么区别?
  5. 【js】实现分页查询操作的步骤
  6. 【jquery】jquery选择器
  7. 【HTML】兴唐二十八节课之常用标签(不定期更新)
  8. chrome 浏览器打开静态html 获取json文件失败 解决方法
  9. qt 多个模型如何显示在表格中_Qt MOOC系列教程 第五章第四节:QML中的C++模型
  10. 基本数据结构(图: 基本结构,DFS,prim算法, kruskal算法)