import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.undo.UndoManager;
import javax.swing.*;
public class textframe extends JFrame implements ActionListener, WindowListener, MouseListener{
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
search.dispose();
replace.dispose();
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
private JCheckBoxMenuItem autohh;
public String filename=null;
Toolkit toolkit=Toolkit.getDefaultToolkit();
Clipboard clipboard=toolkit.getSystemClipboard();//剪贴板对象
int chg=0; //标记是有否文本改变
Dialog search=new Dialog(this,"查找",true);//查找对话框
Dialog replace=new Dialog(this,"替换",true);
TextField txtf=new TextField(25);
TextField resourcet=new TextField(15);
TextField replacet=new TextField(15);
static int stringindex1,stringindex2=0;//指示字符串的位置
int locate=0;
private Color samplecolor=Color.black; //默认字体颜色
FontFrame myfont;
public int mdifnumber=0;
Button button1 = new Button();
JDesktopPane desk = null;
public static void main(String args[]){
textframe f=new textframe();
f.setVisible(true);
}
public textframe(){
super("mytext");
setSize(800,700);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
JInternalFrame[] array = desk.getAllFrames();
jmidframe tempf=null;
for ( int a= 0; a < array.length; a++ ){
tempf=(jmidframe)array[a];
if(tempf.textchg==1)
{
chg=1;
break;
}
}
if(chg==1)
for ( int a= 0; a < array.length; a++ ){
tempf=(jmidframe)array[a];
if(tempf.textchg==1)
{
if(JOptionPane.showConfirmDialog(null,
"是否保存", tempf.getTitle()+"已改变是否保存", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
if(tempf.filename!=null)
writefile(tempf,tempf.filename);
else{
JFileChooser chooser = new JFileChooser();
chooser.setAcceptAllFileFilterUsed(false);
chooser.addChoosableFileFilter(new MyFileFilter());
int returnVal = chooser.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
filename=file.getAbsoluteFile().toString()+".txt";
writefile(tempf,filename);
}
}
}
array[a].dispose();
}
else
array[a].dispose();
}
System.exit(0);
}
});
JMenuBar mb=new JMenuBar();
setJMenuBar(mb);
JMenu mfile=new JMenu("文件");
JMenu medit=new JMenu("编辑");
JMenu mgs=new JMenu("格式");
mgs.addMouseListener(this);
JMenu mhelp=new JMenu("帮助");
JMenuItem minew=new JMenuItem("新建");
minew.addActionListener(this);
JMenuItem miopen=new JMenuItem("打开");
miopen.addActionListener(this);
JMenuItem misave=new JMenuItem("保存");
misave.addActionListener(this);
JMenuItem minewsave=new JMenuItem("另存为..");
minewsave.addActionListener(this);
JMenuItem miclose=new JMenuItem("退出");
miclose.addActionListener(this);
mfile.add(minew);
mfile.add(miopen);
mfile.add(misave);
mfile.add(minewsave);
mfile.add(miclose);
JMenuItem miundo=new JMenuItem("撤销");
miundo.addActionListener(this);
JMenuItem micut=new JMenuItem("剪切");
micut.addActionListener(this);
JMenuItem micopy=new JMenuItem("复制");
micopy.addActionListener(this);
JMenuItem mipaste=new JMenuItem("粘贴");
mipaste.addActionListener(this);
JMenuItem midelete=new JMenuItem("删除");
midelete.addActionListener(this);
JMenuItem misearch=new JMenuItem("查找");
misearch.addActionListener(this);
JMenuItem mireplace=new JMenuItem("替换");
mireplace.addActionListener(this);
JMenuItem miselectall=new JMenuItem("全选");
miselectall.addActionListener(this);
medit.add(miundo);
medit.addSeparator();
medit.add(micut);
medit.add(micopy);
medit.add(mipaste);
medit.add(midelete);
medit.addSeparator();
medit.add(misearch);
medit.add(mireplace);
medit.addSeparator();
medit.add(miselectall);
autohh=new JCheckBoxMenuItem("自动换行");
autohh.addActionListener(this);
mgs.add(autohh);
JMenuItem micolor=new JMenuItem("颜色");
micolor.addActionListener(this);
JMenuItem mifont=new JMenuItem("字体");
mifont.addActionListener(this);
mgs.add(micolor);
mgs.add(mifont);
JMenuItem miabout=new JMenuItem("关于");
miabout.addActionListener(this);
mhelp.add(miabout);
mb.add(mfile);
mb.add(medit);
mb.add(mgs);
mb.add(mhelp);
/*JPanel textpanel=new JPanel();
texta=new JTextArea();
//texta.setLineWrap(true);
//add(texta);
texta.getDocument().addUndoableEditListener(undo);
JScrollPane scrolletext=new JScrollPane(texta);
scrolletext.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrolletext.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
textpanel.add(scrolletext);
add(scrolletext,BorderLayout.CENTER);*/
desk = new JDesktopPane();
add(desk,BorderLayout.CENTER);
search_dialog();
search.addWindowListener(this);
replace_dialog();
replace.addWindowListener(this);
//new java.awt.event.ActionListener() { //菜单1的事件监听
//public void actionPerformed(this) {
//menuItem1_actionPerformed(e); //菜单事件函数
//}
//});
JToolBar toolBar = new JToolBar();//实例化工具条
JButton newbt = new JButton("新建",new ImageIcon("images/new.png"));
newbt.setRequestFocusEnabled(false); //设置不需要焦点
newbt.addActionListener(this);
JButton openbt = new JButton("打开",new ImageIcon("images/open.png"));
openbt.setRequestFocusEnabled(false); //设置不需要焦点
openbt.addActionListener(this);
JButton savebt = new JButton("保存",new ImageIcon("images/save.gif"));
savebt.setRequestFocusEnabled(false); //设置不需要焦点
savebt.addActionListener(this);
JButton undobt = new JButton("撤销",new ImageIcon("images/undo.png"));
undobt.setRequestFocusEnabled(false); //设置不需要焦点
undobt.addActionListener(this);
JButton cutbt = new JButton("剪切",new ImageIcon("images/cut.png"));
cutbt.setRequestFocusEnabled(false); //设置不需要焦点
cutbt.addActionListener(this);
JButton copybt = new JButton("复制",new ImageIcon("images/copy.png"));
copybt.setRequestFocusEnabled(false); //设置不需要焦点
copybt.addActionListener(this);
JButton pastebt = new JButton("粘贴",new ImageIcon("images/paste.png"));
pastebt.setRequestFocusEnabled(false); //设置不需要焦点
pastebt.addActionListener(this);
JButton deletebt = new JButton("删除",new ImageIcon("images/delete.png"));
deletebt.setRequestFocusEnabled(false); //设置不需要焦点
deletebt.addActionListener(this);
toolBa