JAVA源文件记事本

package AWT; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import javax.swing.filechooser.FileFilter; 
public class NotePad extends JFrame 

int index =0;//记录查找的地址 
int[] indexs =new int[9999];     //存放查找的记录 
int id = 0;//数组的索引 
JMenu file = new JMenu("File"); 
JMenu edit = new JMenu("Edit"); 
JMenuItem open = new JMenuItem("Open"); 
JMenuItem save = new JMenuItem("Save"); 
JMenuItem exit = new JMenuItem("Exit"); 
JMenuItem find = new JMenuItem("Find"); 
JTextArea area = new JTextArea(); 
JMenuBar bar = new JMenuBar(); 
JScrollPane pane = new JScrollPane(area,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
public NotePad(String name)                     //构造器初始话 

super(name); 
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
file.add(open);file.add(save);file.add(exit); 
open.addActionListener(new OpenMonitor()); 
exit.addActionListener(new ExitMonitor()); 
save.addActionListener(new SaveMonitor()); 
find.addActionListener(new SearchMonitor()); 
edit.add(find); 
bar.add(file);   
bar.add(edit); 
this.setJMenuBar(bar);                      //添加MenuBar 
add(pane);                           //添加滚动区域不要添加TextArea 
this.setSize(700,700); 
area.setBackground(Color.BLACK); 
area.setForeground(Color.WHITE); 
//area.setFont(Font.ITALIC); 
this.setVisible(true); 

public static void main(String[] args) 

// TODO Auto-generated method stub 
new NotePad("记事本"); 

class OpenMonitor implements ActionListener    //读取数据 

public void actionPerformed(ActionEvent e) 

JFileChooser MyChooser = new JFileChooser();   
MyChooser.setFileSelectionMode( JFileChooser.FILES_ONLY);   //设置文件选择模式 
MyChooser.setMultiSelectionEnabled(false); 
MyChooser.removeChoosableFileFilter(MyChooser.getFileFilter());   
           MyFileFilter   filter   =  new MyFileFilter();//实例话自定义的对象一共3个 
           MyFileFilter2  filter2  =  new MyFileFilter2(); 
           MyFileFilter3  filter3  =  new MyFileFilter3(); 
           MyChooser.setFileFilter(filter);                    //在过滤中添加.txt过滤 
           MyChooser.setFileFilter(filter2); 
           MyChooser.setFileFilter(filter3);//在过滤中添加.java文件过滤 
           MyChooser.showOpenDialog(NotePad.this); 
File file = MyChooser.getSelectedFile(); 
String name =  file.toString();           //路径的名称 
System.out.println(name); 
FileReader fr = null; 
try 

area.setText(""); 
fr = new FileReader(name); 
int data = fr.read(); 
while(data!=-1) 

area.append((char)data+""); 
data = fr.read(); 


catch(Exception a) 

a.printStackTrace(); 

finally 

if(fr!=null) 

try 

fr.close(); 

catch (IOException e1) 

// TODO Auto-generated catch block 
e1.printStackTrace(); 





class MyFileFilter extends FileFilter   //继承过滤类 

public boolean accept(File f)   //重写Accpt方法过滤.txt文件 
       {   
             if(f.isDirectory()||f.getName().endsWith(".txt")) 
             {   
                 return true;   
             }   
             else   
             {   
                 return false;   
             }   
       }       
       public String getDescription()  //重写getDescription方法 
       {   
             return ".txt";   
       }   
   } 
class MyFileFilter3 extends FileFilter   //继承过滤类 

public boolean accept(File f)   //重写Accpt方法不过滤文件 
       {   
             if(f.isDirectory()||f.getName().endsWith("")) 
             {   
                 return true;   
             }   
             else   
             {   
                 return false;   
             }   
       }   
       public String getDescription()  //重写getDescription方法 
       {   
             return "all Files";   
       }   
   } 
class MyFileFilter2 extends FileFilter   //继承过滤类过滤.java文件 
{   
       public boolean accept(File f)   //重写Accpt方法 
       {   
             if(f.isDirectory()|| f.getName().endsWith(".java")) 
             {   
                   return true;   
             }   
             else   
             {   
                   return false;   
             }   
       }   
   
       public String getDescription()  //重写getDescription方法 
       {   
             return ".java";   
       }   
    } 
class ExitMonitor implements ActionListener        //退出 

public void actionPerformed(ActionEvent e) 

System.exit(-1); 


class SaveMonitor implements ActionListener   //保存数据 

public void actionPerformed(ActionEvent e) { 
JFileChooser chooser = new JFileChooser(); 
chooser.showOpenDialog(NotePad.this); 
File file = chooser.getSelectedFile(); 
String name =  file.toString();           //路径的名称 
System.out.println(name); 
FileWriter fw = null; 
try 

fw = new FileWriter(name); 
String data2 = area.getText(); 
byte[]  num  = data2.getBytes(); 
for(int i = 0 ;i<num.length;i++) 

fw.write((char)num ); 

System.out.println("保存完毕"); 

catch(Exception a) 

a.printStackTrace(); 

finally 

if(fw!=null) 

try 

fw.close(); 

catch (IOException e1) 

e1.printStackTrace(); 






class SearchMonitor implements ActionListener  //查找数据           

int index = 0; 
boolean go = false; 
boolean UP = true; 
JTextField jf; 
JCheckBox jb; 
JRadioButton radio1; 
JRadioButton radio2; 
ButtonGroup bg; 
public void actionPerformed(ActionEvent e) 

JDialog dialog = new JDialog(NotePad.this,"查找"); 
dialog.setSize(400,100); 
dialog.setVisible(true); 
dialog.setLocation(100,100); 
dialog.setLayout(new FlowLayout()); 
JLabel l = new JLabel("请输入查找的关键字:"); 
jf = new JTextField(20); 
jb = new JCheckBox("不区分大小写"); 
radio1 = new JRadioButton("向上查找"); 
radio2 = new JRadioButton("向下查找"); 
radio2.doClick(); 
bg = new ButtonGroup(); 
jb.addItemListener(new DxWriter()); 
JButton one = ne* **utton("开始"); 
dialog.add(l); 
dialog.add(jf); 
dialog.add(one); 
dialog.add(jb); 
bg.add(radio1); 
bg.add(radio2); 
dialog.add(radio1); 
dialog.add(radio2); 
one.addActionListener(new StringMonitor()); 
radio1.addActionListener(new Upserach()); 
radio2.addActionListener(new Downserach()); 
  } 
class StringMonitor implements ActionListener         //查找 

public void actionPerformed(ActionEvent e) { 
if(!go)                                    //向下查找 

if(UP)                               //不区分大小写 

String zifu = jf.getText(); 
String text = area.getText(); 
int d = text.indexOf(zifu,index); 
if(d!=-1) 

area.select(d,d+zifu.length()); 
index = d+zifu.length()+1; 
indexs[id] = d; 
id++; 

else 
System.out.println("没找到"); 

else                                 //区分大小写 
   { 
String zifu = jf.getText().toUpperCase(); 
String text = area.getText().toUpperCase(); 
int d = text.indexOf(zifu,index); 
if(d!=-1) 

area.select(d,d+zifu.length()); 
index = d+zifu.length()+1; 
indexs[id] = d; 
id++; 

else 
System.out.println("没找到"); 


else                                            //向上查找 

if(UP)                                     //不区分大小写 

String zifu = jf.getText(); 
String text = area.getText(); 
if((id-1)<0) 

//JOptionPane.showMessageDialog(null,"Hello,China!"); 
System.out.println("最前面了"); 

else 

int d = text.indexOf(zifu,indexs[id-2]); 
id--; 
if(d!=-1) 

area.select(d,d+zifu.length()); 

else 
System.out.println("没找到"); 


else                                            //区分大小写 

String zifu = jf.getText().toUpperCase(); 
String text = area.getText().toUpperCase(); 
if((id-1)<0) 
System.out.println("最前面了"); 
else 

int d = text.indexOf(zifu,indexs[id-2]); 
id--; 
if(d!=-1) 

area.select(d,d+zifu.length()); 
index = d+zifu.length()+1; 

else 
System.out.println("没找到"); 





class DxWriter implements ItemListener       //区分大小写 

public void itemStateChanged(ItemEvent e) 

if(e.getSource()==jb) 

if(e.getStateChange()==1) 
UP = false; 
else 
UP = true; 



class Upserach implements ActionListener           //向上查找 

public void actionPerformed(ActionEvent e) 

go = true; 


class Downserach implements ActionListener           //向上查找 

public void actionPerformed(ActionEvent e) 

go = false; 



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值