Java編寫的文字編輯器(菜鳥作品)

破玉發表於2015-07-18
//這是主窗體檔案 Wordwin.java
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class Wordwin extends JFrame implements DocumentListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JMenuBar menubar=new JMenuBar();
    JMenu file=new JMenu("檔案");
    JMenu edit=new JMenu("編輯");
    JMenu geshi=new JMenu("格式");
    JMenu look =new JMenu("檢視");
    JMenu help =new JMenu("幫助");
    JTextArea wordArea=new JTextArea();
    JScrollPane imgScrollPane = new JScrollPane(wordArea);
    String [] str1={"新建","開啟","儲存","另存為","頁面設定","列印","退出"};
    String [] str2={"剪下","複製","貼上","查詢","替換"};
    String [] str3={"自動換行","字型"};
    Font f1=new Font("隸書",Font.PLAIN,15);
    Search d1=new Search();
    Font1 z1=new Font1(); 
    Change c1=new Change();
    int flag=0;
    String source="";
    public static void main(String[] args) {
      JFrame wordwin=new Wordwin();
      wordwin.setVisible(true);
	}
    public Wordwin(){
       c1.set(wordArea);
       z1.set(wordArea);
       setTitle("文字編輯器");
 	   Toolkit kit = Toolkit.getDefaultToolkit();
 	   Dimension screenSize = kit.getScreenSize();//獲取螢幕解析度
 	   setSize(screenSize.width/2,screenSize.height/2);//大小
 	   setLocation(screenSize.width/4,screenSize.height/4);//位置
 	   add(imgScrollPane,BorderLayout.CENTER);
 	   setJMenuBar(menubar);
 	   file.setFont(f1);
 	   edit.setFont(f1);
 	   geshi.setFont(f1);
 	   look.setFont(f1);
 	   help.setFont(f1);
 	   menubar.add(file);
 	   menubar.add(edit);
 	   menubar.add(geshi);
 	   menubar.add(look);
 	   menubar.add(help);
 	   wordArea.getDocument().addDocumentListener(this);
 	   for(int i=0;i<str1.length;i++){
 		  JMenuItem item1= new JMenuItem(str1[i]);
 		  item1.addActionListener(new MyActionListener1());
 		  item1.setFont(f1);
 		  file.add(item1);
 	   }
 	  for(int i=0;i<str2.length;i++){
 		  JMenuItem item2= new JMenuItem(str2[i]);
 		 item2.addActionListener(new MyActionListener1());
 		  item2.setFont(f1);
 		  edit.add(item2);
 	   }
 	 for(int i=0;i<str3.length;i++){
		  JMenuItem item3= new JMenuItem(str3[i]);
		  item3.addActionListener(new MyActionListener1());
		  item3.setFont(f1);
		  geshi.add(item3);
	   }
    }
    public void changedUpdate(DocumentEvent e) {
		flag=1;
	}
	
	public void insertUpdate(DocumentEvent e) {
		flag=1;
	}
	public void removeUpdate(DocumentEvent e) {
		flag=1;
	}
    void open(){
    	FileDialog  filedialog=new FileDialog(this,"開啟",FileDialog.LOAD);
    	filedialog.setVisible(true);
    	String path=filedialog.getDirectory();
        String name=filedialog.getFile();
        if(path!=null && name!=null){
        	 FileInputStream file = null;
			try {
				file = new FileInputStream(path+name);
			} catch (FileNotFoundException e) {
				
			}
        	 InputStreamReader put =new InputStreamReader(file);
        	 BufferedReader in=new BufferedReader(put);
        	 String temp="";
        	  String now = null;
			try {
				now = (String)in.readLine();
			} catch (IOException e) {
				
				e.printStackTrace();
			}
        	  while(now!=null){
        	      temp+=now+"\r\n";
        	      try {
					now=(String)in.readLine();
				} catch (IOException e) {
					
					e.printStackTrace();
				}
        	       }

        	   wordArea.setText(temp);
        }
    	
    }

    void save(){
    	FileDialog  filedialog=new FileDialog(this,"儲存",FileDialog.SAVE);
    	filedialog.setVisible(true);
    	if(filedialog.getDirectory()!=null && filedialog.getFile()!=null){
    		 OutputStreamWriter out = null;
			try {
				out = new OutputStreamWriter(new FileOutputStream(filedialog.getDirectory()+filedialog.getFile()));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
    		 try {
				out.write(wordArea.getText());
			} catch (IOException e) {
				e.printStackTrace();
			}
    		 flag=0;
    		 try {
				out.close();
				source=wordArea.getText();
			} catch (IOException e) {
				e.printStackTrace();
			}
    	}
    }
    void newfile(){
    	if(flag==0){
    		wordArea.setText("");
    	}
    	if(flag==1){
    		int m=  JOptionPane.showConfirmDialog(this,"是否儲存該檔案");
    		if(m==0){
    			save();
    			wordArea.setText("");
    		}
    		
    		if(m==1){ 
    			//System.exit(0);
    			wordArea.setText("");
    			flag=0;
    			}
    	}
    }
    void exit(){
    	if(flag==0){
    		System.exit(0);
    	}
    	if(flag==1){
    		int m=  JOptionPane.showConfirmDialog(this,"是否儲存該檔案");
    		if(m==0){
    			save();
    		}
    		if(m==1){ 
    			System.exit(0);
    			}
    	}
    }
    class MyActionListener1 implements ActionListener{
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource()instanceof JMenuItem){
    			if(e.getActionCommand()=="剪下"){
    				wordArea.cut();
    			}
    			if(e.getActionCommand()=="複製"){
    				wordArea.copy();
    			}
    			if(e.getActionCommand()=="貼上"){
    				wordArea.paste();
    			}
    			if(e.getActionCommand()=="查詢"){
    				d1.setVisible(true);
    			}
    			if(e.getActionCommand()=="字型"){
    				z1.setVisible(true);
    			}
    			if(e.getActionCommand()=="替換"){
    				c1.setVisible(true);
    			}
    			if(e.getActionCommand()=="開啟"){
    				open();
    			}
    			if(e.getActionCommand()=="儲存"){
    				save();
    			}
    			if(e.getActionCommand()=="新建"){
    				newfile();
    			}
    			if(e.getActionCommand()=="退出"){
    				exit();
    			}
    		}
    		
    	}
    	   
}
}

