目录
要求
实现效果:
代码:
要求
题目描述
编写一个 GUI 登录界面,如下图:
输入用户名、密码,
1、如果是第一次进入该界面,请点击“注册”,自动将用户名和密码存入“学号.txt”文件。然
后登陆。
2、如果不是第一次进入该界面,请点击“登陆”。如果输入正确,显示“登陆成功!”的提示框;
否则,给出“不存在该用户!”或“密码错误,请重新输入!”的提示框,并清空用户名、
密码输入区域。
基本要求
A.用户名为考生自己的学号,密码必须包含大写字母、小写字母和数字。
B.用户的业务逻辑单独使用类的定义完成
评分标准
A. 用户类的定义(30 分)
B.程序界面设计(20 分)
C. 事件处理(20 分)
D. 异常处理(10 分)
E. 文件读写(10 分)
E. 程序正常运行(10 分)
每个评分标准中都包含代码质量(40%)、规范性(30%)和结果正确性(30%)
提示:消息提示框
import javax.swing.JOptionPane;
String message=" 不存在 ID 为 "+ID+" 的职工.";
JOptionPane.showMessageDialog(null,message,"信息消息提示框",
JOptionPane.INFORMATION_MESSAGE);
实现效果:
文章来源:https://www.toymoban.com/news/detail-498377.html
代码:
package MY_GUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class test {
public char[] DD = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
public char[] dd = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
public char[] ss = {'0','1','2','3','4','5','6','7','8','9'};
// public String[] D = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","L","S","T","U","V","W","X","Y","Z"};
// public String[] d = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","l","s","t","u","v","w","x","y","z"};
// public String[] s = {"0","1","2","3","4","5","6","7","8","9"};
public JTextField jtext0,jtext1;
public String get_jtext0,get_jtext1;
public JFrame jf;
//目标文件
final String TARGET_FILE = "2000300420.txt";
File targetFile = new File(TARGET_FILE);
public test() throws IOException {
jf = new JFrame("注册登陆页面");
jf.setSize(270, 270);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel jp0 = new JPanel();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
jp0.add(new JLabel("用户名:"));
jtext0 = new JTextField(10);
jp0.add(jtext0);
jp1.add(new JLabel("密 码:"));
jtext1 = new JTextField(10);
jp1.add(jtext1);
JButton jb0 = new JButton("注册");
JButton jb1 = new JButton("登陆");
jp2.add(jb0);
jp2.add(jb1);
if(targetFile.createNewFile()) {
System.out.println("文件不存在,创建成功");
}else {
System.out.println("文件存在");
}
// 注册操作
jb0.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String get_jtext0 = jtext0.getText();
String get_jtext1 = jtext1.getText();
int found0 =0,found1 =0,found2 =0;
char[] a=get_jtext1.toCharArray();
for(int yy=0;yy<a.length;yy++) {
for(int y=0;y<26;y++) {
if(a[yy]==DD[y]) {
found0=1;
break;
}
}
}
for(int yy=0;yy<a.length;yy++) {
for(int y=0;y<26;y++) {
if(a[yy]==dd[y]) {
found1=1;
break;
}
}
}
for(int yy=0;yy<a.length;yy++) {
for(int y=0;y<10;y++) {
if(a[yy]==ss[y]) {
found2=1;
break;
}
}
}
System.out.println(found0+found1+found2);
if(found0==0||found1==0||found2==0) {
System.out.println("密码格式错误!");
// 消息对话框无返回, 仅做通知作用
JOptionPane.showMessageDialog(
jf,
"密码必须包含大写、小写字母和数字!",
"注册失败",
JOptionPane.INFORMATION_MESSAGE
);
}else {
try {
//打开文件字符输出流
FileWriter writer = new FileWriter(targetFile,true);
//写入文件
writer.write("\n");
writer.write(get_jtext0);
System.out.println(get_jtext0);
writer.write("\n");
writer.write(get_jtext1);
writer.flush();
//关闭输出流
writer.close();
// 消息对话框无返回, 仅做通知作用
JOptionPane.showMessageDialog(
jf,
"注册成功!",
"注册",
JOptionPane.INFORMATION_MESSAGE
);
// 消息对话框无返回, 仅做通知作用
JOptionPane.showMessageDialog(
jf,
"登陆成功!",
"登陆",
JOptionPane.INFORMATION_MESSAGE
);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
// 登陆操作
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String get_jtext0 = jtext0.getText();
String get_jtext1 = jtext1.getText();
// 读
FileReader in = null;
try {
in = new FileReader("2000300420.txt");
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
BufferedReader bufIn = new BufferedReader(in);
String line = null;
String[] datas = new String[100];
// 读入结果
int i=0;
int j=0;
try {
while ( (line = bufIn.readLine()) != null) {
datas[i]=line;
i++;
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true) {
if(datas[j].equals(get_jtext0)&&datas[j+1].equals(get_jtext1)) {
// 消息对话框无返回, 仅做通知作用
JOptionPane.showMessageDialog(
jf,
"登陆成功!",
"登陆",
JOptionPane.INFORMATION_MESSAGE
);
break;
}
j++;
if(datas[j]==null) {
System.out.println("不存在该用户!");
// 清除框内输入字符
jtext0.setText("");
jtext1.setText("");
// 消息对话框无返回, 仅做通知作用
JOptionPane.showMessageDialog(
jf,
"不存在该用户!",
"登陆失败",
JOptionPane.INFORMATION_MESSAGE
);
break;
}
}
// 关闭 输入流
try {
bufIn.close();
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
// 创建一个垂直盒子容器, 把上面 3 个 JPanel 串起来作为内容面板添加到窗口
Box vBox = Box.createVerticalBox();
vBox.add(jp0);
vBox.add(jp1);
vBox.add(jp2);
jf.setContentPane(vBox);
// jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
public static void main(String[] args) throws IOException {
new test();
}
}
文章来源地址https://www.toymoban.com/news/detail-498377.html
到了这里,关于JAVA_GUI之“注册登陆界面”的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!