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

📄 note.java

📁 实现记事本的新建,保存,复制,粘贴等基本功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     
     BufferedWriter bw = new BufferedWriter( new FileWriter (f));//输入到文件中
     bw.write(s , 0 , s.length());
     bw.close();
     
    }
    catch(FileNotFoundException fe_){
     System.out.println("file not found");
     System.exit(0);
    }
    catch( IOException ie_)
    {
     System.out.println(" IO error");
     System.exit(0);
    }
     
    }   
         
   }
  });  
    
  
  //另存为
  miSaveAs.addActionListener( new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    FileDialog d=new FileDialog(mainFrame , "save file" , FileDialog.SAVE );//保存文件对话框   
    
    d.addWindowListener( new WindowAdapter(){ //关闭文件对话框窗口
     public void windowClosing(WindowEvent ee){
      System.exit(0);
     }
    });
    d.setVisible(true);
    
    String s = ta.getText();//得到所输入的文本内容
    
    try//异常处理
    {
     File f = new File( d.getDirectory()+d.getFile());//新建文件
     
     BufferedWriter bw = new BufferedWriter( new FileWriter (f));//输入到文件中
     bw.write(s , 0 , s.length());
     bw.close();
     
    }
    catch(FileNotFoundException fe_){
     System.out.println("file not found");
     System.exit(0);
    }
    catch( IOException ie_)
    {
     System.out.println(" IO error");
     System.exit(0);
    }     
   }
  });
  
  
  //退出
  miExit.addActionListener( new ActionListener(){ ///退出程序
   public void actionPerformed(ActionEvent e){
    System.exit(0);
   }
  });
  
  ////////////////"编辑"菜单:////////////////////
  
  //剪切
  miCut.addActionListener( new ActionListener(){
   public void actionPerformed(ActionEvent e){  // actionPerformed(ActionEvent e):发生操作时调用
      
    tempString = ta.getSelectedText(); ///得到要复制的内容,暂存在tempString中
    StringBuffer  tmp = new StringBuffer ( ta.getText());//临时存储文本   
    int start = ta.getSelectionStart();  //得到要删除的字符串的起始位置
    int len = ta.getSelectedText().length(); //得到要删除的字符串的长度
    tmp.delete( start , start+len); ///删除所选中的字符串
    ta.setText(tmp.toString());//用新文本设置原文本    
   }
     });                        
  
  //复制
  miCopy.addActionListener( new ActionListener(){
   public void actionPerformed(ActionEvent e){
    tempString = ta.getSelectedText(); ///得到要复制的内容,暂存在tempString中
   }
  });
  
  //粘贴
  miPaste.addActionListener( new ActionListener(){
   public void actionPerformed(ActionEvent e){
    StringBuffer  tmp = new StringBuffer ( ta.getText());//临时存储文本   
    int start = ta.getSelectionStart();  //得到要粘贴的位置
    tmp.insert(start , tempString);//插入要粘贴的内容
    ta.setText(tmp.toString());//用新文本设置原文本 
   }
  });
  
  //删除
  miDelete.addActionListener( new ActionListener(){
   public void actionPerformed(ActionEvent e){
    StringBuffer  tmp = new StringBuffer ( ta.getText());//临时存储文本   
    int start = ta.getSelectionStart();  //得到要删除的字符串的起始位置
    int len = ta.getSelectedText().length(); //得到要删除的字符串的长度
    tmp.delete( start , start+len); ///删除所选中的字符串
    ta.setText(tmp.toString());//用新文本设置原文本 
   }
  });
  

  //全选 
   miSelectAll.addActionListener( new ActionListener(){
   public void actionPerformed (ActionEvent e){	
    ta.select( 0 , ta.getText().length()) ;
  }
  });

 


  ////////////////"格式"菜单:////////////////////
  
  //字体风格
   miFont.addActionListener( new ActionListener(){
   public void actionPerformed(ActionEvent e){
    final Dialog d = new Dialog ( mainFrame , "字形");//新建对话框
    d.setLocation( 250 ,200);// 起始位置
    
    
    d.setLayout( new BorderLayout());//表格布局
    
   
    List font_list = new List (6 , false);//字体列表    
    
    //////////////////////////上部分面板
    
    //添加字体项目
    font_list.add("常规");///普通字体
    font_list.add("粗体"); ///粗体
    font_list.add("斜体 ");//斜体    
    font_list.add("粗斜体 ");//粗斜体 
    
    font_list.addItemListener( new MyItemListener_font() ); //字体增加监视器
    
     
    
    Panel p_1 = new Panel();
    p_1.add(font_list);     
    p_1.setVisible(true);   
    
    
   //////////////////////////下部分面板
    Button ok  = new Button ("OK");
    ok.addActionListener( new ActionListener(){
     public void actionPerformed(ActionEvent e){
      d.dispose();
     }
    });     
    ok.setSize( new Dimension (20 , 5) );
           
           Panel p_2 = new Panel();  //下部分面板
           p_2.add(ok);    
    p_2.setVisible(true);          
    
    
    //添加两个面板
    
    d.add(p_1 , BorderLayout.CENTER);
    d.add(p_2 , BorderLayout.SOUTH);    
    d.pack();                      // 调整此窗口的大小,以适合其子组件的首选大小和布局
    
    d.addWindowListener( new WindowAdapter(){  //关闭对话框窗口
     public void windowClosing(WindowEvent ee){      
      d.dispose();                    // 释放此图形的上下文并释放它所使用的所有系统资源
     }
    });
    
    d.setVisible(true);
          }
  });
  
  
    // 字体大小 
    
   miSize.addActionListener( new ActionListener(){
   public void actionPerformed(ActionEvent e){ 
  } 
  });
   
   	
     //颜色
     
  miColor.addActionListener( new ActionListener(){
   public void actionPerformed(ActionEvent e) {      //执行接收到的事件  
      JColorChooser jcolor=new JColorChooser();      //创建颜色选择容器
             Color bcolor=ta.getForeground();
             jcolor.setColor(bcolor);
             ta.setForeground(
             jcolor.showDialog(ta,"请选择字体颜色...",bcolor));
             return;
  }
  });
   
  ////////////////"帮助"菜单:////////////////////
  
  //关于记事本  
  miAboutNote.addActionListener( new ActionListener(){
   public void actionPerformed(ActionEvent e){
    final Dialog d = new Dialog ( mainFrame , "关于记事本.........");//新建对话框
    TextArea  t = new TextArea("欢迎使用记事本 ."+ "作者: " + "高锦龙" );//添加标签
    t.setSize( new Dimension ( 5 , 5));
    t.setEditable(false);
    d.setResizable(false);//不可调整大小
    d.add(t);       
    d.pack();            //布置容器,让它使用显示其内容所需的最小空间
    
    d.addWindowListener( new WindowAdapter(){ //关闭对话框窗口
     public void windowClosing(WindowEvent ee){      
      d.dispose();
     }
    });    
    d.setLocation( 300 ,250);// 起始位置
    d.setVisible(true);    
   }
  });
  
 }
 
  class MyItemListener_font implements ItemListener { //字体监听器
  public void itemStateChanged(ItemEvent e) {
   id_font = ((java.awt.List)e.getSource()).getSelectedIndex();
   switch( id_font){
    case  0:{
     ta.setFont(new Font("Times New Roman", Font.PLAIN ,ta.getFont().getSize()) );//普通文字
     break;
    }
    case  1:{
     ta.setFont(new Font("Times New Roman" , Font.BOLD ,ta.getFont().getSize()) );//粗体文字
     break;
    }
    case  2:{
     ta.setFont(new Font("Times New Roman" , Font.ITALIC ,ta.getFont().getSize()) );//斜体文字
     break;
      }
     case  3:{
     ta.setFont(new Font("Times New Roman", Font.BOLD+Font.ITALIC,ta.getFont().getSize()) );//粗斜体文字 
     break;
    }
     
          
   }
  }
 }
 

 /////////////////////////////////////////主函数///////////////////////////////////////////////////
 public static void main(String arg[]){
  Note test = new Note(); ///创建记事本
 }
//////////////////////////////////////////////////////////////////////////////////////////////////       

}





⌨️ 快捷键说明

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