📄 texteditor.java
字号:
ignoreCase=true;
}
}
}
//打开文件
private void open()
{
if(okToAbandon())
{
save();
}
jFileChooser.setDialogTitle("打开");
if(jFileChooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
try
{
BufferedInputStream in=new BufferedInputStream(new FileInputStream(jFileChooser.getSelectedFile()));
byte[] b=new byte[in.available()];
in.read(b,0,b.length);
text.setText(new String(b,0,b.length));
in.close();
setTitle("文本编辑器---"+jFileChooser.getSelectedFile().getName());
textContent=text.getText();
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(this,"无法打开文件","打开文件",JOptionPane.INFORMATION_MESSAGE);
}
}
}
//保存文件
private void save()
{
jFileChooser.setDialogTitle("保存");
if(jFileChooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
{
try
{
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(jFileChooser.getSelectedFile()));
byte[] b=(text.getText().getBytes());
out.write(b,0,b.length);
out.close();
textContent=text.getText();
JOptionPane.showMessageDialog(this,"保存文件成功!","保存文件",JOptionPane.INFORMATION_MESSAGE);
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(this,"无法保存文件","保存文件",JOptionPane.INFORMATION_MESSAGE);
}
}
}
//新建文件
private void newDocument()
{
if(okToAbandon())
{
save();
}
text.setText("");
textContent=text.getText();
setTitle("文本编辑器---未命名");
}
//用来查看是否文档改变了
private boolean textIsChange()
{
String current=text.getText();
if(!current.equals(textContent))
{
return true;
}
return false;
}
//用来询问用户,如果文档改变了,该怎么处理
public boolean okToAbandon()
{
boolean i=false;
if(textIsChange())
{
int value = JOptionPane.showConfirmDialog(this, "文件已经改变了,是否保存?",
"文件改变", JOptionPane.YES_NO_OPTION);
switch (value)
{
case JOptionPane.YES_OPTION:i=true;break;
case JOptionPane.NO_OPTION:i=false;break;
}
}
return i;
}
//查找
private void find(String string)
{
int length=text.getText().length();
boolean isNotFind=true;
if(findNext) //向下查找
{
if(index==-1 && string.length()==1)
{
index=1;
}
else if(length<string.length()||(index+string.length()>length))
{
findExit();
}
else if(ignoreCase==true)
{
for(index=index;isNotFind==true;index++)
{
if(index+string.length()>length)
{
findExit();
}
else if(text.getText().substring(index,index+string.length()).equals(string))
{
text.select(index,index+string.length());
isNotFind=false;
isFind=true;
}
}
}
else
{
for(index=index;isNotFind==true;index++)
{
if(index+string.length()>length)
{
findExit();
}
else if(text.getText().substring(index,index+string.length()).equalsIgnoreCase(string))
{
text.select(index,index+string.length());
isNotFind=false;
isFind=true;
}
}
}
}
if(!findNext) //向上查找
{
if(index==length && string.length()==1)
{
index=length-2;
}
else if(length<string.length()|| (index-string.length()+1<0))
{
findExit();
}
if(ignoreCase==true)
{
for(index=index;isNotFind==true;index--)
{
if(index-string.length()+1<0)
{
findExit();
break;
}
else if(text.getText().substring(index-string.length()+1,index+1).equals(string))
{
text.select(index-string.length()+1,index+1);
isNotFind=false;
isFind=true;
}
}
}
else
{
for(index=index;isNotFind==true;index--)
{
if(index-string.length()+1<0)
{
findExit();
break;
}
else if(text.getText().substring(index-string.length()+1,index+1).equalsIgnoreCase(string))
{
text.select(index-string.length()+1,index+1);
isNotFind=false;
isFind=true;
}
}
}
}
}
//结束查询或终止查询
private void findExit()
{
searchJFrame.setVisible(false);
text.setText(text.getText());
index=0;
JOptionPane.showMessageDialog(this,"查找结素","查找与替换",JOptionPane.INFORMATION_MESSAGE);
}
//替换程序,其中要用到查找程序,先查找再决定替换
public void replace()
{
text.replaceRange(replaceText.getText(),index-1,index+findText.getText().length()-1);
isFind=false;
}
public void replaceAll()
{
while(index+findText.getText().length()<=text.getText().length())
{
findNext=true;
find(findText.getText());
replace();
isFind=false;
}
}
//查错功能
public void check()
{
text.setHighlighter(hl);
String string=text.getText();
Pattern p=Pattern.compile("[a-zA-Z]+");
Matcher m = p.matcher(string);
while(m.find())
{
int start=m.start();
int end=m.end();
if(!isFind(string.substring(start,end),0,13791))
{
try
{
hl.addHighlight(start,end,hp);
}
catch(Exception e){}
}
}
}
//看是否在字典中
private boolean isFind(String s,int m,int n)
{
int left=m;
int right=n;
int centre=(left+right)/2;
String string=s.toLowerCase();
if(right-left<=1)
{
if(dic[left].equalsIgnoreCase(string)||dic[right].equalsIgnoreCase(string))
{
return true;
}
return false;
}
else if(dic[centre].equalsIgnoreCase(string)){return true;}
else if(dic[centre].compareTo(string)<0){return isFind(string,centre,right);}
else return isFind(string,left,centre);
}
//给出纠错建议
private void showOpinion()
{
JMenuItem[] jmenu=new JMenuItem[6];
menu.removeAll();
int k=0;
final int start=text.getSelectionStart();
final int end=text.getSelectionEnd();
String s=text.getText().substring(start,end);
int len=s.length();
for(int i=1;i<len-1&&k<6;i++)
{
for(char c='a';c>='a'&&c<='z'&k<6;c++)
{
String str=s.substring(0,len-i)+new String(new char[]{c})+s.substring(len-i,len);
if(isFind(str,0,13791))
{
jmenu[k]=new JMenuItem(str);
k++;
}
}
for(char c='a';c>='a'&&c<='z'&&i<=len/2+1&k<6;c++)
{
String str=s.substring(0,len-i+1)+new String(new char[]{c});
if(isFind(str,0,13791))
{
jmenu[k]=new JMenuItem(str);
k++;
}
}
}
for(char c='a';c>='a'&&c<='z'&&k<6;c++)
{
String str=s.substring(0,len-1)+new String(new char[]{c});
if(isFind(str,0,13791))
{
jmenu[k]=new JMenuItem(str);
k++;
}
}
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent e) {
text.replaceRange(((JMenuItem)e.getSource()).getText(),start,end);}
};
if(jmenu[1]==null)
{
jmenu[1]=new JMenuItem("没找到!");
menu.add(jmenu[1]);
}
for(int i=0;i<=k-1;i++)
{
jmenu[i].addActionListener(al);
menu.add(jmenu[i]);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -