效果如下图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201231162016408.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzQ0OTI0Ng==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201231162031226.png)
虽然使用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() {
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);
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) {
if (e.getActionCommand()=="登录")
{
try {
login();
} catch (HeadlessException | IOException e1) {
e1.printStackTrace();
}
}
else if(e.getActionCommand()=="注册")
{
new Register();
dispose();
}
}
private void clear() {
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() {
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);
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) {
if (e.getActionCommand()=="提交")
{
try {
register();
} catch (IOException e1) {
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())
{
String []message = new String[2];
message[0] = jtf3.getText();
message[1] = jtf4.getText();
if (new Check().check2(message[0]))
{
new UserMessage().write(message);
JOptionPane.showMessageDialog(null,"注册成功!","提示消息",JOptionPane.WARNING_MESSAGE);
dispose();
new Login();
}
else
{
JOptionPane.showMessageDialog(null,"账号已存在,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
}
}
}
private void clear() {
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);
StringBuffer sb=new StringBuffer();
sb.append(messagesum+"\n");
out.write(sb.toString().getBytes("gb2312"));
out.close();
}
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));
String temp=null;
StringBuffer sb=new StringBuffer();
temp=br.readLine();
String []message = new String[5];
while(temp!=null){
String sbstring = temp.toString();
int n = sbstring.length();
for (int i=0; i<5; i++)
message[i] = "";
int k=0;
for (int i=0; i<n; i++)
{
if(sbstring.charAt(i)=='~')
{
k++;
}
else
{
message[k] += sbstring.charAt(i);
}
}
if (message[2].equals(countname))
{
return message;
}
temp=br.readLine();
}
return null;
}
public String updatepwd(String countname,String pwd) throws IOException
{
File file=new File("Message.txt");
if(!file.exists()||file.isDirectory())
throw new FileNotFoundException();
BufferedReader br=new BufferedReader(new FileReader(file));
String temp=null;
StringBuffer sb=new StringBuffer();
StringBuffer sb1=new StringBuffer();
String moneystring="";
temp=br.readLine();
String []message = new String[2];
while(temp!=null){
String sbstring = temp.toString();
int n = sbstring.length();
for (int i=0; i<2; i++)
message[i] = "";
int k=0;
for (int i=0; i<n; i++)
{
if(sbstring.charAt(i)=='~')
{
k++;
}
else
{
message[k] += sbstring.charAt(i);
}
}
if (message[0].equals(countname))
{
for (int i=0; i<3; i++)
sb1.append(message[i]+"~");
sb1.append(pwd+"~");
sb1.append(message[4]+"~\n");
}
else
{
sb1.append(temp+"\n");
}
temp=br.readLine();
}
File file1=new File("Message.txt");
if(!file1.exists())
file1.createNewFile();
FileOutputStream out=new FileOutputStream(file1,false);
out.write(sb1.toString().getBytes("gb2312"));
out.close();
return moneystring;
}
}
5.Check类
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Check {
public boolean check1(String countname,String pwd) throws IOException{
File file=new File("Message.txt");
if(!file.exists()||file.isDirectory())
file.createNewFile();
BufferedReader br=new BufferedReader(new FileReader(file));
String temp=null;
temp=br.readLine();
while(temp!=null){
String sbstring = temp.toString();
int n = sbstring.length();
String []message = new String[5];
int k=0;
for (int i=0; i<2; i++)
message[i]="";
for (int i=0; i<n; i++)
{
if(sbstring.charAt(i)=='~')
{
k++;
}
else
{
message[k] += sbstring.charAt(i);
}
}
if (countname.equals(message[0])&&pwd.equals(message[1]))
return true;
temp=br.readLine();
}
return false;
}
public boolean check2(String countname) throws IOException{
File file=new File("Message.txt");
if(!file.exists()||file.isDirectory())
file.createNewFile();
BufferedReader br=new BufferedReader(new FileReader(file));
String temp=null;
temp=br.readLine();
while(temp!=null){
String sbstring = temp.toString();
int n = sbstring.length();
String []message = new String[5];
int k=0;
for (int i=0; i<5; i++)
message[i]="";
for (int i=0; i<n; i++)
{
if(sbstring.charAt(i)=='~')
{
k++;
}
else
{
message[k] += sbstring.charAt(i);
}
}
if (!countname.equals(message[0]))
return true;
temp=br.readLine();
}
return false;
}
public boolean checkcountname(String countname)
{
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(countname);
if (m.find()) {
return true;
}
return false;
}
public boolean checkname(String name)
{
int n = 0;
for(int i = 0; i < name.length(); i++) {
n = (int)name.charAt(i);
if(!(19968 <= n && n <40869)) {
return false;
}
}
return true;
}
}
源码已上传码云:
https://gitee.com/wangjinchan/java-swing-login.git
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)