java--swing界面实现注册登录(用文本文件存储数据)

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

**开源地址:https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB **

在这里插入图片描述

在这里插入图片描述

虽然使用java很久了,但是对于swing的图形编程还是个小白,还有对于文件io流的操作也几乎是没有接触过,所以今天写这篇文章来学习一下。用swing来写桌面软件真的好麻烦,主要是我没有安装可视化的插件,所以在设置布局的时候很烦。

来看一下代码吧!

1.首先是软件的入口 Main类

import java.io.IOException;

public class Main {

public static void main(String[] args) throws IOException {

new Login();//首先进入登录界面

}

}

2.Login类

import java.awt.GridLayout;

import java.awt.HeadlessException;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import javax.swing.*;

@SuppressWarnings(“serial”)

public class Login extends JFrame implements ActionListener{

JButton jb1, jb2, jb3; //按钮

JPanel jp1,jp2,jp3, jp4; //面板

JTextField jtf; //文本框

JLabel jlb1, jlb2, jlb3; //标签

JPasswordField jpf; //密码框

public Login() {

// TODO Auto-generated constructor stub

jb1 = new JButton(“登录”);

jb2 = new JButton(“注册”);

//设置按钮监听

jb1.addActionListener(this);

jb2.addActionListener(this);

jp1 = new JPanel(); //创建面板

jp2 = new JPanel();

jp3 = new JPanel();

jp4 = new JPanel();

jlb1 = new JLabel(“用户名”); //添加标签

jlb2 = new JLabel(" 密 码");

jtf = new JTextField(10); //创建文本框和密码框

jpf = new JPasswordField(10);

//加入面板中

jp1.add(jlb1);

jp1.add(jtf);

jp2.add(jlb2);

jp2.add(jpf);

jp3.add(jb1);

jp3.add(jb2);

//将JPane加入JFrame中

this.add(jp1);

this.add(jp2);

this.add(jp3);

//设置布局

this.setTitle(“用户登录”);

this.setLayout(new GridLayout(3,1));

this.setSize(300, 200); //设置窗体大小

this.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); //设置仅关闭当前窗口

this.setVisible(true); //设置可见

this.setResizable(false); //设置不可拉伸大小

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if (e.getActionCommand()==“登录”)

{

try {

login();

} catch (HeadlessException | IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

else if(e.getActionCommand()==“注册”)

{

// clear();

new Register();

dispose(); //使窗口消失

}

}

//清空账号和密码框

private void clear() {

// TODO Auto-generated method stub

jtf.setText(“”); //设置为空

jpf.setText(“”);

}

//验证登录信息,并做处理

@SuppressWarnings(“deprecation”)

public void login() throws HeadlessException, IOException

{

//判断账户名和密码是否包含中文

if (new Check().checkcountname(jtf.getText())||new Check().checkcountname(jpf.getText()))

{

JOptionPane.showMessageDialog(null, “用户名或密码存在中文,不合法!”,“消息提示”,JOptionPane.WARNING_MESSAGE);

}

else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())

{

JOptionPane.showMessageDialog(null, “账号密码为空,请输入!”,“消息提示”,JOptionPane.WARNING_MESSAGE);

}

else if (jtf.getText().isEmpty())

{

JOptionPane.showMessageDialog(null, “账号为空,请输入!”,“消息提示”,JOptionPane.WARNING_MESSAGE);

}

else if (jpf.getText().isEmpty())

{

JOptionPane.showMessageDialog(null, “密码为空,请输入!”,“消息提示”,JOptionPane.WARNING_MESSAGE);

}

else if (new Check().check1(jtf.getText(),jpf.getText()))

{

JOptionPane.showMessageDialog(null,“登录成功!”,“提示消息”,JOptionPane.WARNING_MESSAGE);

dispose(); //使文原窗体消失

}

else

{

JOptionPane.showMessageDialog(null, “账号密码错误请重新输入!”,“消息提示”,JOptionPane.ERROR_MESSAGE);

clear(); //调用清除函数

}

}

}

3.Register类

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import javax.swing.*;

/**

  • 此类完成对注册页面的编写, 用户需填写 账户,密码

  • 并且会进行验证操作, 如姓名是否合法(中文)

*/

@SuppressWarnings(“serial”)

