我刚学java,写了一个用户名密码登陆,用户名密码都是文本框,要把写完的结果保存到一个文本文件中,如何实现呢?请教大家,下面是我写的代码,麻烦大家给帮帮忙。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FileNotFoundException;
public class login extends Frame { TextField textField1 = new TextField(10);
Label label1 = new Label();
TextField textField2 = new TextField(10);
TextField textField3 = new TextField(12);
Label label2 = new Label();
TextField textField4 = new TextField(12); public login() {
try {
jbInit(); }
catch(Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
login login = new login();
login.setSize(300,400);
login.setVisible(true); }
private void jbInit() throws Exception {
textField1.setText("用户名");
textField1.setBounds(30, 30, 50, 30);
textField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(KeyEvent e) {
textField1_keyTyped(e);
}
}); label1.setText("用户名为:");
label1.setBounds(100, 30, 60, 30);
textField2.setText("");
textField2.setBounds(170,30,60,30);
textField3.setText("密码");
textField3.setBounds(30,70,50,30);
textField3.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(KeyEvent e) {
textField3_keyTyped(e);
}
});
this.setLayout(null); label2.setText("密码为:");
label2.setBounds(100,70,60,30);
textField4.setText("");
textField4.setBounds(170,70,60,30); this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
this_windowClosing(e);
}
});
this.add(textField2);
this.add(textField3);
this.add(label2);
this.add(textField4);
this.add(textField1);
this.add(label1);
} void textField3_textValueChanged(TextEvent e) { if(e.getSource()!=null)
{
textField3.setEchoChar('*');
}
} void this_windowClosing(WindowEvent e) {
System.exit(0);
}
void textField1_keyTyped(KeyEvent e) {
if(e.getKeyChar()== KeyEvent.VK_ENTER)
{
textField3.requestFocus();
}
} void textField3_keyTyped(KeyEvent e) {
if(e.getKeyChar()== KeyEvent.VK_ENTER)
{
textField2.setText(textField1.getText());
textField4.setText(textField3.getText());
}
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FileNotFoundException;
public class login extends Frame { TextField textField1 = new TextField(10);
Label label1 = new Label();
TextField textField2 = new TextField(10);
TextField textField3 = new TextField(12);
Label label2 = new Label();
TextField textField4 = new TextField(12); public login() {
try {
jbInit(); }
catch(Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
login login = new login();
login.setSize(300,400);
login.setVisible(true); }
private void jbInit() throws Exception {
textField1.setText("用户名");
textField1.setBounds(30, 30, 50, 30);
textField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(KeyEvent e) {
textField1_keyTyped(e);
}
}); label1.setText("用户名为:");
label1.setBounds(100, 30, 60, 30);
textField2.setText("");
textField2.setBounds(170,30,60,30);
textField3.setText("密码");
textField3.setBounds(30,70,50,30);
textField3.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(KeyEvent e) {
textField3_keyTyped(e);
}
});
this.setLayout(null); label2.setText("密码为:");
label2.setBounds(100,70,60,30);
textField4.setText("");
textField4.setBounds(170,70,60,30); this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
this_windowClosing(e);
}
});
this.add(textField2);
this.add(textField3);
this.add(label2);
this.add(textField4);
this.add(textField1);
this.add(label1);
} void textField3_textValueChanged(TextEvent e) { if(e.getSource()!=null)
{
textField3.setEchoChar('*');
}
} void this_windowClosing(WindowEvent e) {
System.exit(0);
}
void textField1_keyTyped(KeyEvent e) {
if(e.getKeyChar()== KeyEvent.VK_ENTER)
{
textField3.requestFocus();
}
} void textField3_keyTyped(KeyEvent e) {
if(e.getKeyChar()== KeyEvent.VK_ENTER)
{
textField2.setText(textField1.getText());
textField4.setText(textField3.getText());
}
}
}
String passwd = 密码TextField.getText();
然后用OutputStream
write()进一个txt文本就行了
public void saveToFile(String username,String password)
{
PrintWriter pw = new PrintWriter (new FileOutputStream("c:\\account.txt"));
pw.println(username);
pw.println(password);
pw.close();
}