//這是查詢替換的實現Change.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Change extends JDialog{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JLabel l1=new JLabel("查詢內容");
	JLabel l2=new JLabel("替換為");
	JTextField t1=new JTextField(10);
	JTextField t2=new JTextField(10);
	JButton b1=new JButton("替換");
	JButton b2=new JButton("全部替換");
	JButton b3=new JButton("取消");
	JTextArea a1=new JTextArea();
	Font f1=new Font("隸書",Font.PLAIN,15);
	//int m;
	void set(JTextArea n){
		a1=n;
	}
	public Change(){
		setTitle("替換");
		setSize(500,300);
		setLocation(200,300);
		setLayout(new FlowLayout());
		l1.setFont(f1);
		l2.setFont(f1);
		t1.setFont(f1);
		t2.setFont(f1);
		b1.setFont(f1);
		b2.setFont(f1);
		b3.setFont(f1);
		add(l1);
		add(t1);
		add(l2);
		add(t2);
		add(b1);
		add(b2);
		add(b3);
		b1.addActionListener(new MyActionListener3());
		b2.addActionListener(new MyActionListener3());
		b3.addActionListener(new MyActionListener3());
	}
	class MyActionListener3 implements ActionListener{
		
public void actionPerformed(ActionEvent e2) {
	int m;
	String source=a1.getText();
	String find=t1.getText();
	String change=t2.getText();
			if(e2.getActionCommand()=="取消"){
				setVisible(false);
				t1.setText("");
				t2.setText("");
			}
			if(e2.getActionCommand()=="替換"){
				m=source.indexOf(find,0);
				String s1=source.substring(0,m);
				String s2=source.substring(m+find.length());
				source=s1+change+s2;
				if(m==-1){
					JOptionPane.showMessageDialog(null,"對不起,沒找到您要找的內容!");
				}else{
					a1.setText(source);
				}
			}
			if(e2.getActionCommand()=="全部替換"){
				m=-change.length();
				while(true){
					m=source.indexOf(find,m+change.length());
				    if(m==-1)
				    	break;
				    else{
				    String s1=source.substring(0,m);
					String s2=source.substring(m+find.length());
					source=s1+change+s2;
				    }
				}
				a1.setText(source);
			}
			
		}
	}
	
}