public class Register extends JFrame implements ActionListener{

JButton jb1, jb2; //按钮

JLabel jlb1, jlb2, jlb3,jlb4,jlb5, jlb6; //标签

JTextField jtf3,jtf4; //文本框

JPasswordField jpf; //密码框

JPanel jp3, jp4,jp6,jp7; //面板

public Register() {

// TODO Auto-generated constructor stub

//按钮

jb1 = new JButton(“提交”);

jb2 = new JButton(“登录”);

//设置按钮监听

jb1.addActionListener(this);

jb2.addActionListener(this);

//标签信息

jlb3 = new JLabel(" 账号");

jlb4 = new JLabel(" 密码");

jtf3 = new JTextField(13);

jtf4 = new JTextField(13);

jp3 = new JPanel();

jp4 = new JPanel();

jp6 = new JPanel();

jp7 = new JPanel();

//将对应信息加入面板中

jp3.add(jlb3);

jp3.add(jtf3);

jp4.add(jlb4);

jp4.add(jtf4);

jp6.add(jb1);

jp6.add(jb2);

//将JPane加入JFrame中

this.add(jp7); //先加入提示语

this.add(jp3);

this.add(jp4);

this.add(jp6);

//设置布局

this.setTitle(“注册信息”);

this.setLayout(new GridLayout(5, 1));

this.setSize(250, 240); //设置窗体大小

this.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); //设置仅关闭当前窗口

this.setVisible(true); //设置可见

this.setResizable(false); //设置不可拉伸大小

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if (e.getActionCommand()==“提交”)

{

try {

register();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

else if (e.getActionCommand()==“登录”)

{

new Login();

}

}

//验证注册信息,并做处理

public void register() throws IOException

{

//判断信息是否补全

if (jtf3.getText().isEmpty()||jtf4.getText().isEmpty())

{

JOptionPane.showMessageDialog(null, “信息有空缺,请补全!”,“消息提示”,JOptionPane.WARNING_MESSAGE);

}

//判断账户名和密码是否包含中文

else if (new Check().checkcountname(jtf3.getText())||new Check().checkcountname(jtf4.getText()))

{

JOptionPane.showMessageDialog(null, “用户名或密码存在中文,不合法!”,“消息提示”,JOptionPane.WARNING_MESSAGE);

}

//满足要求

else if (!jtf3.getText().isEmpty()&&!jtf4.getText().isEmpty())

{

//注册成功, 打包为信息数组传递给UserMessage进行更新操作

String []message = new String[2];

message[0] = jtf3.getText();

message[1] = jtf4.getText();

if (new Check().check2(message[0])) //调用Check的check方法检测用户是否存在, 如果不存在执行

{

new UserMessage().write(message); //调用UserMseeage的write方法进行写操作, 将信息格式化存入

JOptionPane.showMessageDialog(null,“注册成功!”,“提示消息”,JOptionPane.WARNING_MESSAGE);

dispose(); //使窗口消失

new Login();

}

else

{

JOptionPane.showMessageDialog(null,“账号已存在,请重新输入!”,“提示消息”,JOptionPane.WARNING_MESSAGE);

//dispose();

}

}

}

//清空账号和密码框

private void clear() {

// TODO Auto-generated method stub

jtf3.setText(“”);

jtf4.setText(“”);

}

}

4.UserMessage类

import java.io.*;

/**

  • 此类事对用户信息的 写入和读取操作

*/

public class UserMessage

{

/*

  • 将注册的信息写入文本

*/

public void write(String[] message)throws IOException

{

File file=new File(“Message.txt”);

String messagesum=“”;

for (int i=0; i<2; i++) //将信息格式化存储

messagesum+=message[i]+“~”;

if(!file.exists())

file.createNewFile();

FileOutputStream out=new FileOutputStream(file,true); //建立输出对象,true表示追加

StringBuffer sb=new StringBuffer(); //创建字符串流

sb.append(messagesum+“\n”); //向字符串流中添加信息

out.write(sb.toString().getBytes(“gb2312”)); //将字符串流中的信息写入文本

out.close(); //关闭

}

/*

  • 读取信息,将用户名信息返回(如果不存在返回null),和Check类配合使用

*/

public String[] read(String countname) throws IOException

{

File file=new File(“Message.txt”);

if(!file.exists()||file.isDirectory())

throw new FileNotFoundException();

BufferedReader br=new BufferedReader(new FileReader(file));

  • 16
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.shou.loginfjame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.bind.util.ValidationEventCollector; import com.shou.LoginUtil.LoginUser; import com.shou.dao.LoginDao; import com.shuo.util.ValidCode; public class LoginFjame extends JFrame implements ActionListener { private JFrame frame = new JFrame("登录"); private JPanel panel = new JPanel(); private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题 private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名 private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel private JPasswordField passText = new JPasswordField(20); // 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 JTextField jt_code; private JButton loginButton = new JButton("登录"); // 创建登录按钮 private JButton registerButton = new JButton("注 册"); // 创建注册按钮 private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮 private JButton exitButton = new JButton("退出"); JTextField field = null; public LoginFjame() { System.out.println("====================================="); System.out.println("== 龍丶逸小说系统 =="); System.out.println("== V1.1.1.0 =="); System.out.println("====================================="); WinLogin(); } public void WinLogin() { panel.setLayout(null); // 设置布局为 null // 创建标题名称 this.tiel.setFont(new Font("宋体", 1, 20)); this.tiel.setBounds(150, 30, 300, 25); this.panel.add(this.tiel); // 创建 UserJLabel this.userLabel.setFont(new Font("宋体", 1, 13)); this.userLabel.setBounds(70, 80, 80, 25); this.panel.add(userLabel); // 创建文本域用于用户输入 this.userText.setBounds(145, 80, 165, 25); this.panel.add(this.userText); // 注册 this.registerButton.setFont(new Font("宋体", 1, 15)); this.registerButton.setContentAreaFilled(false); this.registerButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.registerButton.setBounds(320, 80, 100, 25); this.panel.add(this.registerButton); // 变成小手 this.registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 创建PassJLabel this.passLabel.setFont(new Font("宋体", 1, 13)); this.passLabel.setBounds(70, 110, 80, 25); this.panel.add(this.passLabel); // 密码输入框 隐藏 this.passText.setBounds(145, 110, 165, 25); this.panel.add(this.passText); // 忘记密码 this.newPasswordButton.setFont(new Font("宋体", 1, 15)); this.newPasswordButton.setContentAreaFilled(false); this.newPasswordButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.newPasswordButton.setBounds(320, 110, 100, 25); this.panel.add(this.newPasswordButton); // 变成小手 this.newPasswordButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 验证码code this.verCodeLa.setFont(new Font("宋体", 1, 13)); this.verCodeLa.setBounds(70, 140, 80, 25); this.panel.add(this.verCodeLa); // 验证码框 this.inputCode.setBounds(145, 140, 165, 25); this.panel.add(this.inputCode); // 验证码图片 this.vcode.setBounds(320, 140, 165, 25); this.panel.add(this.vcode); System.out.println(this.vcode); // 创建登录按钮 this.loginButton.setFont(new Font("宋体", 1, 15)); this.loginButton.setBounds(95, 190, 80, 25); this.panel.add(this.loginButton); // 变成小手 this.loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 退出按钮 this.exitButton.setFont(new Font("宋体", 1, 15)); this.exitButton.setBounds(230, 190, 80, 25); this.panel.add(this.exitButton); // 变成小手 this.exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 设置窗体的位置及大小 this.frame.setSize(460, 355); frame.setLocationRelativeTo(null); // 在屏幕中居中显示 frame.add(this.panel); // 添加面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭 //设置按钮 this.registerButton.addActionListener(this); //注册按钮 this.newPasswordButton.addActionListener(this); //忘记密码 this.loginButton.addActionListener(this); //登录 this.exitButton.addActionListener(this); //退出 // 往窗体里放其他控件 frame.setVisible(true); // 设置窗体可见 } @Override public void actionPerformed(ActionEvent e) { JButton bt = (JButton) e.getSource(); // 获取按钮信息 String str = bt.getText(); // 获取用户名 String name = this.userText.getText().trim(); // 获取密码 String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录")) { System.out.println("登录"); // 验证码转为大写 String Dcode = code.toUpperCase(); String Dvcode = vcode.toUpperCase(); // 验证码判断 if (Dcode.equals(Dvcode)) { //获取页面的用户名 String username=this.userText.getText().trim(); // 根据用户名查看是否有该用户 try { List loginUser=new LoginDao().queryAll(username); String a=loginUser.toString(); System.out.println(a.toString()); if(!a.toString().equals("[]")){ //密码判断 String mysqlPasword=loginUser.get(0).l_password(); if(mysqlPasword.equals(password)){ //登录成功 JOptionPane pane = new JOptionPane("登录成功"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); }else{ JOptionPane pane = new JOptionPane("密码错误错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } }else{ JOptionPane pane = new JOptionPane("用户名错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } /*System.out.println(loginUser.toString()); String sqlUername=loginUser.get(0).getL_username();*/ /*int sqlpassword=loginUser.get(0).getL_power();*/ /*System.out.println("loginF:"+sqlUername);*/ } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("验证码错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); System.out.println(dialog.getFont()); dialog.show(); } } else // 退出 if (str.equals("退出")) { System.out.println("退出"); System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out.println("异常错误"); } } public boolean isValidCodeRight() { System.out.println(this.jt_code.getText()); if (this.jt_code == null) { return false; } if (this.vcode == null) { return true; } if (this.vcode.getCode().equals(this.jt_code.getText())) { return true; } return false; } public static void main(String[] args) { new LoginFjame(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值