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

📄 f1.java

📁 一个简单的文本编辑器(Java实现),有操作界面,对大家可能会有点用处
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class f1 extends Frame implements ActionListener
{
      private MenuBar menubar=new MenuBar();
      private Menu filemenu=new Menu("文件");
      private Menu editmenu=new Menu("编辑");
      private Menu formmenu=new Menu("格式");
      private MenuItem[] itemf=new MenuItem[4];
      private MenuItem[] iteme=new MenuItem[6];
      private MenuItem[] items=new MenuItem[2];
      private TextArea tf=new TextArea();
      
      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);
      Container cp=dialog.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 jb1=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);
      JScrollBar jb2=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);
      JScrollBar jb3=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);

     
      public f1()
      {
           super("文本编辑器");
           itemf[0]=new MenuItem("新建");
           itemf[1]=new MenuItem("打开");
           itemf[2]=new MenuItem("保存");          
           itemf[3]=new MenuItem("退出");

           for(int i=0;i<4;i++)
                 itemf[i].addActionListener(this);
           for(int i=0;i<2;i++)
                 filemenu.add(itemf[i]);
           filemenu.addSeparator();           
           filemenu.add(itemf[2]);
           filemenu.addSeparator();
           filemenu.add(itemf[3]);
           
           iteme[0]=new MenuItem("撤销");
           iteme[1]=new MenuItem("复制");
           iteme[2]=new MenuItem("剪切");
           iteme[3]=new MenuItem("粘贴");
           iteme[4]=new MenuItem("删除");
           iteme[5]=new MenuItem("全选");
       
           for(int i=0;i<6;i++)
                 iteme[i].addActionListener(this);
           for(int i=0;i<1;i++)
                 editmenu.add(iteme[i]);
           editmenu.addSeparator();
           for(int i=1;i<5;i++)
                 editmenu.add(iteme[i]);
           editmenu.addSeparator();
           editmenu.add(iteme[5]);

           items[0]=new MenuItem("自动换行");
           items[0].addActionListener(this);
           items[1]=new MenuItem("字体...");
           items[1].addActionListener(this);
           for(int i=0;i<2;i++)
               formmenu.add(items[i]);
           
           menubar.add(filemenu);
           menubar.add(editmenu);
           menubar.add(formmenu);
           setMenuBar(menubar);
           add(tf);
           addWindowListener(new win());
           setSize(600,600);
           show();
      }
      class win extends WindowAdapter       
      {
           public void windowClosing(WindowEvent e)
           {
                 System.exit(0);
           }
      }
      public void actionPerformed(ActionEvent e)
      {   
           if(e.getSource()==b1)
           {
              tf.setForeground(new Color(a,b,c));
              Font font=new Font(s2,style,size);
              tf.setFont(font); 
              dialog.dispose();
              tf.append(s2);
           } 
           if(e.getSource()==b2)
              dialog.dispose();                
           if(e.getSource()==itemf[3])
              System.exit(0);
           if(e.getSource()==itemf[0])
              tf.setText("");
           if(e.getSource()==itemf[1]) 
           {
              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[1024];
                     tf.setText("");                     
                     int length;
                     while((length=fr.read(buffer))!=-1)
                     {
                          String str=String.valueOf(buffer);
                          tf.append(str);
                     }
                  }
                  catch(IOException ex)
                  {
                     tf.append("IOException");
                  }
              }
           } 
           if(e.getSource()==itemf[2])
           {
              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=tf.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)
                  {
                       tf.setText("IOException");
                  }
              }
           }
           
           if(e.getSource()==items[1]) 
           {
              jdialog();
           }
              
      }
      public void jdialog()
      {
            
            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]);
            jb1.addAdjustmentListener(new adjust());
            box3.add(jb1);
            box.add(box3);
           
            Box box4=Box.createVerticalBox();
            box4.add(lb[4]);
            jb2.addAdjustmentListener(new adjust());
            box4.add(jb2);
            box.add(box4);
              
            Box box5=Box.createVerticalBox();
            box5.add(lb[5]);
            jb3.addAdjustmentListener(new adjust());
            box5.add(jb3);
            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,250);
            dialog.setResizable(false);
            dialog.show();
            
      }    
      
      class adjust implements AdjustmentListener
      {
            public void adjustmentValueChanged(AdjustmentEvent ea)
            {
                 if((JScrollBar)ea.getSource()==jb1)
                     a=ea.getValue();
                 if((JScrollBar)ea.getSource()==jb2)
                     b=ea.getValue();
                 if((JScrollBar)ea.getSource()==jb3)
                     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]);
            }
      }
 
      public static void main(String args[])
      {
           f1 frame=new f1();
      }
}


          
             
      

⌨️ 快捷键说明

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