⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mianframe.java

📁 一个简单的文本编辑器,模趽msword写的哈!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package chapter14;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.*;
import javax.swing.JMenuBar;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;


public class Mianframe{
    JDesktopPane desktop;
    JFrame topframe;
    JMenuBar mbar;
    JToolBar toolbar;
    JPopupMenu popupmenu;
    static final String aboutmsg="a java simple edition example ver 1.0\n\nAn application" +
    		"written to show off the function of editor.\n\nWritten by chenbiao\n\n" +
    		"Copyright@2007 by 陈彪,all right reserved.";
    public Mianframe(){
    	topframe=new JFrame("mainframe");
    	topframe.setBounds(20,20,1000,650);
    	buildcontent();//把这行去掉,结果是不一样的!
    	buildmenubar();
    	buildtoolbar();
    	popupmenu=buildpopupmenu();
    	topframe.addWindowListener(new WindowAdapter(){
    		public void windowClosing(WindowEvent e){
    			quit();
    		}
    	});
    	topframe.setVisible(true);
    } 
    public void quit(){
    	System.exit(0);
    }
    protected void buildcontent(){
    	desktop=new JDesktopPane();
        topframe.getContentPane().add(desktop);
    }
    public void buildmenubar(){
    	String menuname;
    	mbar=new JMenuBar();
    	menuname="File";
    	String[] filefunction={"new","open","close","quit"};
    	char[] filemnemonic={'N','O','C','Q'};
    	JMenu mfile=buildmenu(menuname,filefunction,filemnemonic);
    	
    	menuname="Edit";
    	String[] editfunction={"undo","redo","addseparator","copy","cut","paste"};
    	char[] editmnemonic={'U','R','A','C','O','P'};
    	JMenu medit=buildmenu(menuname,editfunction,editmnemonic);
    	
    	menuname="Insert";
    	String[] insertfunction={"picture","object"};
    	char[] insertmnemonic={'P','O'};
    	JMenu minsert=buildmenu(menuname,insertfunction,insertmnemonic);
    	
    	menuname="Demo";
    	String[] demofunction={"slider","tree structure","table structure","password"};
    	char[] demomnemonic={'S','T','A','P'};
    	JMenu mdemo=buildmenu(menuname,demofunction,demomnemonic);
    	
    	menuname="Help";
    	String[] helpfunction={"about this programe","open help window"};
    	char[] helpmnemonic={'A','O'};
    	JMenu mhelp=buildmenu(menuname,helpfunction,helpmnemonic);
    	
        JMenu malign=buildalignmenu();
    	
    	mfile.setMnemonic('F');
    	medit.setMnemonic('E');
    	minsert.setMnemonic('I');
    	mdemo.setMnemonic('D');
    	mhelp.setMnemonic('H');
    	malign.setMnemonic('A');
    	
    	mbar.add(mfile);
    	mbar.add(medit);
    	mbar.add(minsert);
    	mbar.add(mdemo);
    	mbar.add(mhelp);
    	mbar.add(malign);

    	topframe.setJMenuBar(mbar);
    }
    
    public JMenu buildmenu(String menuname,String[] function,char[] mnemonic){
    	ActionSet action;
    	JMenuItem item;
    	JMenu menu=new JMenu(menuname);
    	for(int i=0;i<function.length;i++){
    		if(function[i].equals("addseparator")){
    			menu.addSeparator();
    			continue;
    		}
    		item=menu.add(new ActionSet(function[i],null));//比较特殊,注意下怎么运行的。已经加入了事情处理机制。
    		item.setActionCommand(function[i]);
    		//if(mnemonic!=null){//这这里都可以的,不需要判断。
    			item.setMnemonic(mnemonic[i]);
    			item.setAccelerator(KeyStroke.getKeyStroke(mnemonic[i],java.awt.Event.CTRL_MASK));    			
    		//}
    	}
    	return menu;
    }
    
    class ActionSet extends AbstractAction {
        public ActionSet(String name,Icon icon){
        	super(name,icon);
        	if(name.equals("undo")||name.equals("redo"))
        		this.setEnabled(false);
        }
        
