java仿windows记事本小程序

时间:2021-09-13 03:27:00

本文实例为大家分享了java仿windows记事本小程序的具体代码,供大家参考,具体内容如下

?
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
import java.awt.checkboxmenuitem;
import java.awt.font;
import java.awt.frame;
import java.awt.menu;
import java.awt.menubar;
import java.awt.menuitem;
import java.awt.menushortcut;
import java.awt.textarea;
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.windowadapter;
import java.awt.event.windowevent;
import java.awt.peer.buttonpeer;
import java.io.bufferedreader;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.outputstream;
import java.io.stringreader;
import java.io.stringwriter;
import java.sql.date;
import java.text.simpledateformat;
 
import javax.print.docflavor.string;
import javax.script.scriptexception;
import javax.swing.borderfactory;
import javax.swing.boxlayout;
import javax.swing.buttongroup;
import javax.swing.jfilechooser;
import javax.swing.jframe;
import javax.swing.joptionpane;
import javax.swing.jpanel;
import javax.swing.jradiobutton;
import javax.swing.jscrollbar;
import javax.swing.jscrollpane;
import javax.swing.jtextarea;
import javax.swing.border.etchedborder;
import javax.swing.border.titledborder;
import javax.swing.event.ancestorlistener;
import javax.swing.filechooser.filenameextensionfilter;
 
public class crazy_15_4 {
 private jframe f = new jframe("记事本");
 
 //成员变量
 static string shearplate = null;
 static boolean isautowrap = true;//是否自动换行
 jtextarea ta = new jtextarea(20, 60);
 jscrollpane jsp = new jscrollpane(ta);
 private menubar mb = new menubar();
 public string fontstr = "宋体";
 public string shapestr = "0";
 public string sizestr = "20";
 font font = new font(fontstr, integer.parseint(shapestr),integer.parseint(sizestr) );
 
 
 //menu
 menu file = new menu("文件");
 menu edit = new menu("编辑");
 menu format = new menu("格式");
 
 //menuitem
 menuitem newitem = new menuitem("新建");
 menuitem openitem = new menuitem("打开");
 menuitem saveitem = new menuitem("保存");
 menuitem saveasitem = new menuitem("另存为");
 menuitem cutitem = new menuitem("剪切");
 menuitem copyitem = new menuitem("复制");
 menuitem pasteitem = new menuitem("粘贴");
 checkboxmenuitem autowrap = new checkboxmenuitem("自动换行",isautowrap);
 menuitem fontitem = new menuitem("字体");
 //使用“ctrl+x”快捷键
 menuitem exitiitem = new menuitem("退出", new menushortcut(keyevent.vk_x));
 
 
 
 
 
