java仿windows 字体设置选项卡

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

java仿windows 字体设置选项卡
  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 }
java仿windows 字体设置选项卡

java仿windows 字体设置选项卡

上一篇:ArcGis API JS 4.X本地化部署与地图的基础使用


下一篇:安卓文件存储使用参考