我正在尝试创建一个用户注册的程序,并使用简单的文本输出将其信息输出到文件中?
这是我的全班..
package malkawi.login;
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.*;
import javax.swing.*;
import malkawi.login.JTextFieldLimit;
/**
*
* @author Defiledx1
* sign up
*/
public class SignUp extends JFrame implements EventListener {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton complete = new JButton("Next");
JLabel fname = new JLabel("Name: ");
JLabel Mname = new JLabel("Middle Name: ");
JLabel Lname = new JLabel("Last Name: ");
JLabel user = new JLabel("Username: ");
JLabel pass = new JLabel("Password: ");
JLabel info = new JLabel("Click Next to Continue");
JLabel email = new JLabel("Email: ");
JLabel scode = new JLabel("Secret Code: ");
JTextField fname1 = new JTextField();
JTextField Mname1 = new JTextField();
JTextField Lname1 = new JTextField();
JTextField user1 = new JTextField();
JPasswordField pass1 = new JPasswordField();
JTextField email1 = new JTextField();
JTextField scode1 = new JTextField();
JRadioButton showPass = new JRadioButton("Show Pass");
public SignUp() {
super("Sign Up - Flare By Malkawi");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 400);
setResizable(false);
setVisible(true);
setVisible(true);
setLayout(new GridLayout(0, 2, 10, 10));
setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
/*
* Limitations
*/
fname1.setDocument(new JTextFieldLimit(10));
Mname1.setDocument(new JTextFieldLimit(1));
Lname1.setDocument(new JTextFieldLimit(10));
user1.setDocument(new JTextFieldLimit(15));
email1.setDocument(new JTextFieldLimit(80));
scode1.setDocument(new JTextFieldLimit(5));
/*
* End Of Limitations
*/
/*
* RadioButton Checked : Unchecked
*/
showPass.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
showPassword(e.getStateChange() == 1 ? true : false);
}
});
/*
* End of RadioButton Checked : UnChecked
*/
/*
* Action of registration
*/
complete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
try {
outPutInformation();
} catch (FileNotFoundException | UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("Flare is unable at the moment!");
}
}
});
/*
* End of Action of registration
*/
// Dimension labelSize = info.getPreferredSize();
/*
* Start of placements
*/
//add(info);
add(fname);
add(fname1);
add(Mname);
add(Mname1);
add(Lname);
add(Lname1);
add(user);
add(user1);
add(pass);
add(pass1);
add(email);
add(email1);
add(scode);
add(scode1);
add(complete);
add(showPass);
add(info);
pack();
}
public void showPassword(boolean showP) {
if (showP == true) {
pass1.setEchoChar((char)0);
} else {
pass1.setEchoChar('*');
}
}
/*
* File Output Requirements
*/
String filename = user1.getText();
String firstname = fname1.getText();
String middlename = Mname1.getText();
String lastname = Lname1.getText();
String username = user1.getText();
@SuppressWarnings("deprecation")
String password = pass1.getText();
String hotmail = email1.getText();
String secretcode = scode1.getText();
/*
* File Output done
*/
public void outPutInformation() throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter writer = new PrintWriter(filename+".txt", "UTF-8");
writer.println(firstname);
writer.println(middlename);
writer.println(lastname);
writer.println(username);
writer.println(password);
writer.println(hotmail);
writer.println(secretcode);
writer.close();
}
}
问题是它没有输出任何东西。 如何输出像
后面的2文件夹这样的文件谢谢!
答案 0 :(得分:1)
这不输出任何内容,因为在输入文本字段中的任何内容之前已初始化变量。您需要这样做,或者直接将文本字段值写入文件,而不是先保存到变量然后写入文件:
public void outPutInformation() throws FileNotFoundException, UnsupportedEncodingException {
String filename = user1.getText();
String firstname = fname1.getText();
String middlename = Mname1.getText();
String lastname = Lname1.getText();
String username = user1.getText();
@SuppressWarnings("deprecation")
String password = pass1.getText();
String hotmail = email1.getText();
String secretcode = scode1.getText();
PrintWriter writer = new PrintWriter(filename+".txt", "UTF-8");
writer.println(firstname);
writer.println(middlename);
writer.println(lastname);
writer.println(username);
writer.println(password);
writer.println(hotmail);
writer.println(secretcode);
writer.close();
}