你這個不用數(shù)據(jù)庫真的是有點難搞
創(chuàng)新互聯(lián)專注于彝良企業(yè)網(wǎng)站建設(shè),響應式網(wǎng)站開發(fā),商城建設(shè)。彝良網(wǎng)站建設(shè)公司,為彝良等地區(qū)提供建站服務。全流程按需策劃,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務
我寫了個用集合存儲的,你看看,能否幫上你
java.util.ListString?list?=?new?ArrayListString();
list.add("qq=123");//存儲的時候用(用戶名=密碼)的形式
list.add("ww=456");
String?username?=?"qq";
String?password?=?"123";
for?(int?i?=?0;?i??list.size();?i++)?{
String?num?=?username?+"="+password;
if(num.equals(list.get(i))){
System.out.println("登錄成功");
break;
}
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
private JFrame frame = new JFrame("登錄");
private Container c = frame.getContentPane();
private JTextField username = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton ok = new JButton("確定");
private JButton cancel = new JButton("取消");
public Login(){
frame.setSize(300,200);
c.setLayout(new BorderLayout());
initFrame();
frame.setVisible(true);
}
private void initFrame() {
//頂部
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
titlePanel.add(new JLabel("系統(tǒng)管理員登錄"));
c.add(titlePanel,"North");
//中部表單
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
JLabel l1 = new JLabel("用戶名:");
l1.setBounds(50, 20, 50, 20);
JLabel l2 = new JLabel("密 碼:");
l2.setBounds(50, 60, 50, 20);
fieldPanel.add(l1);
fieldPanel.add(l2);
username.setBounds(110,20,120,20);
password.setBounds(110,60,120,20);
fieldPanel.add(username);
fieldPanel.add(password);
c.add(fieldPanel,"Center");
//底部按鈕
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(ok);
buttonPanel.add(cancel);
c.add(buttonPanel,"South");
}
public static void main(String[] args){
new Login();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test_Login extends javax.swing.JFrame {
private JPanel jPanel1;
private JButton bntLogin;
private JButton bntCannel;
private JPasswordField pwd;
private JTextField username;
private JLabel jLabel2;
private JLabel jLabel1;
public static void main(String[] args) {
Test_Login inst = new Test_Login();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
public Test_Login() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
jLabel1 = new JLabel();
jPanel1.add(jLabel1);
jLabel1.setText("用戶名");
jLabel1.setBounds(45, 30, 75, 25);
}
{
jLabel2 = new JLabel();
jPanel1.add(jLabel2);
jLabel2.setText("密碼");
jLabel2.setBounds(45, 75, 55, 15);
}
{
username = new JTextField();
jPanel1.add(username);
username.setBounds(100, 30, 140, 25);
}
{
pwd = new JPasswordField();
jPanel1.add(pwd);
pwd.setBounds(100, 70, 140, 25);
}
{
bntLogin = new JButton();
jPanel1.add(bntLogin);
bntLogin.setText("登陸");
bntLogin.setBounds(40, 120, 60, 30);
bntLogin.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (username.getText().equals("lisong")
pwd.getText().equals("lisong")) {
JOptionPane.showMessageDialog(Test_Login.this,
"登錄成功");
} else {
JOptionPane.showMessageDialog(Test_Login.this,
"登錄失敗");
}
}
});
bntCannel = new JButton();
jPanel1.add(bntCannel);
bntCannel.setText("取消");
bntCannel.setBounds(180, 120, 60, 30);
bntCannel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
}
}
pack();
setSize(300, 215);
} catch (Exception e) {
e.printStackTrace();
}
}
}
概述
具體框架使用jframe,文本框組件:JTextField;密碼框組件:JPasswordField;標簽組件:JLabel;復選框組件:JCheckBox;單選框組件:JRadioButton;按鈕組件JButton。
登錄界面:
代碼實例
import javax.swing.*;
import java.awt.*; ? //導入必要的包
public class denglu extends JFrame{
JTextField jTextField ;//定義文本框組件
JPasswordField jPasswordField;//定義密碼框組件
JLabel jLabel1,jLabel2;
JPanel jp1,jp2,jp3;
JButton jb1,jb2; //創(chuàng)建按鈕
public denglu(){
jTextField = new JTextField(12);
jPasswordField = new JPasswordField(13);
jLabel1 = new JLabel("用戶名");
jLabel2 = new JLabel("密碼");
jb1 = new JButton("確認");
jb2 = new JButton("取消");
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
//設(shè)置布局
this.setLayout(new GridLayout(3,1));
jp1.add(jLabel1);
jp1.add(jTextField);//第一塊面板添加用戶名和文本框
jp2.add(jLabel2);
jp2.add(jPasswordField);//第二塊面板添加密碼和密碼輸入框
jp3.add(jb1);
jp3.add(jb2); //第三塊面板添加確認和取消
// ? ? ? ?jp3.setLayout(new FlowLayout()); ?//因為JPanel默認布局方式為FlowLayout,所以可以注銷這段代碼.
this.add(jp1);
this.add(jp2);
this.add(jp3); ?//將三塊面板添加到登陸框上面
//設(shè)置顯示
this.setSize(300, 200);
//this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("登陸");
}
public static void main(String[] args){
new denglu();
}
}
拓展內(nèi)容
java swing包
Swing 是一個為Java設(shè)計的GUI工具包。
Swing是JAVA基礎(chǔ)類的一部分。
Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。
Swing提供許多比AWT更好的屏幕顯示元素。它們用純Java寫成,所以同Java本身一樣可以跨平臺運行,這一點不像AWT。它們是JFC的一部分。它們支持可更換的面板和主題(各種操作系統(tǒng)默認的特有主題),然而不是真的使用原生平臺提供的設(shè)備,而是僅僅在表面上模仿它們。這意味著你可以在任意平臺上使用JAVA支持的任意面板。輕量級組件的缺點則是執(zhí)行速度較慢,優(yōu)點就是可以在所有平臺上采用統(tǒng)一的行為。
概念解析:
JFrame?– java的GUI程序的基本思路是以JFrame為基礎(chǔ),它是屏幕上window的對象,能夠最大化、最小化、關(guān)閉。
JPanel?– Java圖形用戶界面(GUI)工具包swing中的面板容器類,包含在javax.swing 包中,可以進行嵌套,功能是對窗體中具有相同邏輯功能的組件進行組合,是一種輕量級容器,可以加入到JFrame窗體中。。
JLabel?– JLabel 對象可以顯示文本、圖像或同時顯示二者。可以通過設(shè)置垂直和水平對齊方式,指定標簽顯示區(qū)中標簽內(nèi)容在何處對齊。默認情況下,標簽在其顯示區(qū)內(nèi)垂直居中對齊。默認情況下,只顯示文本的標簽是開始邊對齊;而只顯示圖像的標簽則水平居中對齊。
JTextField?–一個輕量級組件,它允許編輯單行文本。
JPasswordField?– 允許我們輸入了一行字像輸入框,但隱藏星號(*) 或點創(chuàng)建密碼(密碼)
JButton?– JButton 類的實例。用于創(chuàng)建按鈕類似實例中的 "Login"。
import?java.awt.Color;
import?java.awt.Font;
import?java.awt.Graphics;
import?java.awt.image.BufferedImage;
import?java.io.IOException;
import?java.io.OutputStream;
import?java.util.Random;
import?javax.imageio.ImageIO;
/*生成驗證碼圖片
*/
public?class?MakeCertPic?{
//驗證碼圖片中可以出現(xiàn)的字符集,可以根據(jù)需要修改
private?char?mapTable[]={
'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','0','1','2','3',
'4','5','6','7','8','9'
};
/*?功能:生成彩色驗證碼圖片
參數(shù)wedth為生成圖片的寬度,參數(shù)height為生成圖片的高度,參數(shù)os為頁面的輸出流
*/
public?String?getCertPic(int?width,int?height,OutputStream?os){
if(width=0)
width=60;
if(height=0)
height=20;
BufferedImage?image=?new?BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//獲取圖形上下文
Graphics?g?=?image.getGraphics();
//設(shè)定背景顏色
g.setColor(new?Color(0xDCDCDC));
g.fillRect(0,0,width,height);
//畫邊框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
//隨機產(chǎn)生的驗證碼
String?strEnsure?=?"";
//4代表4為驗證碼,如果要產(chǎn)生更多位的驗證碼,則加大數(shù)值
for(int?i?=?0;i4;++i){
strEnsure?+=?mapTable[(int)?(mapTable.length*Math.random())];
}
//將認證碼顯示到圖像中,如果要生成更多位的驗證碼,增加drawString語句
g.setColor(Color.black);
g.setFont(new?Font("Atlantic?Inline",Font.PLAIN,18));
String?str?=?strEnsure.substring(0,1);
g.drawString(str,8,17);
str?=?strEnsure.substring(1,2);
g.drawString(str,?20,?15);
str?=?strEnsure.substring(2,3);
g.drawString(str,?35,?18);
str?=?strEnsure.substring(3,4);
g.drawString(str,?45,?15);
//隨機產(chǎn)生15個干擾點
Random?rand?=?new?Random();
for(int?i=0;?i10;?i++){
int?x?=?rand.nextInt(width);
int?y?=?rand.nextInt(height);
g.drawOval(x,y,1,1);
}
//釋放圖形上下文
g.dispose();
try{
//輸出圖形到頁面
ImageIO.write(image,?"JPEG",?os);
}catch?(IOException?e){
return?"";
}
return?strEnsure;
}
}
makeCertPic.jsp頁面用于調(diào)用生成驗證碼圖片的JavaBean,并在客戶端顯示,源代碼如下:
%@page?contentType="image/jpeg"?%%@page?language="java"?pageEncoding="utf-8"%jsp:useBean?id="image"?scope="page"?class="securityCode.pic.MakeCertPic"/%
String?str?=?image.getCertPic(0,0,response.getOutputStream());
//將驗證碼存入session中
session.setAttribute("certCode",str);
%
下邊是登錄頁面:
%@?page?language="java"?contentType="text/html;?charset=UTF-8"
pageEncoding="UTF-8"%
!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?""
html
head
meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8"
title驗證碼測試登錄頁面/title
script?type="text/javascript"
function?changeimg()
{
var?myimg?=?document.getElementById("code");?
now?=?new?Date();?
myimg.src="makeCertPic.jsp?code="+now.getTime();
}?
/script
/head
body
center
form?action="loginCheck.jsp"?method="post"?/
用戶名:input?type="text"?name="username"?/br
密nbsp;nbsp;碼:input?type="password"?name="password"/br
nbsp;驗證碼:input?type="text"?name="certCode"/
img?id="code"?src="makeCertPic.jsp"a?href="javascript:changeimg()"看不清,換一張?/abr
input?type="submit"?value="登錄"/
/form
/center
/body
/html
網(wǎng)站題目:java登錄驗證的源代碼,java 登錄驗證
新聞來源:http://chinadenli.net/article20/dsshijo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、響應式網(wǎng)站、定制網(wǎng)站、微信公眾號、網(wǎng)站排名、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)