        public void updateUndoState(){
        	Newframe currentframe=(Newframe)desktop.getSelectedFrame();
        	if(currentframe==null){return;}
        	undo=currentframe.getUndoManager();
        	JMenuItem undoitem=mbar.getMenu(1).getItem(0);
        	if(undo.canUndo()){
        		undoitem.setEnabled(true);
        	}else{
        		undoitem.setEnabled(false);
        	}
        }
        
        public void updateRedoState(){
        	Newframe currentframe=(Newframe)desktop.getSelectedFrame();
        	if(currentframe==null){return;}
        	undo=currentframe.getUndoManager();//能够撤销就能够重做。
        	JMenuItem redoitem=mbar.getMenu(1).getItem(1);
        	if(undo.canRedo()){
        		redoitem.setEnabled(true);
        	}else{
        		redoitem.setEnabled(false);
        	}
        }
                  
    	public void actionPerformed(ActionEvent e) {
            String command=e.getActionCommand();
            if(command.equals("new")){
        	Newframe internalframe=new Newframe();
        	desktop.add(internalframe);
        	try{
        		internalframe.setVisible(true);
        		internalframe.setSelected(true);
        	}catch(java.beans.PropertyVetoException e2){
        		e2.printStackTrace();
        	}
        	JTextPane textpane=internalframe.getTextPane();//觉得应该加上getdocument才好。
        	textpane.addMouseListener(new showpopupmenu(popupmenu));
        	textpane.requestFocus();
        }
            if(command.equals("open")){
            	Newframe internalframe=new Newframe();
            	desktop.add(internalframe);
            	try{
            		internalframe.setVisible(true);
            		internalframe.setSelected(true);
            	}catch(java.beans.PropertyVetoException e2){
            		e2.printStackTrace();
            	}
            	JTextPane textpane=internalframe.getTextPane();
            	ReadDoccontent ris=new ReadDoccontent(textpane,topframe,internalframe);
            }
    		if(command.equals("close")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){return;}
    			else{
    				desktop.remove(currentframe);
    				desktop.updateUI();
    			}
    		}
    		
