//编写一个文件内容替换程序。要求:
//(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)
{
}
}
----------------解决方案--------------------------------------------------------
哪有像你这样写程序的,
你应该为你的按钮添加ActionListener
还有,你不要再用老的东西了,用新的javax.swing
----------------解决方案--------------------------------------------------------
谢谢版主回复,但我还是不很了解,能不能详细一些?
我用b.addMouseListener(this)对按钮进行监听;
这里是动作
public void mouseClicked(MouseEvent e)
{
Button myBTN = (Button) (e.getSource());
if (myBTN.getLabel() == "搜 索" )
{
SearchFiles myNewFile=new SearchFiles();
myNewFile.search();
}
}
小弟对java不熟悉,请版主多帮帮我啊!
我用的是j2sdk1.4.2
----------------解决方案--------------------------------------------------------
我是基本上看不懂..你在写什么的..
下载个JDK5.0吧.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class FileDialogDemo extends JFrame implements ActionListener{
private JButton jbSearch;
private JTextArea jtaFileList;
private JTextField jtfPath,jtfEndsChar;
public FileDialogDemo(){
Container container=this.getContentPane();
JPanel panel=new JPanel();
jbSearch=new JButton("搜 索");
jbSearch.addActionListener(this);
jtaFileList=new JTextArea();
JLabel l1=new JLabel("搜索路径:");
jtfPath=new JTextField(20);
jtfEndsChar=new JTextField(16);
JLabel l2=new JLabel("扩展名:");
panel.add(l1);
panel.add(jtfPath);
panel.add(l2);
panel.add(jtfEndsChar);
panel.add(jbSearch);
container.add(panel,BorderLayout.NORTH);
container.add(new JScrollPane(jtaFileList,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS),BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("OpenFile");
setSize(700,200);
setVisible(true);
}
void search(){
File file = new File(jtfPath.getText());
File[] fileList = file.listFiles();
int i;
String fullName;
String endsName=jtfEndsChar.getText();
StringBuffer sb=new StringBuffer();
for(i=0;i<fileList.length;i++){
fullName=fileList[i].getName();
if(fullName.toUpperCase().endsWith(endsName.toUpperCase()))
sb.append(fullName+"\n");
}
jtaFileList.setText(sb.toString());
}
public static void main(String args[]){
new FileDialogDemo();
}
public void actionPerformed(ActionEvent e){
if (e.getSource()==jbSearch){
search();
}
}
}
----------------解决方案--------------------------------------------------------
还有,比较字符串是否相等,应该用eqals,而不应该用==
myBTN.getLabel() == "搜 索" )
----------------解决方案--------------------------------------------------------
楼上打错了..
是equals
----------------解决方案--------------------------------------------------------
这也被你发现了,你真细心
----------------解决方案--------------------------------------------------------
呵呵.
----------------解决方案--------------------------------------------------------
谢谢两位我用jdk5.0试试
----------------解决方案--------------------------------------------------------
我也看出来了
----------------解决方案--------------------------------------------------------