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

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java文本编辑器实现方法详解

Java文本编辑器实现方法详解

2021-07-22 15:40水中鱼之1999 Java教程

这篇文章主要介绍了Java文本编辑器实现方法,结合实例形式详细分析了java文本编辑器结构、原理、布局、实现步骤与相关操作技巧,需要的朋友可以参考下

本文实例讲述了java文本编辑器实现方法。分享给大家供大家参考,具体如下:

结构分析:

  • 界面布局 : editframe
  • main方法所在: edittext
  • 打开功能实现:  filereadthread
  • 保存跟能实现:  save

实际运行效果:

Java文本编辑器实现方法详解

附:完整代码实现

一、 editframe

Java文本编辑器实现方法详解

包括一个菜单menu

底部:日期时间

代码附上:

?
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
public class editframe extends jframe {
 // todo 自动生成的构造函数存根
 boolean saveflag = false;
 file savefileroot = null;
 jframe jframe;
 jpanel jpanelsouth;
 jmenubar jmenubar1;
 jmenu jmenu1;
 jmenuitem jmenuitem1;
 jmenuitem jmenuitem2;
 jmenuitem jmenuitem3;
 jmenuitem jmenuitem4;
 jseparator jseparator1;
 jtextarea jtextarea;
 jscrollpane scrollpane;// 滚动条
 public editframe() {
 // todo 自动生成的构造函数存根
 jframe = new jframe("水中鱼之1999-文本编辑器");
 jpanelsouth = new jpanel();
 jmenubar1 = new jmenubar();
 jmenu1 = new jmenu("文件");
 jmenuitem1 = new jmenuitem("打开");
 jmenuitem2 = new jmenuitem("保存");
 jmenuitem3 = new jmenuitem("另存为");
 jmenuitem4 = new jmenuitem("退出");
 jseparator1 = new jseparator();
 jtextarea = new jtextarea();
 scrollpane = new jscrollpane(jtextarea);
 jframe.setsize(800, 500);
 jframe.setlocationrelativeto(null);
 jframe.setvisible(false);
 
 setlayout();
 setsouthpanel();
 // set relationship for your component
 setrelationship();
 // 设置 scrollpane for textarea
 setscscrollpane();
 iniclick();
 }
 private void setrelationship() {
 jframe.add(borderlayout.center, scrollpane);
 jframe.add(borderlayout.south, jpanelsouth);
 jmenu1.add(jmenuitem1);
 jmenu1.add(jmenuitem2);
 jmenu1.add(jmenuitem3);
 jmenu1.add(jseparator1);
 jmenu1.add(jmenuitem4);
 jmenubar1.add(jmenu1);
 jframe.setjmenubar(jmenubar1);
 }
 private void setlayout() {
 gridlayout gridlayout = new gridlayout(1, 2);
 jpanelsouth.setlayout(gridlayout);
 }
 private void setscscrollpane() {
// jtextarea.setlinewrap(true);// 设置满一行自动换行
 scrollpane.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_as_needed);
 scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_as_needed);
 }
 private void setsouthpanel() {
 // add time for southpanel
 jlabel jlabeldate = new jlabel("date");
 jlabel jlabeltime = new jlabel("time");
 timer timeaction = new timer(1000, new actionlistener() {
  public void actionperformed(actionevent e) {
  long timemillis = system.currenttimemillis();
  // 转换日期显示格式
  simpledateformat date = new simpledateformat("yyyy 年 mm 月 dd 日 ");
  jlabeldate.settext("  当前日期: " + date.format(new date(timemillis)));
  simpledateformat time = new simpledateformat("hh:mm:ss ");
  jlabeltime.settext("  当前时间: " + time.format(new date(timemillis)));
  }
 });
 jpanelsouth.add(jlabeldate);
 jpanelsouth.add(jlabeltime);
 timeaction.start();
 }
 private void iniclick() {
 jframe.addwindowlistener(new windowlistener() {
  @override
  public void windowopened(windowevent e) {
  // todo auto-generated method stub
  }
  @override
  public void windowiconified(windowevent e) {
  // todo auto-generated method stub
  }
  @override
  public void windowdeiconified(windowevent e) {
  // todo auto-generated method stub
  }
  @override
  public void windowdeactivated(windowevent e) {
  // todo auto-generated method stub
  }
  @override
  public void windowclosing(windowevent e) {
  // todo auto-generated method stub
  int x = joptionpane.showconfirmdialog(null, "确认退出么?", "友情提示", joptionpane.ok_cancel_option,
   joptionpane.warning_message);
  if (x == 0) {
   system.exit(0);
  }
  }
  @override
  public void windowclosed(windowevent e) {
  // todo auto-generated method stub
  }
  @override
  public void windowactivated(windowevent e) {
  // todo auto-generated method stub
  }
 });
 jmenuitem4.addactionlistener(new actionlistener() {
  @override
  public void actionperformed(actionevent e) {
  // todo auto-generated method stub
  int x = joptionpane.showconfirmdialog(null, "确认退出么?", "友情提示", joptionpane.ok_cancel_option,
   joptionpane.warning_message);
  if (x == 0) {
   system.exit(0);
  }
  }
 });
 jmenuitem1.addactionlistener(new actionlistener() {
  @override
  public void actionperformed(actionevent e) {
  // todo auto-generated method stub
  filereadthread filereadthread = new filereadthread(editframe.this);// 开启文件读取线程
  filereadthread.start();
  system.out.println(savefileroot);
  saveflag = true;
  jtextarea.settext("");
  }
 });
 jmenuitem3.addactionlistener(new actionlistener() {
  @override
  public void actionperformed(actionevent e) {
  // todo auto-generated method stub
  save save = new save(editframe.this);
  save.start();
  saveflag = true;
  }
 });
 jmenuitem2.addactionlistener(new actionlistener() {
  @override
  public void actionperformed(actionevent e) {
  // todo auto-generated method stub
  if (!saveflag) {
   save save = new save(editframe.this);
   save.start();
   saveflag = true;
  } else {
   new save(editframe.this, savefileroot);
  }
  }
 });
 }
 public jtextarea getjtextarea() {
 return jtextarea;
 }
 public void setjtextarea(jtextarea jtextarea) {
 this.jtextarea = jtextarea;
 }
 public file getsavefileroot() {
 return savefileroot;
 }
 public void setsavefileroot(file savefileroot) {
 this.savefileroot = savefileroot;
 }
 public jframe getjframe() {
 return jframe;
 }
 public void setjframe(jframe jframe) {
 this.jframe = jframe;
 }
}

二、测试类 edittext

分两部分:

1.闪屏 由于加载页面需要时间 原想用 splashscreen 由于打包成jar包才能用所以这里用jframe进行替代

闪屏图片直接粘贴到:

Java文本编辑器实现方法详解

2.new 一个editframe 对象, 闪屏结束后设置为可见

?
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
public class edittext {
 public static void main(string[] args) {
 new thread() {
  @override
  public void run() {
  // todo auto-generated method stub
  editframe editframe = new editframe();
  
  jframe jframe = new jframe();
  jpanel jpanel = new javax.swing.jpanel(){
    protected void paintcomponent(java.awt.graphics g){
      super.paintcomponent(g);
      g.drawimage(new imageicon("experiment_bac.jpg").getimage(),0,0,400,250,null);
    }
  };
  jframe.add(jpanel);
  jframe.setvisible(true);
  jframe.setsize(400, 300);
  jframe.setlocationrelativeto(null);
  
  try {
   sleep(1500);
  } catch (interruptedexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
  jframe.dispose();
  editframe.getjframe().setvisible(true);
  }
 }.start();
 }
}

三、filereadthread

调用swing自带的 jfilechooser

选择文件路径

?
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
class filereadthread extends thread {
 private editframe test;
 public filereadthread(editframe test ) {
 this.test = test;
 }
 @override
 public void run() {
 jfilechooser chooser = new jfilechooser("d:/");
 chooser.setfilefilter(new filefilter() {// 定义文件过滤器,仅显示文件夹和txt文本
  @override
  public string getdescription() {
  return null;
  }
  @override
  public boolean accept(file file) {
  if (file.isdirectory() || file.getname().endswith(".txt"))
   return true;
  return false;
  }
 });
 int option = chooser.showopendialog(test);
 if (option == jfilechooser.approve_option) {
  file selfile = chooser.getselectedfile();
  try {
  bufferedreader reader = new bufferedreader(new inputstreamreader(new fileinputstream(selfile), "gbk"));
  test.setsavefileroot(selfile);
  string line = null;
  while ((line = reader.readline()) != null) {
   test.getjtextarea().append(line + "\n");
   thread.sleep(30);// 线程暂停,以看到读取过程效果
  }
  } catch (exception e1) {
  e1.printstacktrace();
  }
  joptionpane.showmessagedialog(test, "读取完毕");
 }
 }
}

四、save 保存

保存的调用分三种:

  • 如果该文件是打开的 那么保存到打开文件中
  • 如果该文件还未保存 这调用:进行保存
  • 如果该文件已经另存为,则直接保存到另存为得的目录下
?
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
public class save extends thread {
 private editframe area;
 private file savefileroot = null;
 public save(editframe area, file savefileroot) {
 system.out.println(savefileroot + "123");
 string text = area.getjtextarea().gettext();
 string[] lines = text.trim().split("\n");
 try {
  printwriter out = new printwriter(new fileoutputstream(savefileroot), true);
  for (string line : lines)
  out.println(line);
 } catch (filenotfoundexception e) {
  // todo auto-generated catch block
  e.printstacktrace();
 }
 }
 public save(editframe area) {
 this.area = area;
 jfilechooser chooser = new jfilechooser();
 chooser.setcurrentdirectory(new file("."));
 chooser.setfilefilter(new filefilter() {
  @override
  public boolean accept(file f) {
  return f.getname().tolowercase().endswith(".txt");
  }
  @override
  public string getdescription() {
  return "save to";
  }
 });
 int r = chooser.showsavedialog(area);
 if (r != jfilechooser.approve_option)
  return;
 file f = chooser.getselectedfile();
 area.setsavefileroot(f);
 string text = area.getjtextarea().gettext();
 string[] lines = text.trim().split("\n");
 try {
  printwriter out = new printwriter(new fileoutputstream(f), true);
  for (string line : lines)
  out.println(line);
 } catch (filenotfoundexception e) {
  // todo auto-generated catch block
  e.printstacktrace();
 }
 }
 public file getsavefileroot() {
 return savefileroot;
 }
 public void setsavefileroot(file savefileroot) {
 this.savefileroot = savefileroot;
 }
}

希望本文所述对大家java程序设计有所帮助。

原文链接:https://blog.csdn.net/qq_43377749/article/details/85091983

延伸 · 阅读

精彩推荐