说明
使用的开发工具是idea 社区2019 .2.2 版
实验进程
1 回顾 java GUI的基本用法
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.*;
public class Login_in extends JFrame implements ActionListener {
public JLabel account;
public JLabel password;
public JButton button_login_in;
public JTextField jt_account;
public JPasswordField jt_password;
public JPanel jp1, jp2;
public JLabel jl1;
public ImageIcon Icon;
protected Windows windows=null;
public Login_in() {
this.jl1 = new JLabel();
this.account = new JLabel("输入用户名");
this.password = new JLabel("输入密码");
this.button_login_in = new JButton("submit");
this.Icon = new ImageIcon(getClass().getResource("/photo/4.jpg"));//使用的是getResource
this.Icon.setImage(this.Icon.getImage().getScaledInstance(400, 400, Image.SCALE_DEFAULT));
this.jl1.setIcon(this.Icon);
this.button_login_in.addActionListener(this);
this.jt_account = new JTextField(10);
this.jt_password = new JPasswordField(10);
this.jp1 = new JPanel();
this.jp2 = new JPanel();
this.setSize(400, 550);
this.getContentPane().add(this.jl1);
this.setLayout(new FlowLayout());
this.setLocationRelativeTo(null);
this.jp1.add(this.account);
this.jp1.add(this.jt_account);
this.getContentPane().add(this.jp1);
this.jp2.add(this.password);
this.jp2.add(this.jt_password);
this.jp2.add(this.button_login_in);
this.getContentPane().add(this.jp2);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
new Login_in();
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
String a, b;
if (actionEvent.getSource().equals(this.button_login_in)) {
a = this.jt_account.getText();
b = this.jt_password.getText();
Pattern p_account = Pattern.compile("[A-Za-z0-9]{4,9}");
Pattern p_password = Pattern.compile("[A-Za-z0-9]{6,9}");
Matcher m_account = p_account.matcher(a);
if (m_account.find()) {
if(this.windows==null){
this.windows= new Windows(this);
}
this.windows.setVisible(true);
this.setVisible(false);
System.out.println("ok is login ");
} else {
JOptionPane.showMessageDialog(this,"account is error!!");
}
}
}
}
效果如图所示:
2 需要的使用 java 正则表达式的如下
import java.util.regex.*;
public class process {
public static void main(String[] args) {
String p ="[A-Za-z0-9]{4,9}";
Pattern r=Pattern.compile(p);
String line="125";
Matcher m =r.matcher(line);
if(m.find()){
System.out.println("ok is find !!!");
}
else {
System.out.println(" no !!!");
}
}
}
这个类将会用来判断学生还是教师登录
3 java Vector 相关的用法
import java.util.Vector;
public class VectorTest {
public static void main(String[] args) {
Vector<String >str = new Vector<>();
str.add("a");
str.add("b");
for(int i=1;i<=str.size();i++){
System.out.println(str.get(i-1));
}
str.clear();
//清空所有的数据
}
}
在存储数据库的数据将会用到vector
4 使用的Md5 加密如下
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class Md5 {
public String getMd5_String(String a){
String str = null;
try{
MessageDigest md=MessageDigest.getInstance("md5");
byte[] bytes = md.digest(a.getBytes("UTF-8"));
//得到了一个字节 计算摘要
// a-z A-Z 0-9 / * 表示生成的string
str= Base64.getEncoder().encodeToString(bytes);
System.out.println(str);
}catch (NoSuchAlgorithmException e){
e.printStackTrace();
}
catch (UnsupportedEncodingException e){
e.printStackTrace();
System.out.println("编码不支持");
}
return str;
}
}
md5 将会用在存储用户的密码这个方面上。
5 重写 DefaultTableModel 这个类
import javax.swing.table.DefaultTableModel;
public class MyDefaultTable extends DefaultTableModel {
public MyDefaultTable(String [] str,int rows){
super(str,rows);
}
public boolean isCellEditable(int row,int col){
return false;
}
}
由此可见重写isCellEditable 方法改为false 即可,当然只有这一个构造方法。这个类是用于将表格组件可编辑改为不能编辑。