//這是改變字型的Font1.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Font1 extends JDialog implements ItemListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JPanel panel1 = new JPanel();
	JPanel panel2 = new JPanel();
	JPanel panel3 = new JPanel();
	JComboBox comboBox1 = new JComboBox();
	JComboBox comboBox2 = new JComboBox();
	JComboBox comboBox3 = new JComboBox();
	JLabel lab1=new JLabel("字型:");
	JLabel lab2=new JLabel("字形:");
	JLabel lab3=new JLabel("字號:");
	String name=new String("宋體");
	Font f1=new Font("隸書",Font.PLAIN,15);
	int style=1;
	int size=12;
	String []array2=new String[]{"常       規","傾        斜","加       粗"};
	String []array3=new String[]{"14","15","15","16","17","18"};
	GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
    String [] fontName=ge.getAvailableFontFamilyNames();
    JButton b1=new JButton("確定");
    JButton b2=new JButton("顏色");
    JTextArea a1=new JTextArea();
    void set(JTextArea n){
		a1=n;
	}
	public Font1(){
		setTitle("字型");
		setSize(500,600);
		setLayout(new FlowLayout());
		//panel1.setLocation(100, 200);
		lab1.setFont(f1);
		lab2.setFont(f1);
		comboBox1.setModel(new DefaultComboBoxModel(fontName));
		comboBox1.setFont(f1);
		for(int i=1;i<fontName.length;i++){
			//comboBox1.setSelectedIndex(i);
			comboBox1.setSelectedItem(fontName);
			//comboBox1.addItem(fontName);
		}
		comboBox2.setModel(new DefaultComboBoxModel(array2));
		comboBox2.setFont(f1);
		for(int i=1;i<array2.length;i++){
			//comboBox2.setSelectedIndex(i);
			comboBox2.setSelectedItem(array2);
			//comboBox2.addItem(array2);
		}
		comboBox3.setModel(new DefaultComboBoxModel(array3));
		comboBox3.setFont(f1);
		for(int i=1;i<array3.length;i++){
			//comboBox2.setSelectedIndex(i);
			comboBox2.setSelectedItem(array3);
			//comboBox3.addItem(array3);
		}
		
		panel1.add(lab1);
		panel1.add(comboBox1);
		panel2.add(lab2);
		panel2.add(comboBox2);
		panel3.add(lab3);
		panel3.add(comboBox3);
		panel3.add(b1);
		panel3.add(b2);
		b2.addActionListener(new MyActionListener3());
		comboBox1.addItemListener(this);
		comboBox2.addItemListener(this);
		comboBox3.addItemListener(this);
		b1.addActionListener(new MyActionListener3());
		add(panel1);
		add(panel2);
		add(panel3);
	}
		public void itemStateChanged(ItemEvent e) {
			if(e.getSource()==comboBox1){
			    name=comboBox1.getSelectedItem().toString();
			}
			if(e.getSource()==comboBox2){
			  String s1=comboBox2.getSelectedItem().toString();
			  if(s1.equals("加粗")){
			  style=Font.BOLD;
			  }
			  if(s1.equals("傾斜")){
				  style=Font.ITALIC;
			  }
			  if(s1.equals("常規")){
				  style=Font.PLAIN;
			  }
			}
			if(e.getSource()==comboBox3){
				 String s2=comboBox3.getSelectedItem().toString();
				 size=Integer.parseInt(s2);
			}
			
		}
		
		class MyActionListener3 implements ActionListener{
			public void actionPerformed(ActionEvent e2) {
				Font f=new Font(name,style,size);
				a1.setFont(f);
				if(e2.getActionCommand()=="顏色"){
					setcolor();
				}
			}
		}
		 void setcolor(){
			    Color	fontcolor=JColorChooser.showDialog(this,"字型顏色選擇",a1.getForeground());
			    	  a1.setForeground(fontcolor);
			    }
	}


//這是查詢視窗,對不起,查詢功能沒實現Search.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Search extends JDialog{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JLabel l1=new JLabel("查詢內容");
	JTextField t1=new JTextField(10);
	JButton b1=new JButton("查詢下一個");
	JButton b2=new JButton("取消");
	Font f1=new Font("隸書",Font.PLAIN,15);
	public Search(){
		setTitle("查詢");
		setSize(300,200);
		setLayout(new FlowLayout());
		b1.setFont(f1);
		b2.setFont(f1);
		l1.setFont(f1);
		add(l1);
		add(t1);
		add(b1);
		add(b2);
		b2.addActionListener(new MyActionListener2());
	}
	class MyActionListener2 implements ActionListener{
		public void actionPerformed(ActionEvent e1) {
			setVisible(false);
		}
		
	}
}


執行截圖:




 

相關文章