编辑文章 - 博客频道 - CSDN.NET

开始时查找时每换一行,查找出来的字符串会往后移位标记一个字符串,原来是得到的text中多了"\n",然后替换后才好了。。。

下面是代码:ps:还没有写完的一个程序。。。

package NoteBook;
import javax.swing.*;


import java.awt.Color;
import java.awt.event.*;
import java.io.*;
import java.util.*;


import javax.swing.text.*;
import javax.swing.filechooser.*;


public class NoteBook {

private JFrame jf=new JFrame("记事本");
private JTextPaneN jtp=new JTextPaneN();
private JMenuBar jmb=new JMenuBar();
private JMenu fileM=new JMenu("文件(M)");
private JMenu editM=new JMenu("编辑(E)");
private JMenuItem openI=new JMenuItem("打开");
private JMenuItem saveI=new JMenuItem("保存");
private JMenuItem otherSaveI=new JMenuItem("另存为");
private JMenuItem seekI=new JMenuItem("查找");
private JMenuItem seekNextI=new JMenuItem("查找下一个");
private JMenuItem replaceI=new JMenuItem("替换");
String name;
private File f;
public void init()
{
addAction();
fileM.add(openI);
fileM.addSeparator();
fileM.add(saveI);
fileM.add(otherSaveI);
editM.add(seekI);
editM.add(seekNextI);
editM.addSeparator();
editM.add(replaceI);
jmb.add(fileM);
jmb.add(editM);
jf.setJMenuBar(jmb);
jf.add(new JScrollPane(jtp));
jf.setSize(800,800);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args)
{
new NoteBook().init();
}
private void addAction()
{
openI.addActionListener(new OpenListener());
saveI.addActionListener(new SaveListener());
otherSaveI.addActionListener(new OtherSaveListener());
seekI.addActionListener(new SeekListener());
}
class SeekListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String word=JOptionPane.showInputDialog("输入要查找的字");
int wordLength=word.length();
String first=word.substring(0, 1);
//System.out.println(first);
String textone=jtp.getText();
String text=textone.replace("\n", "");
int textLength=text.length();
System.out.println("总的文本长度:"+textLength);
System.out.println("搜索文本长度:"+wordLength);
StyledDocument doc=jtp.getStyledDocument();
SimpleAttributeSet style=new SimpleAttributeSet();
for(int i=0;i<textLength;i++)
{
int sameCount=0;
if((text.substring(i,i+1).equals(first)))
{   sameCount=0;
for(int j=0;j<wordLength;j++)
{
if(text.substring(i+j,i+j+1).equals(word.substring(j,j+1)))
{
sameCount++;
}
}
if(sameCount==wordLength)
{
String selectedWord=text.substring(i,i+wordLength);
System.out.println(selectedWord);
StyleConstants.setForeground(style,Color.RED);
doc.setCharacterAttributes(i, wordLength,style,false);
i=i+wordLength;
//System.out.println(sameCount);
}
}

}
}
}
class OtherSaveListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser=new JFileChooser(".");
chooser.showSaveDialog(otherSaveI);
File file=chooser.getCurrentDirectory();

String path=file.getPath()+"\\"+chooser.getSelectedFile().getName();
System.out.println(path);
try{
PrintStream pso=new PrintStream(new FileOutputStream(path));
System.setOut(pso);
Document text=jtp.getDocument();
System.out.println(text.getText(0, text.getLength()));
}
catch(Exception ez){
JOptionPane.showMessageDialog(null,"保存失败");
ez.printStackTrace();}
}
}
class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try(
PrintStream ps=new PrintStream(new FileOutputStream(f)))
{
System.setOut(ps);
Document text=jtp.getDocument();
System.out.println(text.getText(0, text.getLength()));
}
catch(Exception ex){ex.printStackTrace();}
}
}
class OpenListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

String s=jtp.getText();
if(s.length()!=0)
{
jtp.setText("");
}
ExtensionFileFilter fileFilter=new ExtensionFileFilter();
fileFilter.addExtension("txt");
fileFilter.addExtension("java");
fileFilter.addExtension("class");
JFileChooser chooser=new JFileChooser(".");
chooser.setAcceptAllFileFilterUsed(false);
chooser.addChoosableFileFilter(fileFilter);
chooser.showOpenDialog(openI);
f=chooser.getSelectedFile();
name=f.getName();
//System.out.println(name);
try{
FileInputStream fis=new FileInputStream(f);
System.setIn(fis);
Scanner sc=new Scanner(System.in);
sc.useDelimiter("\n");
SimpleAttributeSet attrSet = new SimpleAttributeSet();
while(sc.hasNext())
{
jtp.insert(sc.next(),attrSet);
}
sc.close();
}
catch(Exception ex){ex.printStackTrace();}
}
}
class JTextPaneN extends JTextPane
{
public void insert(String str, AttributeSet attrSet) {
Document doc = jtp.getDocument();
str = "\n" + str;
try {
doc.insertString(doc.getLength(), str, attrSet);
} catch (BadLocationException e) {
System.out.println("BadLocationException: " + e);
}
}}
class ExtensionFileFilter extends javax.swing.filechooser.FileFilter
{
private String description;
private ArrayList<String> extensions = new ArrayList<>();
// 自定义方法,用于添加文件扩展名
public void addExtension(String extension)
{
if (!extension.startsWith("."))
{
extension = "." + extension;
extensions.add(extension.toLowerCase());
}
}
// 用于设置该文件过滤器的描述文本
public void setDescription(String aDescription)
{
description = aDescription;
}
// 继承FileFilter类必须实现的抽象方法,返回该文件过滤器的描述文本
public String getDescription()
{
return description;
}
// 继承FileFilter类必须实现的抽象方法,判断该文件过滤器是否接受该文件
public boolean accept(File f)
{
// 如果该文件是路径,接受该文件
if (f.isDirectory()) return true;
// 将文件名转为小写(全部转为小写后比较,用于忽略文件名大小写)
String name = f.getName().toLowerCase();
// 遍历所有可接受的扩展名,如果扩展名相同,该文件就可接受。
for (String extension : extensions)
{
if (name.endsWith(extension))
{
return true;
}
}
return false;
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值