62,621
社区成员
发帖
与我相关
我的任务
分享
public class Test extends JFrame implements ActionListener {
JButton saveBu=new JButton("保存");
JTextField jt=new JTextField(20);
JPanel jp=new JPanel();
//构造函数
public Test(){
setTitle("测试");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
saveBu.addActionListener(this);
jp.add(jt);
jp.add(saveBu);
add(jp);
setSize(600, 200);
setLocation(100, 100);
setVisible(true);
}
//事件处理
public void actionPerformed(ActionEvent e) {
String str=jt.getText();//获取文本框中的字符串
File file=new File(str+".txt");
System.out.println(file.getAbsolutePath());
FileWriter fw=null;
try {
fw=new FileWriter(file);
fw.write(str);
} catch (IOException e1) {
e1.printStackTrace();
} finally{
try {
fw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
//程序入口
public static void main(String[] args){
new Test();
}
}