//编写一个文件内容替换程序。要求:
//(1)允许用户选择目录;
//(2)选择是否包含子目录;
//(3)输入要修改的文件扩展名、要查找的字符串、替换后的字符串;
//(4)将用户选择范围的所有文件打开并替换;
//我实现了部分代码,其他的没有实现
//源代码:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileDialogDemo extends Frame implements MouseListener, WindowListener
{
Button b;
TextArea ta;
FileDialog fd;
TextField tf1,tf2;
public static void main(String args[])
{
FileDialogDemo fdm=new FileDialogDemo();
fdm.go();
}
class SearchFiles
{
void search()
{
File M = new File(tf1.getText());
File[] F = M.listFiles();
int i;
String name;
String name1=(tf2.getText());
for(i=0;i<F.length;i++)
{
name=F[i].getName();
if(name.equals(name1))
System.out.println("zhaodao");
else
System.out.println("meizhaodao");
}
}
}
public void mouseClicked(MouseEvent e)
{
//下面这个语句用于获得事件源按钮
Button myBTN = (Button) (e.getSource());
//下面的if语句处理事件源是“查找”按钮或“替换”按钮时的情况
if (myBTN.getLabel() == "搜 索" )
{
/* String型变量myString1和myString2获得文本区域和第一个文本框中的文字内容 */
//String myString1 = myTextArea.getText();
SearchFiles myNewFile=new SearchFiles();
myNewFile.search();
}
}
public void go ()
{
b=new Button("搜 索");
b.setBounds(50,130,100,30);
add(b);
b.addMouseListener(this);
TextArea ta=new TextArea();
ta.setBounds(250,50,200,120);
add(ta);
Label l1=new Label("搜索路径:");
l1.setBounds(50,50,65,20);
add(l1);
TextField tf1=new TextField(50);
tf1.setBounds(120,50,90,25);
add(tf1);
TextField tf2=new TextField(50);
tf2.setBounds(120,100,90,25);
add(tf2);
Label l2=new Label(" 扩展名 :");
//这里很奇怪,本来以为是队齐的,但并不对齐 只有在“扩展名:”前面狂加空格才实现对齐的
//不明白是什么原因
l2.setBounds(50,100,65,20);
add(l2);
addWindowListener(this);
setTitle("OpenFile");
setSize(500,200);
setVisible(true);
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
//重写windowClosing方法,关闭窗口时,程序退出
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
}