import java.io.*;
import javax.swing.*;
import java.awt.event.*;
public class StreamT extends JFrame implements ActionListener
{
JPanel p;
JTextField t;
JButton b;

public StreamT()
{
super("StreamT");
p = new JPanel();
t = new JTextField(10);
b = new JButton("Button");

this.getContentPane().add(p);
p.add(t);
p.add(b);
b.addActionListener(this);

show();
pack();
}

public static void main(String [] args)
{
new StreamT();
}

public void actionPerformed(ActionEvent e)
{
try
{
File f = new File("C:\\1.txt");
FileOutputStream fout = new FileOutputStream(f);


fout.write(this.t.getText().getBytes());

fout.close();
}
catch(Exception er)
{

}
}

}

解决方案 »

  1.   

    谢谢但是“b.addActionListener(this);”在jbuilder中编译不过去!为什么?谢谢!
      

  2.   


    用JBuilder编译通不过,那就这样吧
    你用JBuilder先做个界面,上面一输入框框和一个按扭,
    再替按钮加上事件
    再把
    try
    {
       File f = new File("C:\\1.txt");
       FileOutputStream fout = new FileOutputStream(f);

      fout.write(this.t.getText().getBytes());
      fout.close();
    }
    catch(Exception er)
    {}
    按到你的按钮事件里面;
    注意,输入框的名字要与代码中一致,我用的是t
    ---------------------------------------------------
    b.addActionListener(this);
    JBuilder自动生成的代码中,事件处理代码一般都是生成内部类或匿名类,而不用生成我写的这种形式,如果你是把我的这段代码拷贝进去再编译的话,应该不会通不过呀。
    我用的是JCreator