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

📄 document.java

📁 一个java写的文本编辑器
💻 JAVA
字号:
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class document extends JFrame implements ActionListener
{
	JMenuBar bar=new JMenuBar();
	JTextArea textarea=new JTextArea();
	JMenu menu1=new JMenu("文件");
	JMenu menu2=new JMenu("编辑");
	JMenu menu3=new JMenu("格式");
	JMenu menu4=new JMenu("帮助");
	JMenuItem newf=new JMenuItem("新建");
	JMenuItem open=new JMenuItem("打开");
	JMenuItem save=new JMenuItem("保存");
	JMenuItem quit=new JMenuItem("退出");
	JMenuItem cancel=new JMenuItem("撤消");
	JMenuItem cut=new JMenuItem("剪切");
	JMenuItem copy=new JMenuItem("复制");
	JMenuItem paste=new JMenuItem("粘贴");
	JMenuItem delete=new JMenuItem("删除");
	JMenuItem select=new JMenuItem("全选");
	JMenuItem auto=new JMenuItem("自动换行");
	JMenuItem font=new JMenuItem("字体");
	JMenuItem about=new JMenuItem("关于");
	public int a=0,b=0,c=0,style=Font.PLAIN,size=15;
    public String s1="red:"+a+" "+"green:"+b+" "+"blue"+c,
                  s2="宋体";
      
    public String[] sz1={"10","16","24","30","32","36"},
                    sz2={"宋体","黑体","幼圆","隶书","行楷","Arial","Georgia"},
                    sz3={"粗体","倾斜","常规","粗斜"};
    JDialog dialog=new JDialog(this,"字体",true);
    JDialog edit=new JDialog(this,"关于",true);
    Container cp=dialog.getContentPane();
    Container cq=edit.getContentPane();
    JLabel[] lb=new JLabel[8];     
    JLabel lb1=new JLabel(s1,JLabel.LEFT);
    JButton b1=new JButton("确定"),
            b2=new JButton("取消");
    JComboBox jc1=new JComboBox(),
              jc2=new JComboBox(),
              jc3=new JComboBox();
    JScrollBar js1=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);
    JScrollBar js2=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);
    JScrollBar js3=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);
	
 public document()
 {
   	super("文本编辑器");
 	getContentPane().add(new JScrollPane(textarea));
 	textarea.setEditable(true);
 	bar.setOpaque(true);
 	setJMenuBar(bar);
    menu1.add(newf);
    menu1.add(open);
    menu1.add(save);
    menu1.addSeparator();  //添加一斜线
    menu1.add(quit);
    menu2.add(cancel);
    menu2.add(cut);
    menu2.add(copy);
    menu2.add(paste);
    menu2.add(delete);
    menu2.add(select);
    menu3.add(auto);
    menu3.add(font);
    menu4.add(about);
    bar.add(menu1);
    bar.add(menu2);
    bar.add(menu3);
    bar.add(menu4);
    newf.addActionListener(this);
    open.addActionListener(this);
    save.addActionListener(this);
    quit.addActionListener(this);
    cancel.addActionListener(this);
    cut.addActionListener(this);
    copy.addActionListener(this);
    paste.addActionListener(this);
    delete.addActionListener(this);
    select.addActionListener(this);
    auto.addActionListener(this);
    font.addActionListener(this);
    about.addActionListener(this);
    addWindowListener(new WinLs());
 }	
 public void actionPerformed(ActionEvent e)
 {
 	if(e.getSource()==b1)
       {
           textarea.setForeground(new Color(a,b,c));
           Font font=new Font(s2,style,size);
           textarea.setFont(font); 
           dialog.dispose();
           textarea.append(s2);
        }
    if(e.getSource()==b2)dialog.dispose(); 
 	if(e.getSource()==newf)textarea.setText("");
 	if(e.getSource()==open) 
      {
         JFileChooser chooser=new JFileChooser("H:/JBuilder9/jdk1.4/bin/create/Frame");
         if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
           {
             try
               {                  
                  File file=chooser.getSelectedFile();
                  FileReader fr=new FileReader(file);
                  char[] buffer=new char[65535];
                  textarea.setText("");                     
                  int length;
                  while((length=fr.read(buffer))!=-1)
                   {
                     String str=String.valueOf(buffer);
                     textarea.append(str);
                   }
                  }
             catch(IOException ex)
               {textarea.append("IOException");}
            }
       } 
 	if(e.getSource()==save)
 	{
      JFileChooser chooser=new JFileChooser("H:/JBuilder9/jdk1.4/bin/create/Frame");
      if(chooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
        {
          try
             {
                File file=chooser.getSelectedFile();
                FileWriter out=new FileWriter(file);  
                String str=textarea.getText();                                   
                char[] buffer=new char[1024];
                while(str.length()!=0)
                  {
                   if(str.length()>1024)
                      {
                        buffer=str.substring(0,1024).toCharArray();
                        str=str.substring(1024);
                        out.write(buffer);
                        out.flush();
                       }
                    else
                       {
                        buffer=str.toCharArray();
                        out.write(buffer);
                        out.flush();                               
                        str="";
                        }
                    }
               }
             catch(IOException es)
               {textarea.setText("IOException");}
          }
      }
 	if(e.getSource()==quit)System.exit(0);
 	if(e.getSource()==auto)textarea.setLineWrap(true);
 	if(e.getSource()==font){fdialog();}
 	if(e.getSource()==about){edition();System.exit(0);}
 }


 public void fdialog()
 {
   lb[0]=new JLabel("大小"); 
   lb[1]=new JLabel("颜色",JLabel.LEFT); 
   lb[2]=new JLabel("字体");  
   lb[3]=new JLabel("红色",JLabel.LEFT);
   lb[4]=new JLabel("绿色",JLabel.LEFT);
   lb[5]=new JLabel("蓝色",JLabel.LEFT);
   lb[6]=new JLabel("");
   lb[7]=new JLabel("字型");
   for(int i=0;i<6;i++)
       jc2.addItem(sz1[i]);
   for(int i=0;i<7;i++)
       jc1.addItem(sz2[i]);
   for(int i=0;i<4;i++)
       jc3.addItem(sz3[i]);
            
    Box box=Box.createVerticalBox();
    cp.add(box);
    Box box1=Box.createHorizontalBox();
    box1.add(lb[1]);
    box1.add(lb1);
    box.add(box1);
              
    Box box2=Box.createHorizontalBox();
    lb[6].setMaximumSize(new Dimension(200,20));
    lb[6].setBackground(new Color(a,b,c));
    lb[6].setBorder(BorderFactory.createEtchedBorder());
    lb[6].setOpaque(true);            
    box2.add(lb[6]);              
    box.add(box2);

    Box box3=Box.createVerticalBox();
    box3.add(lb[3]);
    js1.addAdjustmentListener(new adjust());
    box3.add(js1);
    box.add(box3);
           
    Box box4=Box.createVerticalBox();
    box4.add(lb[4]);
    js2.addAdjustmentListener(new adjust());
    box4.add(js2);
    box.add(box4);
              
    Box box5=Box.createVerticalBox();
    box5.add(lb[5]);
    js3.addAdjustmentListener(new adjust());
    box5.add(js3);
    box.add(box5);
              
    Box box6=Box.createHorizontalBox();
    box6.add(lb[0]);
    jc2.addItemListener(new item());
    box6.add(jc2);
    box6.add(lb[7]);
    jc3.addItemListener(new item());
    box6.add(jc3);
    box6.add(lb[2]);
    jc1.addItemListener(new item());
    box6.add(jc1);
    box.add(box6);  
           
    Box box7=Box.createHorizontalBox(); 
    b1.addActionListener(this);
    box7.add(b1);
    b2.addActionListener(this);
    box7.add(b2); 
    box.add(box7);   

    dialog.setBounds(0,0,300,210);
    dialog.setResizable(false);
    dialog.show();
            
 } 
 class adjust implements AdjustmentListener
 {
    public void adjustmentValueChanged(AdjustmentEvent ea)
    {
       if((JScrollBar)ea.getSource()==js1)
          a=ea.getValue();
       if((JScrollBar)ea.getSource()==js2)
           b=ea.getValue();
       if((JScrollBar)ea.getSource()==js3)
           c=ea.getValue();
           lb[6].setBackground(new Color(a,b,c));                
           lb1.setText(s1);
    }
  }
  class item implements ItemListener
  {
     public void itemStateChanged(ItemEvent ei)
     {
         String strr=(String)ei.getItem();
         if(strr==sz3[0])
            style=Font.BOLD;
         if(strr==sz3[1])
            style=Font.ITALIC;
         if(strr==sz3[2])
            style=Font.PLAIN;
         if(strr==sz3[3])
            style=Font.BOLD+Font.ITALIC;
         for(int i=0;i<7;i++)
         if(strr==sz2[i])
            s2=sz2[i];
         for(int i=0;i<6;i++)
            if(strr==sz1[i])
               size=Integer.parseInt(sz1[i]);
      }
  }
 class WinLs extends WindowAdapter
 {
 	public void windowsClosing(WindowEvent e)
 	{System.exit(0);}
 }
 
 public void edition()
 {  
 	JLabel label1=new JLabel("版权所有:计算机08班43号赵乐涛",JLabel.CENTER);
 	JButton bt1=new JButton("确定");
 	Box ebox=Box.createVerticalBox();
    
 	cq.add(ebox);
 	Box ebox1=Box.createHorizontalBox();
 	ebox1.add(label1);
 	ebox.add(ebox1);
 	
 	Box ebox2=Box.createHorizontalBox();
 	bt1.addActionListener(this);
    ebox2.add(bt1);
 	ebox.add(ebox2);
 
 	edit.setBounds(0,0,300,210);
    edit.setResizable(false);
    edit.show();
 }
 public static void main(String[] args)
 {
 	JFrame f=new document();
 	f.setSize(600,400);
 	f.setVisible(true);
 }	 
}

⌨️ 快捷键说明

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