    		if(command.equals("quit")){
    			quit();
    		}
    		if(command.equals("undo")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){return;}
    			undo=currentframe.getUndoManager();
    			try{
    				undo.undo();
    			}catch(CannotUndoException ex){
    				ex.printStackTrace();
    			}
    			updateUndoState();
    			updateRedoState();
    		}
    		if(command.equals("redo")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){return;};
    			undo=currentframe.getUndoManager();
    			try{
    				undo.redo();
    			}catch(CannotRedoException ex){
    				ex.printStackTrace();
    			}
    			updateRedoState();
    			updateUndoState();
    		}
    		if(command.equals("copy")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){return;}
    			currentframe.getTextPane().copy();
    		}
    		if(command.equals("cut")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){return;}
    			currentframe.getTextPane().cut();
    		}
    		if(command.equals("paste")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){return;}
    			currentframe.getTextPane().paste();
    		}
    		if(command.equals("Bold")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){
    				return;
    			}
    			JTextPane textpane=currentframe.getTextPane();
    			MutableAttributeSet attrset=textpane.getInputAttributes();
    			//attrset.removeAttribute(attrset);
    			//boolean bold;
				//if(StyleConstants.isBold(attrset))
    				 //bold=false;
    			//else bold=true;
    			boolean bold=(StyleConstants.isBold(attrset))?false:true;
    			SimpleAttributeSet sas=new SimpleAttributeSet();
    			StyleConstants.setBold(sas,bold);
    			textpane.setCharacterAttributes(sas,false);
    		}
    		if(command.equals("Italic")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){
    				return;
    			}
    			JTextPane textpane=currentframe.getTextPane();
    			MutableAttributeSet attrset=textpane.getInputAttributes();
    			//attrset.removeAttribute(attrset);
    			//boolean italic;
    			SimpleAttributeSet sas=new SimpleAttributeSet();
    			//if(StyleConstants.isItalic(attrset)){
    				//italic=false;
    			   // sas=new SimpleAttributeSet();
    			    //StyleConstants.setItalic(sas,false);
    			  //  }
    			//else //italic=true;
    			boolean italic=(StyleConstants.isItalic(attrset))?false:true;    			
    			StyleConstants.setItalic(sas,true);
    			textpane.setCharacterAttributes(sas,false);
    		}
    		if(command.equals("Underline")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){return;}
    			JTextPane textpane=currentframe.getTextPane();
    			MutableAttributeSet attrset=textpane.getInputAttributes();
    			//attrset.removeAttribute(attrset);
    			//boolean underline=(StyleConstants.isUnderline(attrset))?false:true;
    			boolean underline;
    			if(StyleConstants.isUnderline(attrset))
    				underline=false;
    			else underline=true;
    			SimpleAttributeSet sas=new SimpleAttributeSet();
    			StyleConstants.setUnderline(sas,underline);
    			textpane.setCharacterAttributes(sas,false);
    		}
    		if(command.equals("set color")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){return;}
    			JTextPane textpane=currentframe.getTextPane();
    			Color color=JColorChooser.showDialog(topframe, "change font color", Color.black);
    			if(color!=null){
    				SimpleAttributeSet sas=new SimpleAttributeSet();
    				StyleConstants.setForeground(sas,color);
    				textpane.setCharacterAttributes(sas,false);
    			}
    		}
    		if(command.equals("type")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){return;}
    			JTextPane textpane=currentframe.getTextPane();
    			MutableAttributeSet attrset=textpane.getInputAttributes();
    			String s=(((JComboBox)e.getSource()).getSelectedItem()).toString();
    			SimpleAttributeSet sas=new SimpleAttributeSet();
    			StyleConstants.setFontFamily(sas, s);
    			textpane.setCharacterAttributes(sas,false);
    		}
    		if(command.equals("size")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){return;}
    			JTextPane textpane=currentframe.getTextPane();
    			MutableAttributeSet attrset=textpane.getInputAttributes();
    			int s=Integer.parseInt(((JComboBox)e.getSource()).getSelectedItem().toString());
    			SimpleAttributeSet sas=new SimpleAttributeSet();
    			StyleConstants.setFontSize(sas,s);
    			textpane.setCharacterAttributes(sas, false);    			
    		}
    		if(command.equals("picture")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null)return;
    			JTextPane textpane=currentframe.getTextPane();
    			JFileChooser filechooser=new JFileChooser("E:\\我的地盘\\我的文件\\java");
    			filechooser.addChoosableFileFilter(new picturefilter("jpg"));
    			filechooser.addChoosableFileFilter(new picturefilter("gif"));
    			int result=filechooser.showOpenDialog(desktop);
    			if(result==filechooser.APPROVE_OPTION){
    				File file=filechooser.getSelectedFile();
    				textpane.insertIcon(new ImageIcon(file.getPath()));
    			}
    			else if(result==filechooser.CANCEL_OPTION)
    			{};
    		}
    		if(command.equals("object")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){return;}
    			JTextPane textpane=currentframe.getTextPane();
    			JFileChooser filechooser=new JFileChooser("E:\\我的地盘\\我的文件\\java");    			
    			filechooser.addChoosableFileFilter(new filefilter("doc"));
    			filechooser.addChoosableFileFilter(new filefilter("txt"));
    			int result=filechooser.showOpenDialog(topframe);
    			if(result==filechooser.APPROVE_OPTION){
    				File file=filechooser.getSelectedFile();
    				FileInputStream fileinput = null;
					try {
						fileinput = new FileInputStream(file);
					} catch (FileNotFoundException e2) {
						e2.printStackTrace();
					}
    				InputStreamReader in = null;
					try {
						in = new InputStreamReader(new FileInputStream(file));
					} catch (FileNotFoundException e2) {
						e2.printStackTrace();
					} 				
     				int length=textpane.getDocument().getLength();
     				int readbyte;
     				AttributeSet attrset = null;
					try {
						while((readbyte=in.read())!=-1){   			    
						textpane.getDocument().insertString(length,String.valueOf((char)readbyte),attrset);	
						}//方法很重要。
					} catch (IOException e1) {
						e1.printStackTrace();
					} catch (BadLocationException e1) {
						e1.printStackTrace();
					}
				}    			
    		}
    		if(command.equals("save")){
    			Newframe currentframe=(Newframe)desktop.getSelectedFrame();
    			if(currentframe==null){return;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -