写入文件:
OutputStream os=new FileOutputStream("text.txt");
PrintWriter pw=new PrintWriter(os);
pw.println(textfield1.getText());
pw.println(textfield2.getText());
pw.close();
os.close();
读出:
DataInputStream dis=new DataInputStream(new FileInputStream("text.txt"));
String s="";
while((s=dis.readLine())!=null)
textarea.setText(textarea.getText()+"\r\n"+s);
OutputStream os=new FileOutputStream("text.txt");
PrintWriter pw=new PrintWriter(os);
pw.println(textfield1.getText());
pw.println(textfield2.getText());
pw.close();
os.close();
读出:
DataInputStream dis=new DataInputStream(new FileInputStream("text.txt"));
String s="";
while((s=dis.readLine())!=null)
textarea.setText(textarea.getText()+"\r\n"+s);
textfield.getText();
然后加上一个事件侦听器,当button按下的时候,就执行以上的方法。这是一个大概的过程。具体自己写啦。
JButton jb=(JButton)e.getSource();//得到是哪个jbutton
}
JButton jb1=new JButton("myButton1");
jb1.addActionListener(new ActionListener() {
public void ActionPerformed(ActionEvent e) {
JButton jb=(JButton)e.getSource();//得到激活本事件的jbutton
}
});