import java.awt.*;
我们提供的服务有:成都网站建设、网站制作、微信公众号开发、网站优化、网站认证、曲沃ssl等。为上千多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的曲沃网站制作公司
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();
}
}
这个应该就是你想要的答案!
效果图
参考代码和注释
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
//该窗口继承自JFrame, 实现了ActionListener接口
public class SingUpFrame extends JFrame implements ActionListener {
//定义需要的组件
JTextField jtfName, jtfEmail;
JPasswordField jpf;
JRadioButton jrb1, jrb2;
JComboBoxString jcb;
JButton jbReset, jbSingUp;
//常量
static final String NEW_LINE = System.getProperty("line.separator");// 获取系统的换行符
static final String FILE_PATH = "d:/users.txt";//指定文件的路径
//构造器
public SingUpFrame() {
JPanel jp1 = new JPanel();
JLabel jl1 = new JLabel("账号");
jtfName = new JTextField(15);
jp1.add(jl1);
jp1.add(jtfName);
JPanel jp2 = new JPanel();
JLabel jl2 = new JLabel("密码");
jpf = new JPasswordField(15);
jp2.add(jl2);
jp2.add(jpf);
JPanel jp3 = new JPanel();
JLabel jl3 = new JLabel("邮箱");
jtfEmail = new JTextField(15);
jp3.add(jl3);
jp3.add(jtfEmail);
JPanel jp4 = new JPanel();
JLabel jl4 = new JLabel("性别");
ButtonGroup bg = new ButtonGroup();
jrb1 = new JRadioButton("男");
jrb1.setSelected(true);// 默认选中男性
jrb2 = new JRadioButton("女");
bg.add(jrb1);
bg.add(jrb2);
jp4.add(jl4);
jp4.add(jrb1);
jp4.add(jrb2);
JLabel jl5 = new JLabel("年龄");
String[] ary = new String[12];
for (int i = 18; i 30; i++) {// 18~30可供选中的范围
ary[i - 18] = i + "";
}
jcb = new JComboBoxString(ary);
jp4.add(jl5);
jp4.add(jcb);
JPanel jpc = new JPanel(new GridLayout(4, 1));// 4行1列布局
jpc.add(jp1);
jpc.add(jp2);
jpc.add(jp3);
jpc.add(jp4);
add(jpc);
JPanel jps = new JPanel();
jbReset = new JButton("重填");
jbReset.addActionListener(this);//给按钮添加响应
jbSingUp = new JButton("确定");
jbSingUp.addActionListener(this);//给按钮添加响应
jps.add(jbReset);
jps.add(jbSingUp);
add(jps, BorderLayout.SOUTH);
setTitle("注册窗口");// 窗口标题
setSize(300, 285);// 窗口大小
setLocationRelativeTo(null);// 窗口居中
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 当窗口关闭时,程序结束
}
@Override
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource();
if (jb == jbReset) {
jtfName.setText("");
jpf.setText("");
jtfEmail.setText("");
jrb1.setSelected(true);
jcb.setSelectedIndex(0);// 选中第1个选项(18)
} else if (jb == jbSingUp) {
String name = jtfName.getText().trim();
String pswd = new String(jpf.getPassword());
String email = jtfEmail.getText().trim();
String xb = jrb1.isSelected() ? "男" : "女";
String age = (String) jcb.getSelectedItem();
if(name.equals("")||pswd.equals("")||email.equals("")) {//对输入的信息进行一个简单的判断
JOptionPane.showMessageDialog(null, "请完整填写所有的信息", "提示",JOptionPane.WARNING_MESSAGE);
return;
}
StringBuffer sb = new StringBuffer();
sb.append("账号:" + name + NEW_LINE + "密码:" + pswd + NEW_LINE + "邮箱:" + email + NEW_LINE + "性别:" + xb
+ NEW_LINE + "年龄:" + age+NEW_LINE);
boolean flag = saveInfo(sb.toString());
if(flag) {
JOptionPane.showMessageDialog(null, "注册成功 ,保存成功");
}else {
JOptionPane.showMessageDialog(null, "Sorry!保存失败.注册出现了问题...", "IO错误",JOptionPane.ERROR_MESSAGE);
}
}
}
//方法:保存信息到文件
public boolean saveInfo(String info) {
FileWriter fw;
try {
fw = new FileWriter(FILE_PATH, true);//追加文字到文件尾部
fw.write(info);
fw.close();
} catch (IOException e) {
e.printStackTrace();
return false;//IO异常,保存不成功
}
return true;//保存成功
}
//main方法
public static void main(String[] args) {
new SingUpFrame().setVisible(true); //创建窗口并可见
}
}
界面建议用可视化来做,美观且便捷。下面这个是完全用代码写的,仅供参考。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class Register extends JFrame {
JLabel jl1 = new JLabel("用户名");
JTextField jt1 = new JTextField();
JLabel jl2 = new JLabel("邮箱");
JTextField jt2 = new JTextField();
JLabel jl3 = new JLabel("密码");
JPasswordField jpw1 = new JPasswordField();
JLabel jl4 = new JLabel("密码确认");
JPasswordField jpw2 = new JPasswordField();
JButton register = new JButton("注册");
JButton clean = new JButton("清空");
public Register(){
setLayout(new GridLayout(5,2));
add(jl1);
add(jt1);
add(jl2);
add(jt2);
add(jl3);
add(jpw1);
add(jl4);
add(jpw2);
add(register);
add(clean);
String name = jt1.getText();
String email = jt2.getText();
String pw = jpw1.getText();
register.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db","root","");
Statement sta = con.createStatement();
sta.executeUpdate("INSERT INTO register VALUES(name,email,pw)");
JOptionPane.showMessageDialog(null,"注册成功","提示",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex){
ex.getStackTrace();
}
}
});
clean.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jt1.setText("");
jt2.setText("");
jpw1.setText("");
jpw2.setText("");
}
});
}
public static void main(String[] args){
Register frame = new Register();
frame.setTitle("用户注册");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
}
ajax异步刷新
input type="text" name="email" id="email" onchange="checkfunc(this.value)"
js脚本:
function checkfunc(obj){
var url = '/check.do?';
var args = obj;
$.getJSON(url,args,function(data){
if(data==" "){ //data为后台传来的响应数据(自定义),局逻辑判断具体值
alert("邮箱已经被注册!");
}
});
}
action类:省(太复杂,吃饭了)
要去数据库里查,先要写连接数据库方法,不同数据库用有不同的连接方式,连接上了再查就简单了