說明
使用的開發工具是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 即可,當然只有這一個構造方法。這個類是用於將表格組件可編輯改爲不能編輯。