PHP小丑 发表于 2021-9-18 12:45:31

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

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

[*]界面布局 : editframe
[*]main方法所在: edittext
[*]打开功能实现:filereadthread
[*]保存跟能实现:save
实际运行效果:

附:完整代码实现
一、 editframe

包括一个菜单menu
底部:日期时间
代码附上:


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进行替代
闪屏图片直接粘贴到:

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


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
选择文件路径


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 保存
保存的调用分三种:

[*]如果该文件是打开的 那么保存到打开文件中
[*]如果该文件还未保存 这调用:进行保存
[*]如果该文件已经另存为,则直接保存到另存为得的目录下


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

http://www.zzvips.com/article/178074.html
页: [1]
查看完整版本: Java文本编辑器实现方法详解