您的位置:首页 > 编程语言 > Java开发

java开发简单记事本

2010-11-21 17:38 183 查看
通过记事本的开发,主要是让像我这样的初学者熟悉java文件流。

这是自己的源代码:

import java.io.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;
public class Notepad extends JFrame {

 JTextArea jta=null;
 JMenuBar jmb=null;
 JMenu jm1=null;
 JMenu jm2=null;
 JMenuItem jm2_jmi1=null;
 JMenuItem jm2_jmi2=null;
 JMenuItem jm2_jmi3=null;
 JMenuItem jm1_jmi1=null;
 JMenuItem jm1_jmi2=null;
 JMenuItem jm1_jmiAdd=null;
 JMenuItem jm1_jmiClear=null;
 JMenuItem jm1_jmiQuit=null;
 JMenuItem jm1_jmiPrint=null;
 //添加一个JScrollPane
 JScrollPane jsp=null;
 JPanel jp=null;
 
 public Notepad(){
  jmb=new JMenuBar();
  jm1=new JMenu("文件(F)");
  jm1.setMnemonic('F');
  jm1_jmi1=new JMenuItem("打开");
  jm1_jmi1.setIcon(new ImageIcon("open.gif"));
  jm1_jmi1.addMouseListener(new OpenHandle());
  jm1_jmi2=new JMenuItem("保存");
  jm1_jmi2.addMouseListener(new SaveHandle());
  jm1_jmi2.setIcon(new ImageIcon("save.gif"));
  jm1_jmiAdd=new JMenuItem("新建",new ImageIcon("create.gif"));
  jm1_jmiAdd.addMouseListener(new AddNew());
  jm1_jmiClear=new JMenuItem("清空",new ImageIcon("delete.gif"));
  jm1_jmiClear.addMouseListener(new ClearAll());
  jm1_jmiPrint=new JMenuItem("打印");
  jm1_jmiQuit=new JMenuItem("退出");
  jm1_jmiQuit.addMouseListener(new QuitForm());
  jm1.add(jm1_jmi1);
  jm1.add(jm1_jmi2);
  jm1.add(jm1_jmiAdd);
  jm1.add(jm1_jmiClear);
  jm1.add(jm1_jmiPrint);
  jm1.add(jm1_jmiQuit);
  jmb.add(jm1);
  jm2=new JMenu("设置(S)");
  jm2.setMnemonic('S');
  jm2_jmi1=new JMenuItem("字体");
  jm2_jmi1.setIcon(new ImageIcon("fontsize.gif"));
  jm2_jmi1.addMouseListener(new FontSetting());
  jm2_jmi2=new JMenuItem("背景颜色");
  jm2_jmi2.setIcon(new ImageIcon("backcolor.jpg"));
  jm2_jmi2.addMouseListener(new BackColorHandle());
  jm2_jmi3=new JMenuItem("字体颜色");
  jm2_jmi3.setIcon(new ImageIcon("fontcolor.jpg"));
  jm2_jmi3.addMouseListener(new FontColorHandle());
  jm2.add(jm2_jmi1);
  jm2.add(jm2_jmi2);
  jm2.add(jm2_jmi3);
  jmb.add(jm2);
  this.setJMenuBar(jmb);
  
  //将JTextArea添加到JScrollPane中
  jta=new JTextArea();
  jp=new JPanel(new BorderLayout());
  jsp=new JScrollPane(jp);
  jp.add(jta);
  this.add(jsp,BorderLayout.CENTER);
  this.setSize(600, 400);
  //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.addWindowListener(new WindowClose());
  this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-600)/2,(Toolkit.getDefaultToolkit().getScreenSize().height-400)/2);
  this.setTitle("NotePad--usharei");
  this.setVisible(true);
 }
 //打开文件
 private class OpenHandle extends MouseAdapter{

  @Override
  public void mousePressed(MouseEvent e) {
   JFileChooser jfc=null;
   jfc=new JFileChooser();
   String str=null;
       //显示JAVA源文件
       jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
         public boolean accept(File f) { //设定可用的文件的后缀名
           if(f.getName().endsWith(".java")||f.isDirectory()){
             return true;
           }
           return false;
         }
         public String getDescription() {
           return "JAVA源程序(*.java)";
         }
       });
       jfc.setFileFilter(new javax.swing.filechooser.FileFilter(){
        public boolean accept(File f){
         if(f.getName().endsWith(".txt")||f.isDir
4000
ectory()){
          return true;
         }
         return false;
        }
        public String getDescription(){
         return "文本文件(*.txt)";
        }
       });

   if(jfc.showOpenDialog(Notepad.this)!=0){
    return ;
   }
   File f=jfc.getSelectedFile();
   setTitle(f.getName()+"--Notepad-usharei");
   str=f.getAbsolutePath();
   //打开txt文件
   if(f.getName().endsWith(".txt")||f.getName().endsWith(".java")){
    FileReader fr=null;
    BufferedReader br=null;
    try{
     fr=new FileReader(str);
     br=new BufferedReader(fr);
     String text="",s=null;
     while((s=br.readLine())!=null){
      text+=s+"/r/n";
     } 
     jta.setText(text);
     
    }catch(Exception ee){ee.printStackTrace();}finally{try {
     fr.close();br.close();
    } catch (IOException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }}
   }
   //打开二进制文件
   else{
    FileInputStream fis=null;
    
    try{
     fis=new FileInputStream(str);
     byte buf[]=new byte[1024];
     int n=0;
     String ss="";
     while((n=fis.read(buf))!=-1){
      ss+=new String(buf,0,n);
     }
     jta.setText(ss.trim());
    }catch(Exception ee){ee.printStackTrace();}finally{try {
     fis.close();
    } catch (IOException e1) {
     e1.printStackTrace();
    }}
   }
   
