我用了半個(gè)小時(shí) 幫你寫了一個(gè)簡(jiǎn)單的驗(yàn)證用戶名和密碼登陸問題 別辜負(fù)我的好意 下面是代碼!(建好包和類 代碼粘過去就能用)
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了宜都免費(fèi)建站歡迎大家使用!
實(shí)體類 包entity
-------------------------------------------------------------
package entity;
/**
* 用戶實(shí)體類
* @author new
*
*/
public class Users {
private String name;//用戶名
private String pass;//用戶密碼
/**
* 空的構(gòu)造函數(shù) 用戶實(shí)力化 此類對(duì)象
*/
public Users(){
}
/**
* 構(gòu)造函數(shù) 接收用戶名和密碼
* @param name
* @param pass
*/
public Users(String name, String pass) {
this.name = name;
this.pass = pass;
}
/**
* 下面set和get方法就不用解釋了吧
* @return
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
數(shù)據(jù)庫類 包dao(我是模擬一下數(shù)據(jù)庫 沒有用到數(shù)據(jù)庫)
--------------------------------------------------------------
package dao;
import java.util.*;
import entity.Users;//導(dǎo)入實(shí)體類
/**
* 模擬數(shù)據(jù)庫 用戶DAO
* @author new
*
*/
public class UsersDAO {
private static Users users=new Users();
static
{
users.setName("tom");
users.setPass("jerry");
}
/**
* 根據(jù)姓名查找這個(gè)用戶 (模擬一下數(shù)據(jù)庫)
* @param name
* @return
*/
public Users findUserByName(String name)
{
if(name.equals(this.users.getName()))
{
return this.users;
}
return null;
}
}
業(yè)務(wù)類 包service (驗(yàn)證用戶名和密碼)
------------------------------------------------------------
package service;
import dao.UsersDAO;
import entity.Users;
/**
* 驗(yàn)證密碼 業(yè)務(wù)類
* @author new
*
*/
public class validatePass {
//實(shí)力化DAO對(duì)象
private UsersDAO us=new UsersDAO();
/**
* 驗(yàn)證輸入的密碼是否正確
* @param name
* @param pass
* @return
*/
public Users validate(String name,String pass)
{
Users user=null;
user=us.findUserByName(name);
//如果不為空 說明查到了
if(user!=null)
{
//用查詢出來對(duì)象的密碼和傳過來的密碼比較
if(user.getPass().equals(pass))
{
return user;
}
}
return null;
}
}
最后是測(cè)試test類 包test
----------------------------------------------------------
package test;
import entity.Users;
import service.validatePass;
/**
* 測(cè)試類
* @author new
*
*/
public class test {
/**
* main方法 用于測(cè)試
* @param args
*/
public static void main(String[] args)
{
//實(shí)例化業(yè)務(wù)類對(duì)象
validatePass v=new validatePass();
//用戶名和密碼
String name="tom";
String pass="jerry";
//開始驗(yàn)證
Users user=v.validate(name, pass);
if(user==null)
{
System.out.println("你輸入的用戶名或密碼錯(cuò)誤!");
}else
{
System.out.println("你已經(jīng)通過驗(yàn)證,成功登陸!");
}
}
}
首先你要在你的電腦上安裝jdk。你可以在后面鏈接地址下載適合你自己的版本(),如果這個(gè)鏈接過期了,請(qǐng)?jiān)谶@個(gè)首先找一找。
在你的電腦上配置java環(huán)境變量,主要是配置path和classpath。你可以百度java環(huán)境變量配置,可以找到很多java環(huán)境變量配置方法。配置完畢,可以在cmd窗口下用java -version來查看是否配置成功。如果顯示出java版本相關(guān)的信息表示配置成功,可以進(jìn)行下一步了。
編譯你的源代碼,cmd窗口下把路徑改變(cd)到你源代碼文件所在的路徑,然后用javac 源文件名編譯,例如javac Hello.java(需要注意的是源文件名需要是你文件public類的類名,如果你的文件有public類的話)。當(dāng)然你也可以不改變(cd)到源文件所在的路徑,你的文件就需要加上絕對(duì)路徑就可以了。例如:javac e:\src\Hello.java.
運(yùn)行你編譯好的文件,java Hello(需要注意運(yùn)行的時(shí)候沒有后綴.java或者.class),同樣你可以不改變路徑用絕對(duì)路徑運(yùn)行,例如:java e:\src\Hello.如果你的代碼中有窗口這樣的類似的圖形化界面,你就需要用javaw來運(yùn)行。
另外,你可以使用eclipse,NetBeans這樣的集成開發(fā)環(huán)境(IDE)來寫代碼,這樣方便很多。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame
extends JFrame {
JTextField text;
JLabel nowBomb, setBomb;
int BombNum, BlockNum; // 當(dāng)前雷數(shù),當(dāng)前方塊數(shù)
int rightBomb, restBomb, restBlock; // 找到的地雷數(shù),剩余雷數(shù),剩余方塊數(shù)
JButton start = new JButton(" 開始 ");
JPanel MenuPamel = new JPanel();
JPanel bombPanel = new JPanel();
Bomb[][] bombButton;
JPanel c;
BorderLayout borderLayout1 = new BorderLayout();
GridLayout gridLayout1 = new GridLayout();
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
c = (JPanel) getContentPane();
setTitle("掃雷");
c.setBackground(Color.WHITE);
MenuPamel.setBackground(Color.GRAY);
c.setLayout(borderLayout1);
setSize(new Dimension(600, 600));
setResizable(false);
BlockNum = 144;
BombNum = 10;
text = new JTextField("10 ", 3);
nowBomb = new JLabel("當(dāng)前雷數(shù)" + ":" + BombNum);
setBomb = new JLabel("設(shè)置地雷數(shù)");
start.addActionListener(new Frame1_start_actionAdapter(this));
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel, java.awt.BorderLayout.SOUTH);
bombPanel.setLayout(gridLayout1);
gridLayout1.setColumns( (int) Math.sqrt(BlockNum));
gridLayout1.setRows( (int) Math.sqrt(BlockNum));
bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (int) Math.sqrt(BlockNum)];
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j] = new Bomb(i, j);
//bombButton[i][j].setSize(10, 10);
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//設(shè)置字體大小
bombButton[i][j].setForeground(Color.white);
bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this));
bombButton[i][j].addActionListener(new Bomb_actionAdapter(this));
bombPanel.add(bombButton[i][j]);
}
}
c.add(bombPanel, java.awt.BorderLayout.CENTER);
startBomb();
}
/* 開始按鈕 */
public void start_actionPerformed(ActionEvent e) {
int num=Integer.parseInt(text.getText().trim());
if (num = 5 num 50) {
BombNum = num;
startBomb();
}
else if (num 5) {
JOptionPane.showMessageDialog(null, "您設(shè)置的地雷數(shù)太少了,請(qǐng)重設(shè)!", "錯(cuò)誤",
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
else {
JOptionPane.showMessageDialog(null, "您設(shè)置的地雷數(shù)太多了,請(qǐng)重設(shè)!", "錯(cuò)誤",
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
}
/* 開始,布雷 */
public void startBomb() {
nowBomb.setText("當(dāng)前雷數(shù)" + ":" + BombNum);
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j].isBomb = false;
bombButton[i][j].isClicked = false;
bombButton[i][j].isRight = false;
bombButton[i][j].BombFlag = 0;
bombButton[i][j].BombRoundCount = 9;
bombButton[i][j].setEnabled(true);
bombButton[i][j].setText("");
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//設(shè)置字體大小
bombButton[i][j].setForeground(Color.BLUE);
rightBomb = 0;
restBomb = BombNum;
restBlock = BlockNum - BombNum;
}
}
for (int i = 0; i BombNum; ) {
int x = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
int y = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
if (bombButton[x][y].isBomb != true) {
bombButton[x][y].isBomb = true;
i++;
}
}
CountRoundBomb();
}
/* 計(jì)算方塊周圍雷數(shù) */
public void CountRoundBomb() {
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
int count = 0;
// 當(dāng)需要檢測(cè)的單元格本身無地雷的情況下,統(tǒng)計(jì)周圍的地雷個(gè)數(shù)
if (bombButton[i][j].isBomb != true) {
for (int x = i - 1; x i + 2; x++) {
for (int y = j - 1; y j + 2; y++) {
if ( (x = 0) (y = 0)
(x ( (int) Math.sqrt(BlockNum)))
(y ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == true) {
count++;
}
}
}
}
bombButton[i][j].BombRoundCount = count;
}
}
}
}
/* 是否挖完了所有的雷 */
public void isWin() {
restBlock = BlockNum - BombNum;
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isClicked == true) {
restBlock--;
}
}
}
if (rightBomb == BombNum || restBlock == 0) {
JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您勝利了!", "勝利",
JOptionPane.INFORMATION_MESSAGE);
startBomb();
}
}
/** 當(dāng)選中的位置為空,則翻開周圍的地圖* */
public void isNull(Bomb ClickedButton) {
int i, j;
i = ClickedButton.num_x;
j = ClickedButton.num_y;
for (int x = i - 1; x i + 2; x++) {
for (int y = j - 1; y j + 2; y++) {
if ( ( (x != i) || (y != j)) (x = 0) (y = 0)
(x ( (int) Math.sqrt(BlockNum)))
(y ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == false
bombButton[x][y].isClicked == false
bombButton[x][y].isRight == false) {
turn(bombButton[x][y]);
}
}
}
}
}
/* 翻開 */
public void turn(Bomb ClickedButton) {
ClickedButton.setEnabled(false);
ClickedButton.isClicked = true;
if (ClickedButton.BombRoundCount 0) {
ClickedButton.setText(ClickedButton.BombRoundCount + "");
}
else {
isNull(ClickedButton);
}
}
/* 左鍵點(diǎn)擊 */
public void actionPerformed(ActionEvent e) {
if ( ( (Bomb) e.getSource()).isClicked == false
( (Bomb) e.getSource()).isRight == false) {
if ( ( (Bomb) e.getSource()).isBomb == false) {
turn( ( (Bomb) e.getSource()));
isWin();
}
else {
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isBomb == true) {
bombButton[i][j].setText("b");
}
}
}
( (Bomb) e.getSource()).setForeground(Color.RED);
( (Bomb) e.getSource()).setFont(new Font("", Font.BOLD, 20));
( (Bomb) e.getSource()).setText("X");
JOptionPane.showMessageDialog(this, "你踩到地雷了,按確定重來", "踩到地雷", 2);
startBomb();
}
}
}
/* 右鍵點(diǎn)擊 */
public void mouseClicked(MouseEvent e) {
Bomb bombSource = (Bomb) e.getSource();
boolean right = SwingUtilities.isRightMouseButton(e);
if ( (right == true) (bombSource.isClicked == false)) {
bombSource.BombFlag = (bombSource.BombFlag + 1) % 3;
if (bombSource.BombFlag == 1) {
if (restBomb 0) {
bombSource.setForeground(Color.RED);
bombSource.setText("F");
bombSource.isRight = true;
restBomb--;
}
else {
bombSource.BombFlag = 0;
}
}
else if (bombSource.BombFlag == 2) {
restBomb++;
bombSource.setText("Q");
bombSource.isRight = false;
}
else {
bombSource.setText("");
}
if (bombSource.isBomb == true) {
if (bombSource.BombFlag == 1) {
rightBomb++;
}
else if (bombSource.BombFlag == 2) {
rightBomb--;
}
}
nowBomb.setText("當(dāng)前雷數(shù)" + ":" + restBomb);
isWin();
}
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
}
}
class Frame1_start_actionAdapter
implements ActionListener {
private Frame adaptee;
Frame1_start_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.start_actionPerformed(e);
}
}
////////////////////////////
class Bomb
extends JButton {
int num_x, num_y; // 第幾號(hào)方塊
int BombRoundCount; // 周圍雷數(shù)
boolean isBomb; // 是否為雷
boolean isClicked; // 是否被點(diǎn)擊
int BombFlag; // 探雷標(biāo)記
boolean isRight; // 是否點(diǎn)擊右鍵
public Bomb(int x, int y) {
num_x = x;
num_y = y;
BombFlag = 0;
BombRoundCount = 9;
isBomb = false;
isClicked = false;
isRight = false;
}
}
class Bomb_actionAdapter
implements ActionListener {
private Frame adaptee;
Bomb_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.actionPerformed(e);
}
}
class Bomb_mouseAdapter
extends MouseAdapter {
private Frame adaptee;
Bomb_mouseAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.mouseClicked(e);
}
}