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

📄 anewediter.java

📁 带有字体设置和前景设置的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class Anewediter implements ActionListener
{JFrame f=new JFrame("文本编辑器");
 JTextArea tf=new JTextArea();
// tf.addDocumentListener(this);
 JScrollPane jsp=new JScrollPane(tf);
 JMenuBar bar=new JMenuBar();
 JMenu menu1=new JMenu("文件(F)");
 JMenu menu2=new JMenu("编辑(E)");
 JMenu menu3=new JMenu("格式(O)");
 JMenuItem newf=new JMenuItem("  新建");
 JMenuItem openf=new JMenuItem("  打开");
 JMenuItem savef=new JMenuItem("  保存");
 JMenuItem saveasf=new JMenuItem("  另存为");
 JMenuItem quitf=new JMenuItem("  退出");
 JMenuItem cancel=new JMenuItem("  撤消");
 JMenuItem copy=new JMenuItem("  复制");
 JMenuItem cut=new JMenuItem("  剪切");
 JMenuItem paste=new JMenuItem("  粘贴");
 JMenuItem selectall=new JMenuItem("  全选");
 JMenuItem del=new JMenuItem("  删除");
 JMenuItem gesi=new JMenuItem("    字体");
 JMenuItem forecolor=new JMenuItem("    前景色");
 //JPopupMenu pmenu=new JPopupMenu();
 /*JMenuItem cancel1=new JMenuItem("  撤消");
 JMenuItem copy1=new JMenuItem("  复制");
 JMenuItem cut1=new JMenuItem("  剪切");
 JMenuItem paste1=new JMenuItem("  粘贴");
 JMenuItem del1=new JMenuItem("  删除");
 JMenuItem selall=new JMenuItem("  全选");*/
      //pmenu.add(new JMenuItem(""));
 File file;
 JDialog dialog;
  JPanel jp;
  JLabel lb1,lb2,lb3,lb4,lb5;
  JComboBox cb1,cb2,cb3;
  JButton btn1,btn2;
  //Button btn4;
  
  Container diacp,diacp2;
  //Point point;
  String cbxstr1,cbxstr2,cbxstr3,buffer,temptext;
  int cbxint2=0,cbxint3=10;int cace=1;
  boolean isopen=false,issaved=false,isnewf=true,iskey=false;
  int posi=0;String serp;
 public Anewediter()
 {
  f.getContentPane().add(jsp);
  //bar.setOpaque(true);
  f.setJMenuBar(bar);//jsp.setBackground(Color.white);
  f.setBackground(Color.white);
  tf.setBackground(Color.white);
  tf.setFont(new Font("宋体",Font.BOLD,25));
  //tf.addMouseListener(new MyMouseAdapter());
  //tf.addKeyListener(new MyKeyAdapter());
  menu1.add(newf);menu1.add(openf);menu1.add(savef);menu1.add(saveasf);menu1.addSeparator();menu1.add(quitf);
  menu2.add(cancel);menu2.addSeparator();menu2.add(cut);menu2.add(copy);menu2.add(paste);menu2.add(del);menu2.addSeparator();

  menu2.add(selectall);
 menu3.add(gesi);menu3.add(forecolor);
  bar.add(menu1);bar.add(menu2);bar.add(menu3);
  menu1.setMnemonic('F');
  menu2.setMnemonic('E');
  menu3.setMnemonic('O');
 newf.addActionListener(this);
  openf.addActionListener(this);
  savef.addActionListener(this);
  saveasf.addActionListener(this);
  quitf.addActionListener(this);
  cancel.addActionListener(this);
  cut.addActionListener(this);
  copy.addActionListener(this);
  paste.addActionListener(this);
  del.addActionListener(this);

  
  selectall.addActionListener(this);
 
  gesi.addActionListener(this);
  forecolor.addActionListener(this);
  tf.setLineWrap(true);
  /*pmenu.add(cancel1);pmenu.addSeparator();cancel1.addActionListener(this);
  pmenu.add(cut1);cut1.addActionListener(this);
  pmenu.add(copy1);copy1.addActionListener(this);
  pmenu.add(paste1);paste1.addActionListener(this);
  pmenu.add(del1);del1.addActionListener(this);
  pmenu.addSeparator();
  pmenu.add(selall);selall.addActionListener(this);*/
  int fnub=1;
  file=new File("新建 文本文档.txt");
  while(file.exists()==true)
    {file=new File("新建 文本文档("+fnub+").txt");
     fnub++;
    }  
  f.setSize(600,500);
  f.setLocation(250,200); 
  f.setVisible(true);
  f.addWindowListener(new WinLis());
  
 }
 

@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
 {if(e.getSource()==newf)
       {int ms;
        //if(isnewf==true&&)
        if(issaved==false&&tf.getText().length()!=0)
          {ms=JOptionPane.showConfirmDialog(f,"文件"+file.getAbsolutePath()+"\n已经改变,想保存文件吗?","记事本",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);                  
           if(ms==0)
           {if(isnewf==true&&tf.getText().length()!=0)
           {FileDialog fdag=new FileDialog(f,"保存",1);
   	        fdag.setSize(300,250);
   	        fdag.setVisible(true);
   	        if(fdag.getFile()!=null)
   	        {
   	         String st=fdag.getDirectory()+fdag.getFile();
             try
             { file=new File(st);
               FileOutputStream fout=new FileOutputStream(file);
               DataOutputStream out=new DataOutputStream(fout);
               out.writeBytes(tf.getText());//tf.setText("");f.setTitle("无标题 - 记事本");
               JOptionPane.showMessageDialog(f,"文件已成功保存在:\n"+file.getAbsolutePath(),"保存信息",JOptionPane.PLAIN_MESSAGE);
               //f.setTitle(file.getName().substring(0,file.getName().indexOf('.'))+" - 记事本");
               tf.setText("");
               f.setTitle("无标题 - 记事本");
               isnewf=false;issaved=true;
             }
             catch(Exception E)
             {System.out.println("I/O错误!");}
            }	
           }
   	       else
   	         {try
   	          {FileOutputStream fout=new FileOutputStream(file);
               DataOutputStream out=new DataOutputStream(fout);
               out.writeBytes(tf.getText());
               out.close();
               JOptionPane.showMessageDialog(f,"文件"+file.getAbsolutePath()+"\n已成功保存!","保存信息",JOptionPane.PLAIN_MESSAGE);
               tf.setText("");
               f.setTitle("无标题 - 记事本");
               isnewf=false;issaved=true;
              }
              catch(Exception E)
              {System.out.println("I/O错误!");}
             }
           }
           if(ms==1){tf.setText("");}
           
          }
          int fnub=1;
           file=new File("新建 文本文档.txt");
           while(file.exists()==true)
           {file=new File("新建 文本文档("+fnub+").txt");fnub++;
           }
           isnewf=true;issaved=false;
       }
  if(e.getSource()==openf)
   {
   	FileDialog fdag=new FileDialog(f,"打开");
   	fdag.setSize(300,250);
   	fdag.setVisible(true);
   	if(fdag.getFile()!=null)
   	{String st=fdag.getDirectory()+fdag.getFile();
   	file=new File(st);
   	try
     {FileInputStream fin=new FileInputStream(file);
     // DataInputStream in=new DataInputStream(fin);
      int len=(int)file.length();
      byte bt[]=new byte[len];
      fin.read(bt,0,len);
       st=new String(bt);
       //st=in.readUTF();
      tf.setText(st);
      f.setTitle(file.getName().substring(0,file.getName().indexOf('.'))+" - 记事本");
      isnewf=false;issaved=true;
     }
     catch(Exception E)
     {System.out.println("I/O错误!");}
     }
     
    } 
   
  if(e.getSource()==savef)
   {if(isnewf==true)
     {FileDialog fdag=new FileDialog(f,"保存",1);
   	  fdag.setSize(300,250);
   	  fdag.setVisible(true);
   	  if(fdag.getFile()!=null)
   	  {
   	   String st=fdag.getDirectory()+fdag.getFile();
       try
        { file=new File(st);
          FileOutputStream fout=new FileOutputStream(file);
          DataOutputStream out=new DataOutputStream(fout);
          out.writeBytes(tf.getText());
          JOptionPane.showMessageDialog(f,"文件已成功保存在:\n"+file.getAbsolutePath(),"保存信息",JOptionPane.PLAIN_MESSAGE);
          f.setTitle(file.getName().substring(0,file.getName().indexOf('.'))+" - 记事本");
         isnewf=false;issaved=true;
        }
       catch(Exception E)
        {System.out.println("I/O错误!");
        }
       }	
     }
   	 else
   	 {try
   	  {FileOutputStream fout=new FileOutputStream(file);
       DataOutputStream out=new DataOutputStream(fout);
      out.writeBytes(tf.getText());
      out.close();
       JOptionPane.showMessageDialog(f,"文件"+file.getAbsolutePath()+"\n已成功保存!","保存信息",JOptionPane.PLAIN_MESSAGE);
      isnewf=false;issaved=true;
      }
      catch(Exception E)
      {System.out.println("I/O错误!");}
     }
      
   }
  if(e.getSource()==saveasf)
     {FileDialog fdag=new FileDialog(f,"另存为",1);
   	  fdag.setSize(300,250);
   	  fdag.setVisible(true);

⌨️ 快捷键说明

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