//System.out.println(str);
  }
  
 }
 //关闭文件
 private class SaveHandle extends MouseAdapter{

  public void mousePressed(MouseEvent e) {
   JFileChooser jfc=null;
   jfc=new JFileChooser();
   int i=jfc.showSaveDialog(Notepad.this);
   if(i!=0){
    return;
   }
   File f=jfc.getSelectedFile();
   String str=jfc.getSelectedFile().getAbsolutePath();
   setTitle(f.getName()+"--Notpad-usharei");
   FileOutputStream fos=null;
   byte buf[]=jta.getText().getBytes();
   try{
    fos=new FileOutputStream(str);
    fos.write(buf);
   }catch(Exception ee){ee.printStackTrace();}finally{try {
    fos.close();
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }}
  }
  
 }
 //字体的设置
 private class FontSetting extends MouseAdapter{

  public void mousePressed(MouseEvent e) {
   //JFontChooser jfc=new JFontChooser();
   //FontChooserDialog fcd=new FontChooserDialog();
   //Font f=fcd.showDialog(Notepad.this, true);
   Font ff=jta.getFont();
   Font f=FontChooserDialog.showDialog(Notepad.this, true,ff);
   //Font f=fcd.getFont();
   jta.setFont(f);
//System.out.println(f.getName()+f.getSize());
  }
  
 }
 //背景颜色的处理
 private class BackColorHandle extends MouseAdapter{
  public void mousePressed(MouseEvent e) {
   Color c=JColorChooser.showDialog(Notepad.this, "BackColorSetting", jta.getBackground());
   jta.setBackground(c);
  }
  
 }
 //字体颜色设置
 private class FontColorHandle extends MouseAdapter{

  public void mousePressed(MouseEvent e) {
   Color c=JColorChooser.showDialog(Notepad.this, "BackColorSetting", jta.getForeground());
   jta.setForeground(c);
  }
 }
 //打开新窗体
 private class AddNew extends MouseAdapter{
  public void mousePressed(MouseEvent e) {
   new Notepad();
  }
  
 }
 //清空所有文本内容
 private class ClearAll extends MouseAdapter{

  public void mousePressed(MouseEvent e) {
   jta.setText("");
  }
  
 }
 //退出处理
 private class QuitForm extends MouseAdapter{
  public void mousePressed(MouseEvent e) {
   System.exit(0);
  }
  
 }
 //窗体退出
 private class WindowClose extends WindowAdapter{
  public void windowClosing(WindowEvent e) {
   System.exit(0);
  }
  
 }
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new Notepad();
 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息