本文參考與:https://blog.csdn.net/wyf2017/article/details/78831744
https://blog.csdn.net/MengKun822/article/details/89302921
https://www.cnblogs.com/yang2000/p/11565412.html
一.登錄界面
1.程序代碼
import java.awt.*;//導入awt包 import javax.swing.*;//導入swing包 import java.awt.event.ActionListener;//導入awt包中的監聽器事件包 import java.awt.event.ActionEvent;//導入awt包中的ActionEvent事件包 public class EnterScreen extends JFrame { static int s=0; public EnterScreen() { Yanzhencode vcode = new Yanzhencode(); setSize(300,290);//設計窗體的大小 setTitle("請登錄"); setBackground(Color.RED);//設置背景顏色 JLabel a=new JLabel("登錄名"); //實例化JLabel對象 JLabel b=new JLabel("密 碼"); JLabel g=new JLabel("忘記用戶名/密碼?"); JLabel h=new JLabel("驗證碼"); g.setForeground(Color.BLUE); JTextField c=new JTextField(15);//實例化用戶名文本框 JPasswordField d=new JPasswordField(15);//實例化密碼框 JTextField k=new JTextField(4);//實例化驗證碼框 d.setEchoChar('*');//將輸入密碼框中的密碼以*顯示出來 JButton e=new JButton("登錄"); JButton f=new JButton("快速注冊"); e.setBackground(Color.YELLOW);//設置登錄按鈕字體顏色 f.setForeground(Color.GRAY);//設置快速登錄按鈕填充色 setVisible(true);//使窗體可視化 Container m=getContentPane();//獲取一個容器 getContentPane().setBackground(Color.WHITE);//設置窗體填充色 // 將用戶名、密碼的Jlabel和用戶名JTextField文本框、密碼JPasswordField密碼框以及確定JButton、快速注冊JButton添加到container容器里面 // m.add(a); m.add(b); m.add(c); m.add(d); m.add(e); m.add(f); m.add(g); m.add(h); m.add(k); m.add(vcode); setBounds(300,300,300,300);//設置窗體的長寬各為300、300 讓其顯示在左上方的300、300處 m.setLayout(null); // a、b、c、d、e、f顯示在container容器中的位置坐標 a.setBounds(10,40,50,18); b.setBounds(10,80,50,18); c.setBounds(60,40,130,18); d.setBounds(60,80,130,18); h.setBounds(10,120,50,18); k.setBounds(60,120,80,18); e.setBounds(90,180,100,30); f.setBounds(90,220,100,30); g.setBounds(190,75,100,30); vcode.setBounds(140,110,80,30); e.addActionListener(new ActionListener() {//對登錄按鈕添加監聽事件 @SuppressWarnings("deprecation") @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(c.getText().trim().equals("xiaoyang")&&new String(d.getPassword()).equals("123456")&&s==1) {//equals函數進行用戶名和密碼的匹配 JOptionPane.showMessageDialog(null,"登錄成功"); new NewFrame();//進入到NewFrame這個窗體中 }else if(c.getText().trim().equals("xiaoyang")&&new String(d.getPassword()).equals("123456")&&s==0) { JOptionPane.showMessageDialog(null,"驗證碼輸入錯誤"); }else { JOptionPane.showMessageDialog(null, "登錄失敗,用戶名、密碼或驗證碼輸入錯誤"); } } }); f.addActionListener(new ActionListener(){//對快速注冊按鈕添加監聽事件 @SuppressWarnings("deprecation") @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub new zhuce();//進入都到zhuce窗體中 } }); //判斷輸入驗證碼是否正確 if(k.getText()== null) { s=0; }else if(vcode.getCode() == null) { s=1; }else if(vcode.getCode() .equals(k.getText())) { s=1; }else { s=0; } } public static void main(String[] args) { new EnterScreen(); } }
2.登錄界面截圖
二.快速注冊界面
1.程序代碼
import java.awt.*; import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.sql.*; import java.awt.Container; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class zhuce extends JFrame { public zhuce() { setSize(300,250);//設計窗體的大小 JLabel a=new JLabel("用戶名"); //實例化JLabel對象 JLabel b=new JLabel("密 碼"); JLabel h=new JLabel("再次確認密碼"); JTextField c=new JTextField(15);//實例化用戶名文本框 JPasswordField d=new JPasswordField(15);//實例化密碼文本框 JPasswordField hd=new JPasswordField(15);//實例化密碼文本框 d.setEchoChar('*');//設置密碼隱藏制度 JButton e=new JButton("確定"); JButton f=new JButton("重置"); JButton g=new JButton("返回"); setVisible(true); //獲取一個容器 Container m=getContentPane(); // 將用戶名、密碼的Jlabel和用戶名JTextField文本框、密碼JPasswordField密碼框以及確定JButton、快速注冊JButton添加到container容器里面 m.add(a); m.add(b); m.add(h); m.add(hd); m.add(c); m.add(d); m.add(e); m.add(f); m.add(g); //設置窗體的長寬各為300、250 讓其顯示在左上方的300、250處 setBounds(300,250,300,250); m.setLayout(null); // a、b、c、d、e、f顯示在container容器中的位置坐標 a.setBounds(10,40,50,18); b.setBounds(10,80,50,18); h.setBounds(5,120,80,18); c.setBounds(60,40,200,18); d.setBounds(60,80,200,18); hd.setBounds(90,120,180,18); e.setBounds(110,160,60,30); f.setBounds(30,160,60,30); g.setBounds(190,160,60,30); g.addActionListener(new ActionListener() {//對返回按鈕添加監聽事件 @SuppressWarnings("deprecation") @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub new EnterScreen(); } }); f.addActionListener(new ActionListener() {//對確認按鈕添加監聽事件 @SuppressWarnings("deprecation") @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub new EnterScreen(); } }); e.addActionListener(new ActionListener() {//對確認按鈕添加監聽事件 @SuppressWarnings("deprecation") @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub new zhuce(); } }); } }
2.快速注冊界面截圖
三.驗證碼部分
1.驗證碼程序
import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; import javax.swing.JComponent; public class Yanzhencode extends JComponent implements MouseListener { private String codes; //自動生成的驗證碼 private int width, height = 40; //設置驗證碼高度、寬度 private int codesLength = 4; //設置代碼長度 private Random random = new Random(); //生成數字的方法 public Yanzhencode() { width = this.codesLength * 16 + (this.codesLength - 1) * 10; //根據驗證碼長度設置寬度 setPreferredSize(new Dimension(width, height)); //設置背景大小 setSize(width, height); //設置驗證碼長度和寬度 this.addMouseListener(this); setToolTipText("點擊可更換驗證碼"); } //得到生成的驗證碼 public int getCodesLength() { return codesLength; } //設置驗證碼的長度 public void setCodesLength(int codeLength) { if(codesLength < 4) { this.codesLength = 4; } else { this.codesLength = codeLength; } } public String getCode() { return codes; } //讓驗證碼產生隨機的顏色 public Color getRandColor(int min, int max) { if (min > 255) min = 255; if (max > 255) max = 255; int red = random.nextInt(max - min) + min; int green = random.nextInt(max - min) + min; int blue = random.nextInt(max - min) + min; return new Color(red, green, blue); } // 設置驗證碼具體的數字或字母是什么 protected String generateCode() { char[] codes = new char[this.codesLength]; for (int i = 0, len = codes.length; i < len; i++) { if (random.nextBoolean()) { codes[i] = (char) (random.nextInt(10) + 48); } else { codes[i] = (char) (random.nextInt(26) + 97); } } this.codes = new String(codes); return this.codes; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if(this.codes == null || this.codes.length() != this.codesLength) { //判斷生成的驗證碼是否為空或超出長度 this.codes = generateCode(); } width = this.codesLength * 16 + (this.codesLength - 1) * 10; super.setSize(width, height); //接口使用,驗證碼字體大小 super.setPreferredSize(new Dimension(width, height));//接口使用,驗證碼背景大小 Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25); //設置字體和字體大小 g.setFont(mFont); //設置對象 //繪制出驗證碼的背景的矩形輪廓 Graphics2D g2d = (Graphics2D) g; g2d.setColor(getRandColor(200, 250)); g2d.fillRect(0, 0, width, height); g2d.setColor(getRandColor(180, 200)); g2d.drawRect(0, 0, width - 1, height - 1); //繪制出驗證碼背景的線 int i = 0, len = 150; for (; i < len; i++) { int x = random.nextInt(width - 1); int y = random.nextInt(height - 1); int x1 = random.nextInt(width - 10) + 10; int y1 = random.nextInt(height - 4) + 4; g2d.setColor(getRandColor(180, 200)); g2d.drawLine(x, y, x1, y1); } //繪制出驗證碼的具體字母 i = 0; len = this.codesLength; FontMetrics fm = g2d.getFontMetrics(); int base = (height - fm.getHeight())/2 + fm.getAscent(); for(;i<len;i++) { int b = random.nextBoolean() ? 1 : -1; g2d.rotate(random.nextInt(10)*0.01*b); g2d.setColor(getRandColor(20, 130)); g2d.drawString(codes.charAt(i)+"", 16 * i + 10, base); } } //下一個驗證碼 public void nextCode() { generateCode(); repaint(); } @Override public void mouseClicked(MouseEvent e) { nextCode(); } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } }
2.驗證碼實現效果
點擊驗證碼圖片后,驗證碼會更換
--------------------------------------------------------------------------------------------------------------------------------------感謝觀看!