java实现简易文本编辑器
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
import javax.swing.filechooser.*;
import javax.swing.filechooser.FileFilter;
import java.awt.datatransfer.*;
public class Text extends JFrame
{
TextArea area;
String name=null;
String board=null;
private Clipboard cb;
public Text()throws Exception
{
setTitle("记事本");
Toolkit kit=Toolkit.getDefaultToolkit();
Image img=kit.getImage("01.jpg");
setIconImage(img);
JMenuBar jmb=new JMenuBar();
add(jmb);
setJMenuBar(jmb);
JMenu file=new JMenu("文件");
JMenu edit=new JMenu("编辑");
JMenuItem rebuild=new JMenuItem("新建");
JMenuItem save=new JMenuItem("保存");
JMenuItem open=new JMenuItem("打开");
JMenuItem anotherSave=new JMenuItem("另存为");
file.add(rebuild);
file.add(save);
file.add(open);
file.add(anotherSave);
rebuild.setAccelerator(KeyStroke.getKeyStroke("ctrl N"));
open.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));
save.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
anotherSave.setAccelerator(KeyStroke.getKeyStroke("ctrl A"));
area=new TextArea();
add(area);
//JScrollPane scroldPane=new JScrollPane(area); //设置滚动轴
//add(scroldPane);
save.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try{
saveText();
}catch(Exception ex)
{
}
}
});
open.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try{
openText();
}catch(Exception ex)
{
}
}
});
rebuild.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try{
area.setText("");
name=null;
}catch(Exception ex)
{}
}
});
anotherSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
anotherSaveText();
}
});
JMenuItem cut=new JMenuItem("剪切");
JMenuItem copy=new JMenuItem("复制");
JMenuItem plaster=new JMenuItem("粘贴");
JMenuItem all=new JMenuItem("全选");
edit.add(cut);
edit.add(copy);
edit.add(plaster);
edit.add(all);
cut.setAccelerator(KeyStroke.getKeyStroke("ctrl X"));
copy.setAccelerator(KeyStroke.getKeyStroke("ctrl C"));
plaster.setAccelerator(KeyStroke.getKeyStroke("ctrl V"));
all.setAccelerator(KeyStroke.getKeyStroke("ctrl A"));
cb=Toolkit.getDefaultToolkit().getSystemClipboard();
cut.addActionListener(new ActionListener() //剪切
{
public void actionPerformed(ActionEvent e)
{
board=area.getSelectedText();
cb.setContents(new StringSelection(board),null);
area.replaceRange("",area.getSelectionStart(),area.getSelectionEnd());
}
});
copy.addActionListener(new ActionListener() //复制
{
public void actionPerformed(ActionEvent
e)
{
board=area.getSelectedText();
cb.setContents(new StringSelection(board),null);
}
});
plaster.addActionListener(new ActionListener() //粘贴
{
public void actionPerformed(ActionEvent e)
{
try{
Transferable content=cb.getContents(null);
String st=(String)content.getTransferData(DataFlavor.stringFlavor);
area.replaceRange(st,area.getSelectionStart(),area.getSelectionEnd());
}catch(Exception ex)
{}
}
});
all.addActionListener(new ActionListener() //全选
{
public void actionPerformed(ActionEvent e)
{
area.setSelectionStart(0);
area.setSelectionEnd(area.getText().length());
}
});
JMenu help=new JMenu("帮助");
JMenuItem about=new JMenuItem("关于");
help.add(about);
about.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFrame frame=new JFrame("关于");
frame.setSize(150,100);
frame.setLocation(400,300);
JTextArea area1=new JTextArea("制作人 :杨杰\n制作时间: 2011.8.03");
frame.add(area1);
frame.setVisible(true);
}
});
jmb.add(file);
jmb.add(edit);
jmb.add(help);
setSize(600,400);
setLocation(300,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void openText() //打开
{
JFileChooser chooser=new JFileChooser();
FileNameExtensionFilter filter=new FileNameExtensionFilter("Files","txt","java");
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(new File("."));
int result=chooser.showOpenDialog(Text.this);
if(result==JFileChooser.APPROVE_OPTION)
{
name=chooser.getSelectedFile().getPath();
setTitle(name);
try{
BufferedReader in=new BufferedReader(new FileReader(name));
String line=null;
String datas="";
while((line=in.readLine())!=null)
{
if(datas=="")
{
datas=datas+line;
}
else
{
datas=datas+"\n"+line;
}
}
area.setText(datas);
in.close();
}catch(Exception ex)
{
}
}
}
public void saveText() //保存
{
if(name==null)
{
JFileChooser chooser=new JFileChooser();
FileNameExtensionFilter filter=new FileNameExtensionFilter("Files","txt","java");
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(new File("."));
int result=chooser.showSaveDialog(Text.this);
if(result==JFileChooser.APPROVE_OPTION)
{
name=chooser.getSelectedFile().getPath();
try{
OutputStream out=new FileOutputStream(name);
String datas=area.getText();
out.write(datas.getBytes());
out.close();
}catch(Exception ex)
{
}
}
}
else
{
try{
OutputStream out=new FileOutputStream(name);
String datas=area.getText();
out.write(datas.getBytes());
out.close();
}catch(Exception ex)
{
}
}
}
public void anotherSaveText() //另存为
{
JFileChooser chooser=n
ew JFileChooser();
FileNameExtensionFilter filter=new FileNameExtensionFilter("Files","txt","java");
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(new File("."));
int result=chooser.showSaveDialog(Text.this);
if(result==JFileChooser.APPROVE_OPTION)
{
name=chooser.getSelectedFile().getPath();
try{
OutputStream out=new FileOutputStream(name);
String datas=area.getText();
out.write(datas.getBytes());
out.close();
}catch(Exception ex)
{
}
}
}
public static void main(String[] args)throws Exception
{
new Text();
}
}