求解:用Java写一段程序,能实现注册用户的,包含用户名和密码,邮箱的GUI程序。

而且具有过滤用户的作用,把用户的资料写到文件中去。

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class TestLogin implements ActionListener {
JTextField jtf1 = new JTextField(15);
JPasswordField jtf2 = new JPasswordField(15);
JTextField jtf3 = new JTextField(15);
JLabel jl0 = new JLabel("欢迎注册!");
String result = "";

@Override
public void actionPerformed(ActionEvent e) {
String comn = e.getActionCommand();
if ("提交".equals(comn)) {
FileInputStream fis;
try {
fis = new FileInputStream("D:\\Program File\\image\\input.txt");
byte[] b = new byte[1024];
while (true) {
int num = fis.read(b);
if (num == -1)
break;
result = result + new String(b, 0, num);
}
fis.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
String[] s = result.split(";", 0);
String name = jtf1.getText();
String password = jtf2.getText();
String email = jtf3.getText();
String temp = name + "," + password + "," + email + ";";
boolean flag = true;
for (int i = 0; i < s.length; i++) {
String[] name0 = s[i].split(",", 0);
if (name.equals(name0[0])) {
jl0.setText("你输入的用户名重复啦!请重新输入");
flag = false;
jtf1.setText("");
jtf2.setText("");
jtf3.setText("");
jtf1.requestFocus();
break;
}
}
if (flag) {
try {
FileOutputStream fos = new FileOutputStream(
"D:\\Program File\\image\\input.txt", true);
fos.write(temp.getBytes());
fos.close();
jl0.setText("恭喜你!注册成功!");
jtf1.setText("");
jtf2.setText("");
jtf3.setText("");
jtf1.requestFocus();
} catch (Exception ae) {
ae.printStackTrace();
}
}
} else if ("清空".equals(comn)) {
jtf1.setText("");
jtf2.setText("");
jtf3.setText("");
jtf1.requestFocus();
}
}

public TestLogin() {
JFrame jf = new JFrame("登录界面");
GridLayout gl = new GridLayout(5, 1);
jf.setLayout(gl);
JPanel[] jp = new JPanel[5];
for (int i = 0; i < jp.length; i++) {
jp[i] = new JPanel();
jf.add(jp[i]);
}
jp[0].add(jl0);
JLabel jl1 = new JLabel("用户名:");
jp[1].add(jl1);
jp[1].add(jtf1);
JLabel jl2 = new JLabel(" 密码: ");
jp[2].add(jl2);
jp[2].add(jtf2);
JLabel jl3 = new JLabel(" Email:");
jp[3].add(jl3);
jp[3].add(jtf3);
JButton jb1 = new JButton("提交");
jp[4].add(jb1);
jb1.addActionListener(this);
JButton jb2 = new JButton("清空");
jp[4].add(jb2);
jb2.addActionListener(this);
jf.setLocation(300, 200);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new TestLogin();
}
}
这个应该就是你想要的答案!追问

这小伙太强了,佩服···

追答

呵呵

温馨提示:答案为网友推荐,仅供参考
相似回答