 public void init() {
 //带滚动条的文本域
 ta.setlinewrap(isautowrap);
 jsp.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_never);
 jsp.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_as_needed);
 
 
 file.add(newitem);
 file.add(openitem);
 file.add(saveitem);
 file.add(saveasitem);
 
 edit.add(cutitem);
 edit.add(copyitem);
 edit.add(pasteitem);
 
 format.add(autowrap);
 format.add(fontitem);
 
 mb.add(file);
 mb.add(edit);
 mb.add(format);
 
 f.setmenubar(mb);
 f.addwindowlistener(new windowadapter() {
 public void windowclosing(windowevent we) {
 if(!(ta.gettext().equals(""))) {
  object[] options = {"保存","不保存","取消"};
  int a = joptionpane.showoptiondialog(null,"是否将更改保存到h:/programing/无标题.txt","记事本", joptionpane.yes_no_cancel_option,joptionpane.question_message, null, options, options[0]);
  if(a == joptionpane.ok_option ) {
  if(save()) {
  system.exit(0);
  }
  }else if(a == joptionpane.no_option) {
  system.exit(0);
  }
 }else {
  system.exit(0);
 }
 }
 });
 
 newitem.addactionlistener(new newlistener());
 openitem.addactionlistener(new openlistener());
 saveitem.addactionlistener(new savelistener());
 saveasitem.addactionlistener(new saveaslistener());
 cutitem.addactionlistener(new cutaslistener());
 copyitem.addactionlistener(new copyaslistener());
 pasteitem.addactionlistener(new pasteaslistener());
 autowrap.additemlistener(new autowrapaslistener());
 fontitem.addactionlistener(new fontlistener());
 
 ta.setfont(font);
 //使文本框处在屏幕中间
 int width = toolkit.getdefaulttoolkit().getscreensize().width;
 int height = toolkit.getdefaulttoolkit().getscreensize().height;
 f.add(jsp);
 f.pack();
 f.setlocation(width/2-f.getwidth()/2, height/2-f.getheight()/2);
 f.setvisible(true);
 //避免关闭对话框直接关闭文本文件
 f.setdefaultcloseoperation(jframe.do_nothing_on_close);
 }
 public static void main(string[] args) {
 new crazy_15_4().init();
 }
 
 //是否保存
 public void saveornot() {
 if(!(ta.gettext().equals(""))) {
 object[] options = {"保存","不保存","取消"};
 int a = joptionpane.showoptiondialog(null,"是否将更改保存到h:/programing/无标题.txt","记事本", joptionpane.yes_no_cancel_option,joptionpane.question_message, null, options, options[0]);
 if(a == joptionpane.ok_option ) {
 save();
 }
 }
 }
 //保存文本文件
 public boolean save() {
 boolean issaved = false;
 jfilechooser jf = new jfilechooser();
 filenameextensionfilter filter = new filenameextensionfilter("txt","txt");
 jf.setacceptallfilefilterused(false);
 jf.setfilefilter(filter);
 int value = jf.showsavedialog(null);
 if(value == jfilechooser.approve_option) {
 file file = jf.getselectedfile();
 string str = ta.gettext();
 byte[] buf = str.getbytes();
 try {
 outputstream os = new fileoutputstream(file);
 os.write(buf);
 os.flush();
 os.close();
 issaved = true;
 } catch (ioexception e) {
 e.printstacktrace();
 }
 }
 return issaved;
 }
 //新建监听器
 class newlistener implements actionlistener{
 public void actionperformed(actionevent e) {
 if(!(ta.gettext().equals(""))) {
 object[] options = {"保存","不保存","取消"};
 int a = joptionpane.showoptiondialog(null,"是否将更改保存到h:/programing/无标题.txt","记事本", joptionpane.yes_no_cancel_option,joptionpane.question_message, null, options, options[0]);
 if(a == joptionpane.ok_option ) {
  if(save()) {
  ta.settext("");
  }
 }else if(a == joptionpane.no_option) {
  ta.settext("");
 }
 }
 
 }
 }
 
 //打开监听器,从硬盘目录打开文本文件
 class openlistener implements actionlistener{
 public void actionperformed(actionevent e) {
 jfilechooser jf = new jfilechooser();
 filenameextensionfilter filter = new filenameextensionfilter("txt","txt");
 jf.setacceptallfilefilterused(false);
 jf.setfilefilter(filter);
 int value = jf.showopendialog(null);
 if(value == jfilechooser.approve_option) {
 file file = jf.getselectedfile();
 try {
  string encoding = "gbk";//考虑编码格式
  inputstreamreader isr = new inputstreamreader(new fileinputstream(file),encoding);
  bufferedreader br = new bufferedreader(isr);
  string str = null;
  while((str = br.readline()) != null) {
  ta.append(str);
  }
 } catch (filenotfoundexception e1) {
  // todo auto-generated catch block
  e1.printstacktrace();
 } catch (ioexception e1) {
  // todo auto-generated catch block
  e1.printstacktrace();
 }
 }
 }
 }
 
 //保存监听器,以电脑时间命名
 class savelistener implements actionlistener{
 public void actionperformed(actionevent e) {
 string name = null;
 simpledateformat df = new simpledateformat("yyyy-mm-dd hh-mm-ss");
 name = df.format(system.currenttimemillis());
 file file = new file("h:/programing/workspace/"+name+".txt");
 string str = ta.gettext();
 byte[] buf = str.getbytes();
 if(!str.equals("")) {
 try {
  outputstream os = new fileoutputstream(file);
  os.write(buf);
  os.flush();
  os.close();
 } catch (ioexception e2) {
  e2.printstacktrace();
 }
 }
 }
 }
 
 //另存为监听器
 class saveaslistener implements actionlistener{
 public void actionperformed(actionevent e) {
 save();
 }
 }
 
 //剪切监听器
 class cutaslistener implements actionlistener{
 public void actionperformed(actionevent e) {
 shearplate = ta.getselectedtext();
 int startposi = ta.getselectionstart();
 int endposi = ta.getselectionend();
 stringbuffer sb = new stringbuffer(ta.gettext());
 sb.delete(startposi, endposi);
 ta.settext(sb.tostring());
 }
 }
 
 //复制监听器
 class copyaslistener implements actionlistener{
 public void actionperformed(actionevent e) {
 shearplate = ta.getselectedtext();
 }
 }
 
 //粘贴监听器
 class pasteaslistener implements actionlistener{
 public void actionperformed(actionevent e) {
 int position = ta.getcaretposition();
 stringbuilder sb = new stringbuilder(ta.gettext());
 sb.insert(position, shearplate);
 ta.settext(sb.tostring());
 ta.setcaretposition(position+shearplate.length());
 }
 }
 
 //自动换行监听器
 class autowrapaslistener implements itemlistener{
 
 @override
 public void itemstatechanged(itemevent e) {
 if(autowrap.getstate()) {
  isautowrap = true;
  ta.setlinewrap(isautowrap);
  jsp.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_never);
  jsp.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_as_needed);
 }else {
  isautowrap = false;
  ta.setlinewrap(isautowrap);
  jsp.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_as_needed);
  jsp.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_never);
 }
 }
 }
 
 //字体监听器
 class fontlistener implements actionlistener{
 jframe jf = new jframe("字体");
 jpanel jp = new jpanel();
 buttonpanel fontbutton = new buttonpanel("字体",new string[] {"宋体","黑体","楷体","吴祎晗1.0"});
 buttonpanel shapebutton = new buttonpanel("字形",new string[] {"常规","粗体","斜体","粗体&斜体"});
 buttonpanel sizebutton = new buttonpanel("字号",new string[] {"12","16","20","24","26"});
 public void actionperformed(actionevent e) {
 jp.setborder(new titledborder(new etchedborder(),"字体选择",titledborder.center,titledborder.top));
 jp.add(fontbutton);
 jp.add(shapebutton);
 jp.add(sizebutton);
 jf.add(jp);
 jf.pack();
 jf.setlocationrelativeto(f);
 jf.setvisible(true);
 jf.addwindowlistener(new windowadapter() {
  public void windowclosing(windowevent e) {
  fontstr = fontbutton.getselec();
  sizestr = sizebutton.getselec();
  font = new font(fontstr, shapeval(shapebutton.getselec()), integer.parseint(sizestr));
  ta.setfont(font);
  }
 });
 }
 
 public int shapeval(string str) {
 int a = 0;
 switch (str) {
 case "常规":
  a = 0;
  break;
 case "粗体":
  a = 1;
  break;
 case "斜体":
  a = 2;
  break;
 case "粗体&斜体":
  a = 3;
  break;
 }
 return a;
 }
 }
 
}
 
//定义一个jpanel类扩展类,该类的对象包含多个纵向排列的
//jradiobutton控件,且panel扩展类可以指定一个字符串作为titledborder
class buttonpanel extends jpanel{
 private buttongroup group;
 public buttonpanel(string title, string[] options) {
 setborder(borderfactory.createtitledborder(borderfactory.createetchedborder(),title));
 setlayout(new boxlayout(this, boxlayout.y_axis));
 group = new buttongroup();
 for(int i=0; options!=null&&i<options.length; i++) {
 jradiobutton b = new jradiobutton(options[i]);
 b.setactioncommand(options[i]);
 this.add(b);
 group.add(b);
 if(title.equals("字号")) {
 b.setselected(i == 2);
 }else {
 b.setselected(i == 0);
 }
 }
 }
 public string getselec() {
 return group.getselection().getactioncommand();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。