服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - java实现仿windows 字体设置选项卡实例

java实现仿windows 字体设置选项卡实例

2020-06-24 12:11beautifulzzzz JAVA教程

本篇文章介绍了java仿windows 字体设置选项卡,可实现类似windows字体设置效果,需要的朋友可以参考下。

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

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

效果如下:

java实现仿windows 字体设置选项卡实例

希望本文所述对你有所帮助,java仿windows 字体设置选项卡内容就给大家介绍到这里了。希望大家继续关注我们的网站!想要学习java可以继续关注本站。

延伸 · 阅读

精彩推荐