import java.awt.filedialog;
import java.awt.flowlayout;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class not implements actionlistener, mouselistener {
private arraylist al = new arraylist();
private int set = 0;
private jframe jf = new jframe(notepad);
private jtextarea jta = new jtextarea(20,40);
jscrollpane jsp = new jscrollpane(jta);
private jtextfield jtf = new jtextfield(10);
private jtextfield jtft = new jtextfield(10);
private jpopupmenu pp = new jpopupmenu();
private string target = null;
private jdialog jd = new jdialog(jf, 警告);
public void actionperformed(actionevent e) {
string comm = e.getactioncommand();
if (comm.equals(新建...)) {
if(!jta.gettext().equals()){
jd.setlocation(300, 300);
jd.setvisible(true);
}else{
jta.settext(1234548882);
}
}
if(comm.equals(否)){
jta.settext();
jd.dispose();
}
if(comm.equals(取消)){
jd.dispose();
}
if (comm.equals(保存) || comm.equals(另存...)||comm.equals(是)) {
if(comm.equals(是)) jd.dispose();
filedialog fd = new filedialog(jf, 打开..., filedialog.save);
fd.setfilenamefilter(new filenamefilter() {
public boolean accept(file dir, string name) {
return name.endswith(.txt) || name.endswith(.java);
}
});
if (target == null || 另存....equals(comm)) {
fd.setvisible(true);
}
if (fd.getfile() != null) {
target = fd.getfile();
try {
fileoutputstream fos = new fileoutputstream(fd
.getdirectory()
+ target);
byte[] b = jta.gettext().getbytes();
for (byte by : b) {
fos.write(by);
}
fos.close();
} catch (ioexception e1) {
e1.printstacktrace();
}
}
}
if (comm.equals(打开...)) {
al.add(jta.gettext());
filedialog fd = new filedialog(jf, 打开..., filedialog.load);
fd.setfilenamefilter(new filenamefilter() {
public boolean accept(file dir, string name) {
return name.endswith(.txt) || name.endswith(.java);
}
});
fd.setvisible(true);
// 以下是打开文件
target = fd.getfile();
string str = fd.getdirectory() + target;
if (fd.getfile() != null) {
try {
string res = ;
fileinputstream fis = new fileinputstream(str);
byte[] b = new byte[1024];
int count;
while ((count = fis.read(b)) != -1) {
res = res + new string(b, 0, count);
}
fis.close();
jta.settext(res);
} catch (ioexception e1) {
e1.printstacktrace();
}
}
}
if (comm.equals(退出)) {
system.exit(0);
}
if (comm.equals(撤销)) {
if(!al.isempty()){
jta.settext(al.get(al.size()-1));
al.remove(al.size()-1);
}
}
if (comm.equals(剪切)) {
al.add(jta.gettext());
jta.cut();
}
if (comm.equals(复制)) {
al.add(jta.gettext());
jta.copy();
}
if (comm.equals(粘贴)) {
al.add(jta.gettext());
jta.paste();
}
if (comm.equals(删除)) {
al.add(jta.gettext());
jta.cut();
}
if (查找....equals(comm) || 替换....equals(comm)) {
jdialog jdct = new jdialog(jf,comm);
string label = comm.substring(0, 2);
jbutton jb = new jbutton(label);
jb.addactionlistener(this);
jdct.setlayout(new flowlayout());
jdct.add(jtf);
if (替换....equals(comm)) {
jlabel jlt = new jlabel(>);
jdct.add(jlt);
jdct.add(jtft);
}
jdct.add(jb);
if (替换....equals(comm)) {
jbutton jball = new jbutton(替换全部);
jball.addactionlistener(this);
jdct.add(jball);
}
jdct.pack();
jdct.setresizable(false);
jdct.setlocation(400, 240);
jdct.setvisible(true);
}
if (comm.equals(全选)) {
jta.selectall();
}
if (comm.equals(关于...)) {
jdialog jdgy = new jdialog(jf, 关于我...);
jdgy.setlayout(new flowlayout());
jlabel jl = new jlabel(制作者: xxx );
jlabel j2 = new jlabel(email:xxxx@.com );
jdgy.add(jl);
jdgy.add(j2);
jdgy.setsize(180, 100);
jdgy.setresizable(false);
jdgy.setlocation(400, 240);
jdgy.setvisible(true);
}
//查找实现
if (comm.equals(查找)) {
string str = jtf.gettext();
string strall = jta.gettext();
int n = strall.indexof(str, set);
if (n != -1) {
set = n + 1;
jta.setselectionstart(n);
jta.setselectionend(n + str.length());
}
}
//替换实现
if (comm.equals(替换) || comm.equals(替换全部)) {
al.add(jta.gettext());
string str = jtf.gettext();
string strall = jta.gettext();
if (comm.equals(替换全部)) {
int n1 = 0;
while (true) {
set = strall.indexof(str, n1);
if (set == -1)
break;
n1 = set + 1;
jta.setselectionstart(set);
jta.setselectionend(set + str.length());
jta.replaceselection(jtft.gettext());
}
set = 0;
}
int n = strall.indexof(str, set);
if (n == -1) {
set = 0;
} else {
set = n + 1;
jta.setselectionstart(n);
jta.setselectionend(n + str.length());
jta.replaceselection(jtft.gettext());
}
}
}
public notepad() {//构造器
jta.setlinewrap(true);
jta.addmouselistener(this);
jmenubar jmb = new jmenubar();
jmenu jm1 = new jmenu(文件);
string[] label1 = { 新建..., 打开..., 保存, 另存..., 退出 };
jmenuitem[] jmi1 = new jmenuitem[label1.length];
for (int i = 0; i < jmi1.length; i++) {
jmi1[i] = new jmenuitem(label1[i]);
jm1.add(jmi1[i]);
jmi1[i].addactionlistener(this);
}
jm1.insertseparator(1);
jm1.insertseparator(3);
jm1.insertseparator(6);
jmenu jm2 = new jmenu(编辑);
string[] label2 = { 撤销, 剪切, 复制, 粘贴, 删除, 查找..., 替换...,
全选 };
jmenuitem[] jmi2 = new jmenuitem[label2.length];
for (int i = 0; i < jmi2.length; i++) {
jmi2[i] = new jmenuitem(label2[i]);
jm2.add(jmi2[i]);
jmi2[i].addactionlistener(this);
}
jm2.insertseparator(1);
jm2.insertseparator(5);
jm2.insertseparator(7);
jm2.insertseparator(10);
jmenu jm3 = new jmenu(帮助);
jmenuitem jmi3 = new jmenuitem(关于...);
jm3.add(jmi3);
jmi3.addactionlistener(this);
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm3);
jf.setjmenubar(jmb);
jf.addwindowlistener(new windowadapter() {
public void windowclosing(windowevent e) {
jf.removeall();
jf.dispose();
}
});
//鼠标右健弹出菜单
jmenuitem jmi11, jmi22, jmi33, jmi44;
pp.add(jmi11 = new jmenuitem(剪切));
pp.add(jmi22 = new jmenuitem(复制));
pp.add(jmi33 = new jmenuitem(粘贴));
pp.addseparator();
pp.add(jmi44 = new jmenuitem(全选));
jmi11.addactionlistener(this);
jmi22.addactionlistener(this);
jmi33.addactionlistener(this);
jmi44.addactionlistener(this);
jf.add(pp);
//
//新建菜单
jd.setlayout(new flowlayout());
jbutton jb1 = new jbutton(是);
jbutton jb2 = new jbutton(否);
jbutton jb3 = new jbutton(取消);
jd.add(new jlabel(您当前编辑未保存,是否保存?));
jb1.addactionlistener(this);
jb2.addactionlistener(this);
jb3.addactionlistener(this);
jd.add(jb1);
jd.add(jb2);
jd.add(jb3);
jd.pack();
jf.add(jsp);
jf.pack();
jf.setlocation(300, 200);
jf.setvisible(true);
jf.setdefaultcloseoperation(jframe.exit_on_close);
}
public static void main(string[] args) {
new notepad();
}
public void mouseclicked(mouseevent e) {
if (e.getbutton() == 3) {
pp.setvisible(true);
pp.show(e.getcomponent(), e.getx(), e.gety());
}
}
public void mouseentered(mouseevent e) {
}
public void mouseexited(mouseevent e) {
}
public void mousepressed(mouseevent e) {
}
public void mousereleased(mouseevent e) {
}
}