-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fontdialogd.java
425 lines (324 loc) · 12.2 KB
/
Fontdialogd.java
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.font.TextAttribute;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Fontdialogd extends JDialog implements ActionListener,ListSelectionListener{
Fontnames fonts;
String name[];
JTextField fontstylete,fontsizete;
JTextField size;
JComboBox FontC,Size,ColorC,pianyiEm;
JList fontstylelist,fontsizelist;
JButton button1,button2;
JCheckBox jcb1,jcb2,wutexiao;
JRadioButton jrb1,jrb2;
JLabel viewla;
int font_size,font_style_bold,font_styple_italic;
String font_type;
String selectedfont;
Font selectedstyle;
int selectedsize;
int i=20;
String fst,s;
Container con=this.getContentPane();
String[] styles = { "常规", "加粗", "斜体", "加粗斜体" };
String[] sizes = { "10", "14", "18", "22", "26", "30" };
static Font font;
HashMap<TextAttribute, Object> hm2 = new HashMap<TextAttribute, Object>();
public Fontdialogd(JFrame own,String title,boolean b)
{
super(own,title ,b);
this.setResizable(false);
//居中
int windowW=470;
int windowH=370;
Dimension tool=Toolkit.getDefaultToolkit().getScreenSize();
int screenW=tool.width;
int screenH=tool.height;
this.setLocation(screenW/2-windowW/2, screenH/2-windowH/2);
this.setSize(470,370);
JPanel p=new JPanel();
p.setLayout(new GridLayout(2,1));
JPanel p1=new JPanel();
p1.setLayout(new GridLayout(1,4));
JPanel p2=new JPanel();
p2.setLayout(new GridLayout(1,2));
JPanel fontpane=new JPanel();
fontpane.setLayout(new GridLayout(3,1));
fontpane.setBorder(BorderFactory.createEmptyBorder(0,5,0,5));
JPanel colorpane=new JPanel();
colorpane.setLayout(new GridLayout(3,1));
colorpane.setSize(10,5);
colorpane.setBorder(BorderFactory.createEmptyBorder(10,50,0,30));
Box fontbox=Box.createVerticalBox();
FontC=new JComboBox();
JLabel fontla= new JLabel("字体");
fontbox.add(fontla);
// Font.setPreferredSize(new Dimension(2,25));
fontbox.add(Box.createVerticalStrut(5));
fontbox.add(FontC);
fontpane.add(fontbox);
fonts=new Fontnames();
name=fonts.getFont();
FontC.addItem("楷体");
for(int i=0;i<name.length;i++)
FontC.addItem(name[i]);
FontC.addActionListener(this);
JPanel Fontsize=new JPanel();
Fontsize.setBorder(BorderFactory.createEmptyBorder(5,10, 5, 0));
fontsizelist=new JList(sizes);
fontsizelist.addListSelectionListener(this);
fontsizelist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fontsizelist.setVisibleRowCount(6);
fontsizete=new JTextField(sizes[0]);
JScrollPane jspfontsize = new JScrollPane(fontsizelist);
Fontsize.setLayout(new BoxLayout(Fontsize, BoxLayout.PAGE_AXIS));
Fontsize.add(fontsizete);
Fontsize.add(jspfontsize);
JPanel Fontstyle=new JPanel();
Fontstyle.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 0));
fontstylelist=new JList(styles);
fontstylelist.setName("style");
fontstylelist.addListSelectionListener(this);
fontstylelist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fontstylelist.setVisibleRowCount(6);
fontstylete=new JTextField(styles[0]);
JScrollPane jspfontstyle = new JScrollPane(fontstylelist);
Fontstyle.setLayout(new BoxLayout(Fontstyle, BoxLayout.PAGE_AXIS));
Fontstyle.add(fontstylete);
Fontstyle.add(jspfontstyle);
button1=new JButton("确定");
button1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str=writebroad.text.getText();
JLabel strla=new JLabel(str);
writebroad.text.setFont(viewla.getFont());
writebroad.text.setForeground(viewla.getForeground());
if(jcb1.isSelected())
{
hm2.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); // 定义下划线
font = new Font(hm2);
writebroad.text.setFont(font);
}
else if(jcb2.isSelected())
{
hm2.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON); // 定义删除线
font = new Font(hm2);
writebroad.text.setFont(font);
}
else if(jrb1.isSelected())
{
writebroad.text.setFont(font);
String ss=writebroad.text.getText();
writebroad.text.setText(new letertochange().ChangetoLO(ss));
}
else if(jrb2.isSelected())
{
writebroad.text.setFont(font);
String ss=writebroad.text.getText();
writebroad.text.setText(new letertochange().ChangetoUP(ss));
}
setVisible(false);
}
});
button2=new JButton("取消");
button2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
dispose();
}
});
JPanel Buttontwo=new JPanel();
Box box=Box.createVerticalBox();
box.add(button1);
box.add(Box.createVerticalStrut(7));
box.add(button2);
Buttontwo.add(box);
Buttontwo.setBorder(BorderFactory.createEmptyBorder(10,10,0,5));
JPanel xiaoguo=new JPanel();
xiaoguo.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
xiaoguo.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.CYAN),"效果"));
jcb1=new JCheckBox("下划线(U)");
jcb2=new JCheckBox("删除线(S)");
wutexiao=new JCheckBox("无特效");
jrb1=new JRadioButton("全部大写(A)");
jrb2=new JRadioButton("全部小写(S)");
ButtonGroup g=new ButtonGroup();
g.add(jcb1);
g.add(jcb2);
g.add(wutexiao);
wutexiao.setSelected(true);
jcb1.addItemListener(new jchandler());
jcb2.addItemListener(new jchandler());
jrb1.addItemListener(new jchandler());
jrb2.addItemListener(new jchandler());
Box boxleft=Box.createVerticalBox();
Box boxright=Box.createVerticalBox();
Box boxH=Box.createHorizontalBox();
boxH.add(boxleft);
boxH.add(Box.createHorizontalStrut(10));
boxH.add(boxright);
boxleft.add(jcb1);
boxright.add(jrb1);
boxleft.add(jcb2);
boxleft.add(wutexiao);
boxright.add(jrb2);
boxright.add(jrb1);
boxright.add(jrb2);
xiaoguo.add(boxH);
p2.add(xiaoguo);
//颜色常量
final Color[] colors = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY,
Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA,
Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };
String[] colornames = { "BLACK", "BLUE", "CYAN", "DARK_GRAY", "GRAY",
"GREEN", "LIGHT_GRAY", "MAGENTA", "ORANGE", "PINK", "RED", "WHITE",
"YELLOW" };
ColorC = new JComboBox();
for(int i=0;i<colornames.length;i++)
ColorC.addItem(colornames[i]);
ColorC.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
viewla.setForeground(colors[ColorC.getSelectedIndex()]);
}
});
Box boxcolor=Box.createVerticalBox();
boxcolor.add(new JLabel("颜色"));
boxcolor.createVerticalStrut(5);
boxcolor.add(ColorC);
colorpane.add(boxcolor);
p2.add(colorpane);
JPanel view=new JPanel();
view.setBorder(BorderFactory.createLoweredBevelBorder());
view.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
viewla=new JLabel("--- 字体的预览AAaa ----");
viewla.setFont(new Font("楷体",Font.BOLD,20));
view.add(viewla, BorderLayout.CENTER);
p1.add(fontpane);
p1.add(Fontsize);
p1.add(Fontstyle);
p1.add(Buttontwo);
p.add(p1);
p.add(p2);
con.add(p,BorderLayout.CENTER);
con.add(view,BorderLayout.SOUTH);
}
class jchandler implements ItemListener
{
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jcb1)
{
viewla.setFont(font);
if(jcb1.isSelected())
viewla.setText("<html><u>--- 字体的预览AAaa ----</u></html>");
else
viewla.setText("--- 字体的预览AAaa ----");
}
else if(e.getSource()==jcb2)
{
viewla.setFont(font);
if(jcb2.isSelected())
viewla.setText("<html><strike>--- 字体的预览AAaa ----</html></strike>");
else
viewla.setText("--- 字体的预览AAaa ----");
}
else if(e.getSource()==jrb1)
{
viewla.setFont(font);
if(jrb1.isSelected())
viewla.setText("--- 字体的预览AAAA ----");
else
viewla.setText("--- 字体的预览AAaa ----");
}
else if(e.getSource()==jrb2)
{
viewla.setFont(font);
if(jrb2.isSelected())
viewla.setText("--- 字体的预览aaaa ----");
else
viewla.setText("--- 字体的预览AAaa ----");
}
}
}
@Override
public void actionPerformed(ActionEvent e)
{
s=name[FontC.getSelectedIndex()];
hm2.put(TextAttribute.FAMILY, s);
font = new Font(hm2);
viewla.setFont(font);
}
@Override
public void valueChanged(ListSelectionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource()==fontsizelist)
{
i=Integer.valueOf(sizes[fontsizelist.getSelectedIndex()]);
hm2.put(TextAttribute.SIZE,i);
font = new Font(hm2);
viewla.setFont(font);
fontsizete.setText(String.valueOf(i));
}
else if(arg0.getSource()==fontstylelist)
{
fst=styles[fontstylelist.getSelectedIndex()];
if(fst.equals("常规"))
viewla.setFont(new Font(s,Font.PLAIN,i));
else if(fst.equals("加粗") )
viewla.setFont(new Font(s,Font.BOLD,i));
else if(fst.equals("斜体"))
viewla.setFont(new Font(s,Font.ITALIC,i));
else if(fst.equals("加粗斜体") )
viewla.setFont(new Font(s,Font.BOLD+Font.ITALIC,i));
fontstylete.setText(fst);
}
}
}