📄 edit.java
字号:
/**编辑器test Edition
*EditFace实现了界面制作;
*EditFace$FileOpen完成了打开文件功能;
*EditFace$FileSave完成了保存文件功能;
*Edit$FindAndReplace查找与替换窗口;
*Edit$AutoSave自动备分文件线程;
*Edit$SaveFileDialog保存文件对话框;
*SetKey添加关键字;
*SetColor设置颜色;
*About程序基本介绍;
*Edit组合各个窗口,并实现功能。
*by KOF 10-27-2004
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.colorchooser.*;
class Edit extends EditFace implements ActionListener,ClipboardOwner,MouseListener,TextListener
{
public int flag=0;
//判断文件是否被修改
SetKey setkey=new SetKey();
//添加关键字类
static SetColor setcolor=new SetColor();
//设置颜色类
Clipboard myclipboard=f.getToolkit().getSystemClipboard();
FindAndReplace fr=new FindAndReplace();
//查找与替换内部类
String information="您希望保存当前文件吗?";
SaveFileDialog sfd=new SaveFileDialog(f,"Save File--Test Edition",true,information);
//弹出保存文件对话框
public Edit()
{
super();
ta.addTextListener(this);
//添加自动保存文件线程---每两分钟保存一次
AutoSave as=new AutoSave();
as.start();
}
public static void main(String[] args)
{
new Edit();
setcolor.set(setcolor);
}
public void lostOwnership(Clipboard db,Transferable contents)
{
}
public void textValueChanged(TextEvent e)
{
flag=1;//文件内容以改变~
}
//如果内容已变,弹出保存文件对话框,保持文件
public void saveFile()
{
if(flag==1)
{
sfd.setVisible(true);//弹出保存文件对话框
if(sfd.isSave==1&&!tfpath.getText().equals(""))
{
FileSave fs=new FileSave();
fs.myFileSave(tfpath.getText());
}
else if((sfd.isSave==1&&tfpath.getText().equals(""))||sfd.isSave==2)
{
FileSave fs=new FileSave();
fs.myFileSave();
}
}
}
public void windowClosing(WindowEvent e)
{
saveFile();//询问是否保存当前文件
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{
//Open File
if(e.getActionCommand()=="Open")
{
saveFile();//询问是否保存当前文件
FileOpen fo=new FileOpen();
fo.myFileOpen();//打开文件
ta.select(0,0);
flag=0;
}
//Save File
if(e.getActionCommand()=="Save")
{
if(tfpath.getText().equals(""))
{
FileSave fs=new FileSave();//保存文件
fs.myFileSave();
}
else
{
FileSave fs=new FileSave();//保存文件
fs.myFileSave(tfpath.getText());
}
flag=0;
}
//Save As a File
if(e.getActionCommand()=="Save As")
{
FileSave fs=new FileSave();
fs.myFileSave(); //保存文件
flag=0;
}
if(e.getActionCommand()=="Exit")
{
saveFile();//询问是否保存当前文件
System.exit(0);
}
//set color
if(e.getActionCommand()=="SetColor")
{
setcolor.setVisible(true);//设置文本区颜色
ta.setForeground(setcolor.newcolor);
}
//clear all content
if(e.getSource()==bclearall)
{
saveFile();//询问是否保存当前文件
tfpath.setText("");//清除
ta.setText("");
}
//set key
if(e.getSource()==bsetkey)
{
setkey.setVisible(true);//设置关键字
}
//set color
if(e.getSource()==bcolor)
{
setcolor.setVisible(true);//设置文本区颜色
ta.setForeground(setcolor.newcolor);
}
if(e.getActionCommand()=="Copy")//复制到剪贴板
{
if(!ta.getSelectedText().equals(""))
{
try
{
StringSelection contents=new StringSelection(ta.getSelectedText());
myclipboard.setContents(contents,this);
ta.select(ta.getSelectionEnd(),ta.getSelectionEnd());
}
catch(Exception ee)
{
System.out.println(ee);
}
}
}
if(e.getActionCommand()=="Cut")//复制到剪贴板并清楚内容
{
if(!ta.getSelectedText().equals(""))
try
{
int startpos=ta.getSelectionStart();//start position
int endpos=ta.getSelectionEnd();
StringSelection contents=new StringSelection(ta.getSelectedText());
myclipboard.setContents(contents,this);
ta.setText(ta.getText().substring(0,startpos)+ta.getText().substring(endpos,ta.getText().length()));
ta.select(startpos,startpos);
}
catch(Exception ee)
{
System.out.println(ee);
}
}
if(e.getActionCommand()=="Paste")//粘贴
{
try
{
Transferable contents=myclipboard.getContents(this);
String dstdata=(String)contents.getTransferData(DataFlavor.stringFlavor);
int startpos=ta.getSelectionStart();
int endpos=ta.getSelectionEnd();
ta.setText(ta.getText().substring(0,startpos)+dstdata+ta.getText().substring(endpos,ta.getText().length()));
ta.select(startpos+dstdata.length(),startpos+dstdata.length());
}
catch(Exception ee)
{
System.err.println(ee);
}
}
if(e.getActionCommand()=="Select All")//全选功能的实现
{
ta.select(0,ta.getText().length());
}
if(e.getActionCommand()=="Find")//查找功能的实现
{
fr.setVisible(true);
fr.setEnabled(false);
}
if(e.getActionCommand()=="Replace")//替换功能的实现
{
fr.setVisible(true);
fr.setEnabled(true);
}
if(e.getActionCommand()=="About Edit"||e.getActionCommand()=="About Me")
{
About about=new About();
about.setVisible(true);
}
}
//change the color
public void mouseClicked(MouseEvent mec) //单击文本区才会改变颜色
{ ta.setForeground(setcolor.newcolor); }
public void mousePressed(MouseEvent mep)
{
if(mep.getModifiers()==mep.BUTTON3_MASK)//弹出右键菜单
pm.show(ta,mep.getX(),mep.getY());
}
public void mouseReleased(MouseEvent mer) { }
public void mouseEntered(MouseEvent mee) { }
public void mouseExited(MouseEvent mex) { }
public void mouseDragged(MouseEvent med) { }
//查找与替换窗口
class FindAndReplace extends WindowAdapter implements ActionListener
{
private Frame frame;
private TextField tffind,tfreplace;
private Label labfind,labreplace;
private Button bfind,bfindnext,breplace,breplaceall;
private Panel p1,p2,p3,p4;
private Label labinfo;
private String info="";
private int pos;
public FindAndReplace()
{
frame=new Frame("Find And Replace--Test Edition");
frame.setLayout(new GridLayout(3,1));
frame.setSize(300,200);
frame.setLocation(80,20);
frame.setBackground(Color.lightGray);
p1=new Panel();
p1.setLayout(new GridLayout(2,1));
labfind=new Label("Please input what you want to search:");
tffind=new TextField(25);
p1.add(labfind);
p1.add(tffind);
p2=new Panel();
p2.setLayout(new GridLayout(2,1));
labreplace=new Label("Please input what you want to replace:");
tfreplace=new TextField(25);
p2.add(labreplace);
p2.add(tfreplace);
p3=new Panel();
p3.setLayout(new FlowLayout(FlowLayout.LEFT));
bfind=new Button(" Find ");
bfindnext=new Button("Find Next");
breplace=new Button(" Replace ");
breplaceall=new Button("Replace All");
bfind.addActionListener(this);
bfindnext.addActionListener(this);
breplace.addActionListener(this);
breplaceall.addActionListener(this);
p3.add(bfind);
p3.add(bfindnext);
p3.add(breplace);
p3.add(breplaceall);
p4=new Panel();
p4.setLayout(new GridLayout(2,1));
labinfo=new Label();
p4.add(p3);
p4.add(labinfo);
frame.addWindowListener(this);
frame.add(p1);
frame.add(p2);
frame.add(p4);
}
public void setVisible(boolean b)
{
frame.setVisible(b);
pos=0;
ta.select(pos,pos);
}
public void setEnabled(boolean b)
{
tfreplace.setEnabled(b);
breplace.setEnabled(b);
breplaceall.setEnabled(b);
}
public int find(String s,String sub)//查找
{
return find(s,sub,0);
}
public int find(String s,String sub,int startpos)//查找
{
String ss="";
int len=s.length();
int sublen=sub.length();
try
{
for(int j=startpos;j<=len-sublen;j++)
{
if(s.substring(j,j+sublen).equals(sub))
return j;
}
}
catch(Exception ee)
{
return -1;
}
return -1;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bfind)
{
String s=tffind.getText();
if(!s.equals(""))
{
pos=find(ta.getText(),s);//查找
if(pos>=0)
{
ta.select(pos,pos+s.length());
info="字符串 "+s+" 在第"+pos+"个字符处";
pos=pos+s.length();
}
else
{
info="字符串 "+s+" 没有找到!";
}
}
}
if(e.getSource()==bfindnext)//查找下一个
{
String s=tffind.getText();
pos=ta.getSelectionEnd();
if(!s.equals(""))
{
pos=find(ta.getText(),s,pos);
if(pos>=0)
{
ta.select(pos,pos+s.length());
info="字符串 "+s+" 在第"+pos+"个字符处!";
pos=pos+s.length();
}
else
{
pos=0;
info="字符串 "+s+" 没有找到!";
}
}
}
if(e.getSource()==breplace)//替换
{
String sfind=tffind.getText();
String sreplace=tfreplace.getText();
String ss=ta.getText();
int a,b;
pos=ta.getSelectionEnd();
if((pos=find(ss,sfind,pos))!=-1)
{
a=pos;
pos=pos+sfind.length();
b=pos;
ss=(ss.substring(0,a)+sreplace+ss.substring(b,ss.length()));
pos=pos+(sreplace.length()-sfind.length());
ta.setText(ss);
ta.select(pos-sreplace.length(),pos);
info="字符串 "+sfind+"在第"+(pos-sreplace.length())+"个字符处"+" 已被 "+sreplace+" 替换!";
}
else
{
//cannot find the result
info="字符串 "+sfind+" 没有找到!";
}
}
if(e.getSource()==breplaceall)//替换all
{
String sfind=tffind.getText();
String sreplace=tfreplace.getText();
String ss=ta.getText();
int a,b;
int isfindflag=0;
pos=0;
while((pos=find(ss,sfind,pos))!=-1)
{
isfindflag++;
a=pos;
pos=pos+sfind.length();
b=pos;
ss=(ss.substring(0,a)+sreplace+ss.substring(b,ss.length()));
pos=pos+(sreplace.length()-sfind.length());
}
if(isfindflag>0)
{
ta.setText(ss);
pos=0;
ta.select(pos,pos);
info="共"+isfindflag+"处的字符串 "+sfind+" 已被 "+sreplace+" 替换!";
}
else
{
info="字符串 "+sfind+" 没有找到!";
}
}
labinfo.setText(info);
}
public void windowClosing(WindowEvent e)
{
frame.setVisible(false);
}
}
//保存文件的对话框实现
class SaveFileDialog extends Dialog implements ActionListener
{
private Dialog d;
private Label info;
private Panel p;
private Button save,saveas,nosave;
int isSave=0;
public SaveFileDialog(Frame owner,String title,boolean modal,String s)
{
super(owner,title,modal);
setSize(200,100);
setLocation(200,100);
setBackground(Color.LIGHT_GRAY);
info=new Label(s);
add(info,"Center");
save=new Button("Save");
saveas=new Button("Save As");
nosave=new Button("No Save");
save.addActionListener(this);
saveas.addActionListener(this);
nosave.addActionListener(this);
p=new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.add(save);
p.add(saveas);
p.add(nosave);
add(p,"South");
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==save)
{
isSave=1;
setVisible(false);
}
if(e.getSource()==saveas)
{
isSave=2;
setVisible(false);
}
if(e.getSource()==nosave)
{
isSave=0;
setVisible(false);
}
}
}
//自动保存文件的线程实现
class AutoSave extends Thread
{
FileSave fs=new FileSave();
public void run()
{
while(true)
{
try
{
if(!tfpath.getText().equals(""))
{
fs.myFileSave(tfpath.getText()+".bak");
}
sleep(2000);
}catch(Exception e)
{
System.out.println(e.getStackTrace());
}
}
}
}
}
//实现了界面制作和打开,保存文件
class EditFace extends WindowAdapter implements ActionListener,ItemListener,MouseListener
{
Frame f;
MenuBar mb;
PopupMenu pm;
Menu mfile,medit,mabout;
TextField tfpath;
Choice choicesize;
Panel p;
Label labsize,labblank;
Checkbox cbbold,cbitalic;
Button bcolor,bsetkey,bclearall;
TextArea ta=new TextArea("",10,10,1);
FileDialog fd;
File file1;
Font font1;
public EditFace()
{
//set Frame
f=new Frame("KOF_EDIT--Test Edition");
f.setSize(750,570);
f.setLocation(10,10);
f.setBackground(Color.lightGray);
f.setLayout(new BorderLayout());
f.addWindowListener(this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -