-
Notifications
You must be signed in to change notification settings - Fork 1
/
writebroad.java
1005 lines (742 loc) · 27.6 KB
/
writebroad.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.UndoManager;
public class writebroad extends JFrame implements MouseListener,ActionListener{
JMenuBar menubar;
static JMenuItem print;
JMenu file,editor,format,visible,help,zidingyi,zidingyibeijing,shubiao;
JMenuItem newfile,open,save,othersave,exit,
jianqie, fuzhi, zhantie,shanchu, chazhaoxia,
swap,zhuandao,tongji,allselect,time, findall,autohuanhang,ziti,
zhuangtai, chakanbanzhu, guanyu,zidingyicolor,xuanzebeijing,chuizhi,shizhijia,moren,shoushi,shuipin;
JPopupMenu popupmenu;
JMenuItem jianqie2,fuzi2,zhantie2,shanchu2,recover,vUnicode;
static JMenuItem chexiao2,chexiao;
Clipboard clipboard=this.getToolkit().getSystemClipboard();
JFileChooser filedialog;
JLabel position, timetip;
JPanel infor;
static JTextArea text;
String filesavename;
boolean flag=false;
static boolean findflag=false;
ImageIcon imageIcon ;
static UndoManager undomg = new UndoManager();
public writebroad()
{
this.setTitle("记事本");
//Icon img = new ImageIcon(getClass().getResource("bg.jpg"));
//Image img = kit.getImage("image//1.png");
//设置鼠标的图片
this.getContentPane().setCursor(getMyCursor());
Toolkit tk=Toolkit.getDefaultToolkit();
Image image=tk.createImage("title2.png");
this.setIconImage(image);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
int result=JOptionPane.showConfirmDialog(null, "是否将更改的文件保存?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION);
if(result==JOptionPane.YES_OPTION)
{
othersave.doClick();
}
else if(result==JOptionPane.CANCEL_OPTION)
{
return;
}
}
} );
menubar=new JMenuBar();
popupmenu=new JPopupMenu();
filedialog=new JFileChooser(".");
text = new JTextArea() {
public void paint(Graphics g) {
imageIcon = new ImageIcon(imagechooser.temp);
g.drawImage(imageIcon.getImage(), 0, 0,this.getWidth(),this.getHeight(), this);
super.paint(g);
}
};
text.setFont(new Font("楷体",Font.BOLD,15));
text.add(popupmenu);
text.addMouseListener(this);
//查找全部标记时,点击文本域,取消标记
text.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
if(findflag==true)
findAll.highlighter.removeAllHighlights();
}
});
text.addKeyListener(new KeyListener()
{
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource()!=null)
{
if(filesavename!=null)
{
writebroad.this.setTitle(filesavename+"**--记事本");
return;
}
else
{
writebroad.this.setTitle("未命名**--记事本");
return;
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
file=new JMenu("文件(F)");
file.setMnemonic('F');
editor=new JMenu("编辑(E)");
editor.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent ee)
{
zhuandao.setEnabled(canZhuandao());
}
});
editor.setMnemonic('E');
format=new JMenu("格式(O)");
format.setMnemonic('O');
visible=new JMenu("查看(V)");
visible.setMnemonic('V');
help=new JMenu("帮助(H)");
help.setMnemonic('H');
newfile=new JMenuItem("新建(N)");
newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
newfile.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
// TODO Auto-generated method stub
int result=JOptionPane.showConfirmDialog(null, "是否将更改的文件保存?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION);
if(result==JOptionPane.YES_OPTION)
{
othersave.doClick();
}
else if(result==JOptionPane.CANCEL_OPTION)
{
return;
}
writebroad.this.setTitle("未命名**--记事本");
text.setText(null);
}
});
open=new JMenuItem("打开(O)");
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
open.addActionListener(this);
save=new JMenuItem("保存(S)");
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
save.addActionListener(this);
othersave=new JMenuItem("另存为(A)");
othersave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
othersave.addActionListener(this);
print=new JMenuItem("打印(P)");
print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
print.addActionListener(new writebroadMain());
exit=new JMenuItem("退出(E)");
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
exit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
int result=JOptionPane.showConfirmDialog(null, "是否将更改的文件保存?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION);
if(result==JOptionPane.YES_OPTION)
{
othersave.doClick();
}
else if(result==JOptionPane.CANCEL_OPTION)
{
return;
}
System.exit(0);
}
});
chexiao=new JMenuItem("撤销(Z)");
chexiao.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK));
text.getDocument().addUndoableEditListener(new UndoableEditListener()
{
@Override
public void undoableEditHappened(UndoableEditEvent arg0) {
// TODO Auto-generated method stub
undomg.addEdit(arg0.getEdit());
}
});
chexiao.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(undomg.canUndo()) {
undomg.undo();
} else {
JOptionPane.showMessageDialog(null,"系统无内容可以撤销了,导致无法撤销","警告",JOptionPane.WARNING_MESSAGE);
}
}
});
jianqie=new JMenuItem("剪切(T)");
jianqie.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
jianqie.addActionListener(new operater());
fuzhi=new JMenuItem("复制(C)");
fuzhi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
fuzhi.addActionListener(new operater());
zhantie=new JMenuItem("粘贴(P)");
zhantie.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
zhantie.addActionListener(new operater());
shanchu=new JMenuItem("删除(D)");
shanchu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));
shanchu.addActionListener(new operater());
findall=new JMenuItem("查找全部(A)");
findall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
findall.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
findAll f=new findAll(null,"查找全部",true);
f.setVisible(true);
}
});
chazhaoxia=new JMenuItem("查找下一个(N)");
chazhaoxia.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
chazhaoxia.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
text.setCaretPosition(0);
MyFind f=new MyFind(null,"查找",true);
f.setVisible(true);
}
});
swap=new JMenuItem("替换(R)");
swap.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK));
swap.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
tihuan t=new tihuan(null,"替换",true);
t.setVisible(true);
}
});
zhuandao=new JMenuItem("转到(N)");
zhuandao.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
zhuandao.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
zhuandao dialog=new zhuandao(writebroad.this,"转到行",true);
dialog.setVisible(true);
}
});
allselect=new JMenuItem("全选(A)");
allselect.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
allselect.addActionListener(new operater());
popupmenu.add(allselect);
tongji=new JMenuItem("统计(T)");
tongji.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,InputEvent.CTRL_MASK));
tongji.addActionListener(new operater());
tongji.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
tongji ji=new tongji(null,"统计",true);
ji.setVisible(true);
}
});
time=new JMenuItem("时间/日期(D)");
time.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));
time.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
// 获得当前光标位置
int cur = text.getCaretPosition();
// 得到光标之后的字符串
String tailString = text.getText().substring(cur);
// 得到光标之前的字符串
String headString = text.getText().substring(0,cur);
// 拼接字符串 并输出
text.setText(headString + new timeinfor().temptime2 + tailString);
}
});
autohuanhang=new JMenuItem("自动转行(W)");
autohuanhang.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,InputEvent.CTRL_MASK));
autohuanhang.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
text.setLineWrap(true);
JOptionPane.showMessageDialog(null, "系统已根据窗口的大小进行换行");
}
});
ziti=new JMenuItem("字体(F)");
ziti.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));
ziti.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Fontdialogd di=new Fontdialogd(null,"字体",true);
di.setVisible(true);
}
});
zhuangtai=new JMenuItem("状态栏(S)");
zhuangtai.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
zhuangtai.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
infor.setVisible(true);
}
});
chakanbanzhu=new JMenuItem("查看帮助(H)");
chakanbanzhu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK));
guanyu=new JMenuItem("关于记事本(A)");
guanyu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
guanyu.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
about about=new about(null,"关于作者",true);
about.setVisible(true);
}
});
zidingyi=new JMenu("自定义设计(D)");
zidingyi.setMnemonic('D');
zidingyibeijing=new JMenu("其他(O)");
zidingyibeijing.setMnemonic('O');
zidingyicolor=new JMenuItem("背景颜色(F1)");
zidingyicolor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0));
zidingyicolor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Color newColor=JColorChooser.showDialog(null,"背景调色板",text.getBackground());
text.setBackground(newColor);
if(newColor==null)
{
JOptionPane.showMessageDialog(null, "您未选择任何颜色,系统已为您设为无色");
}
}
});
xuanzebeijing=new JMenuItem("选择图片背景(F2)");
xuanzebeijing.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2,0));
xuanzebeijing.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
imagechooser im=new imagechooser(null,"请选择背景图片",true);
im.setVisible(true);
zidingyicolor.setEnabled(false);
JOptionPane.showMessageDialog(null, "您已选择了背景图样式,系统已关闭背景颜色操作");
}
});
//chart.setBackgroundImage(image);
shubiao=new JMenu("个性鼠标(S)");
shubiao.setMnemonic('S');
shizhijia=new JMenuItem("十字架");
shizhijia.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_J,InputEvent.SHIFT_MASK));
shoushi=new JMenuItem("手势");
shoushi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.SHIFT_MASK));
shuipin=new JMenuItem("水平");
shuipin.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,InputEvent.SHIFT_MASK));
chuizhi=new JMenuItem("垂直");
chuizhi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.SHIFT_MASK));
moren=new JMenuItem("默认样式");
moren.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M,InputEvent.SHIFT_MASK));
shizhijia.addActionListener(this);
moren.addActionListener(this);
shoushi.addActionListener(this);
shuipin.addActionListener(this);
chuizhi.addActionListener(this);
chexiao2=new JMenuItem("撤销(Z)");
popupmenu.add(chexiao2);
popupmenu.addSeparator();
chexiao2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.SHIFT_MASK));
chexiao2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(undomg.canUndo()) {
undomg.undo();
} else {
JOptionPane.showMessageDialog(null,"系统无内容可以撤销","警告",JOptionPane.WARNING_MESSAGE);
}
}
});
jianqie2=new JMenuItem("剪切(J)");
popupmenu.add(jianqie2);
jianqie2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.SHIFT_MASK));
jianqie2.addActionListener(new operater());
fuzi2=new JMenuItem("复制(C)");
popupmenu.add(fuzi2);
fuzi2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.SHIFT_MASK));
fuzi2.addActionListener(new operater());
zhantie2=new JMenuItem("粘贴(P)");
popupmenu.add(zhantie2);
popupmenu.addSeparator();
zhantie2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.SHIFT_MASK));
zhantie2.addActionListener(new operater());
shanchu2=new JMenuItem("删除(D)");
popupmenu.add(shanchu2);
popupmenu.addSeparator();
shanchu2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.SHIFT_MASK));
shanchu2.addActionListener(new operater());
recover=new JMenuItem("恢复到撤消前状态(R)");
popupmenu.add(recover);
recover.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.SHIFT_MASK));
recover.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(undomg.canRedo()) {
undomg.redo();
} else {
// JOptionPane.showMessageDialog(null,"您未撤销内容了,导致系统无法继续恢复","警告",JOptionPane.WARNING_MESSAGE);
recover.setEnabled(false);
}
}
});
vUnicode =new JMenuItem("选中单个字符显示Unicode控制字符(U)");
popupmenu.add(vUnicode);
vUnicode.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,InputEvent.SHIFT_MASK));
vUnicode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s=text.getSelectedText();
if(s==null)
{
JOptionPane.showMessageDialog(null,"您未选中任何内容");
return;
}
else if(s.length()>1)
{
JOptionPane.showMessageDialog(null,"请用鼠标选择一个字符");
return;
}
// 获得当前光标位置
final int cur = text.getCaretPosition()+s.length();
// 得到光标之后的字符串
String tailString = text.getText().substring(cur);
// 得到光标之前的字符串
String headString = text.getText().substring(0,cur);
// 拼接字符串 并输出
char[] c=s.toCharArray();
for(int i=0;i<c.length;i++)
{
text.setText(headString + "\n"+c[i]+"控制字符是:" +(int)c[i]+ "\n"+tailString);
}
}
});
JScrollPane js=new JScrollPane(text,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
position=new JLabel();
timetip=new JLabel();
timetip.setText(new timeinfor().temptime);
infor=new JPanel();
infor.setVisible(false);
infor.setLayout(new GridLayout(1,2));
infor.add(timetip);
infor.add(position);
timetip.setFont(new Font("宋体",Font.PLAIN,12));
position.setFont(new Font("宋体",Font.PLAIN,12));
Container con=this.getContentPane();
con.add(menubar,BorderLayout.NORTH);
con.add(js,BorderLayout.CENTER);
con.add(infor,BorderLayout.SOUTH);
menubar.add(file);
menubar.add(editor);
menubar.add(format);
menubar.add(visible);
menubar.add(help);
menubar.add(zidingyi);
file.add(newfile);
file.add(open);
file.add(save);
file.add(othersave);
file.addSeparator();
file.add(print);
file.addSeparator();
file.add(exit);
editor.add(chexiao);
editor.addSeparator();
editor.add(jianqie);
editor.add(fuzhi);
editor.add(zhantie);
editor.add(shanchu);
editor.addSeparator();
fuzhi.setEnabled(canCutCopy());
jianqie.setEnabled(canCutCopy());
zhantie.setEnabled(canPaste());
editor.add(findall);
editor.add(chazhaoxia);
editor.add(swap);
editor.add(zhuandao);
editor.addSeparator();
editor.add(allselect);
editor.add(tongji);
editor.addSeparator();
editor.add(time);
format.add(autohuanhang);
format.add(ziti);
visible.add(zhuangtai);
help.add(guanyu);
help.addSeparator();
help.add(chakanbanzhu);
zidingyi.add(zidingyicolor);
zidingyi.add(zidingyibeijing);
zidingyibeijing.add(xuanzebeijing);
zidingyibeijing.add(shubiao);
shubiao.add(moren);
shubiao.add(shoushi);
shubiao.add( shizhijia);
shubiao.add(shuipin);
shubiao.add(chuizhi);
this.setSize(800,600);
}
//设置鼠标的图片
private Cursor getMyCursor() {
// TODO Auto-generated method stub
Image img = new ImageIcon(this.getClass().getResource("1.png")).getImage();
return this.getToolkit().createCustomCursor(img,new Point(16,16),"mycursor");
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getButton()==MouseEvent.BUTTON3)
{
int x=arg0.getX();
int y=arg0.getY();
popupmenu.show(arg0.getComponent(),x,y);
jianqie2.setEnabled(canCutCopy());
fuzi2.setEnabled(canCutCopy());
zhantie2.setEnabled(canPaste());
}
if(arg0.getButton()==MouseEvent.BUTTON1||arg0.getButton()==MouseEvent.BUTTON3)
{
int LocationX=arg0.getX();
int LocationY=arg0.getY();
position.setText(" | 智能插入: 第"+LocationX+"行 第"+LocationY+"列");
}
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
class operater implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e ) {
// TODO Auto-generated method stub
if(e.getSource()==jianqie||e.getSource()==jianqie2)
{
text.cut();
}
else if(e.getSource()==fuzhi||e.getSource()==fuzi2)
{
text.copy();
}
else if(e.getSource()==zhantie||e.getSource()==zhantie2)
{
text.paste();
}
else if(e.getSource()==allselect)
{
text.selectAll();
}
else if(e.getSource()==shanchu||e.getSource()==shanchu2)
{
if(text.getSelectedText()!=null)
text.replaceSelection("");
else
JOptionPane.showMessageDialog(null, "您未选中任何内容");
}
}
}
//判断能否使用剪切和复制功能,如果文本区中有选中的内容,可以剪切和复制
private boolean canCutCopy()
{
boolean flag=false;
if(text.getSelectedText()!=null && !text.getSelectedText().equals(""))
{
flag=true;
}
return flag;
}
private boolean canZhuandao()
{
boolean flag=false;
if(text.getText()!=null&& !text.getText().equals(""))
{
flag=true;
}
return flag;
}
//判断剪贴板中是否有内容,如果有内容,则可以使用粘贴功能
private boolean canPaste()
{
boolean flag = false;
Transferable content = clipboard.getContents(this); //获得剪贴板的内容
try
{
if (content.getTransferData(DataFlavor.stringFlavor) instanceof String) //如果剪贴板中的数据是字符串
{
flag = true;
}
}
catch (Exception e)
{
}
return flag;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==open)
{
int result=JOptionPane.showConfirmDialog(this, "是否将更改的文件保存?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION);
if(result==JOptionPane.YES_OPTION)
{
othersave.doClick();
}
else if(result==JOptionPane.CANCEL_OPTION)
{
return;
}
String s;
if(filedialog.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
try
{
text.setText(null);
File file=filedialog.getSelectedFile();
String filename=file.getName();
FileReader file_reader=new FileReader(file);
BufferedReader in=new BufferedReader(file_reader);
while((s=in.readLine())!=null)
{
text.append(s+"\n");
}
in.close();
file_reader.close();
writebroad.this.setTitle(filename);
}
catch(Exception e1)
{
System.out.println(e.toString());
}
}
}
if(e.getSource()==save&&filesavename!=null)//保存
{
try
{
FileWriter fw=new FileWriter(filesavename);//t102output.txt你想要存放的文件名,可以任意。
fw.write(text.getText());
fw.close();
writebroad.this.setTitle(filesavename);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
if(e.getSource()==othersave||(e.getSource()==save&&filesavename==null))
{
if(filedialog.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
{
try
{
File file=filedialog.getSelectedFile();
filesavename=file.getName();
FileWriter tofile=new FileWriter(file);
String s=text.getText();
tofile.write(s);
tofile.close();
writebroad.this.setTitle(filesavename);
}
catch(Exception e2)
{
System.out.println(e.toString());
}
}
}
else if(e.getSource()==shizhijia){
text.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
else if(e.getSource()==moren){
text.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
else if(e.getSource()==shoushi){
text.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(e.getSource()==shuipin){
text.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
}
else if(e.getSource()==chuizhi){