java登陸
1. 用java編寫一個登陸系統。
第一個java文件LogoingDemo.java
importjava.util.Scanner;
publicclassLogoingDemo{
publicstaticvoidmain(String[]args){
System.out.println("請輸入用戶名");
Scannersc=newScanner(System.in);
Stringname=sc.nextLine();
System.out.println("請輸入密碼");
Stringpsw=sc.nextLine();
sc.close();
CheckDemocd=newCheckDemo(name,psw);//用戶名和密碼傳入驗證類
booleanbo=cd.check();//調用方法進行驗證
if(bo){
System.out.println("登錄成功");
}else{
System.out.println("登錄失敗:提示用戶名admin密碼123");
}
}
}
第二個java文件CheckDemo.java
publicclassCheckDemo{
publicStringname;
publicStringpsw;
publicCheckDemo(Stringname,Stringpsw){//構造器
this.name=name;
this.psw=psw;
}
publicbooleancheck(){
//用戶名密碼不能為空.用戶名=admin密碼=123
if(name!=null&&psw!=null&&name.equals("admin")&&psw.equals("123")){
returntrue;
}
returnfalse;
}
}
效果
請輸入用戶名
admin
請輸入密碼
123
登錄成功
------------------------------------
請輸入用戶名
add
請輸入密碼
123
登錄失敗:提示用戶名admin密碼123
2. 如何用java做登錄界面
import javax.swing.JFrame;//框架
import javax.swing.JPanel;//面板
import javax.swing.JButton;//按鈕
import javax.swing.JLabel;//標簽
import javax.swing.JTextField;//文本框
import java.awt.Font;//字體
import java.awt.Color;//顏色
import javax.swing.JPasswordField;//密碼框
import java.awt.event.ActionListener;//事件監聽
import java.awt.event.ActionEvent;//事件處理
import javax.swing.JOptionPane;//消息窗口public class UserLogIn extends JFrame{
public JPanel pnluser;
public JLabel lbluserLogIn;
public JLabel lbluserName;
public JLabel lbluserPWD;
public JTextField txtName;
public JPasswordField pwdPwd;
public JButton btnSub;
public JButton btnReset;
public UserLogIn(){
pnluser = new JPanel();
lbluserLogIn = new JLabel();
lbluserName = new JLabel();
lbluserPWD = new JLabel();
txtName = new JTextField();
pwdPwd = new JPasswordField();
btnSub = new JButton();
btnReset = new JButton();
userInit();
}
public void userInit(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置關閉框架的同時結束程序
this.setSize(300,200);//設置框架大小為長300,寬200
this.setResizable(false);//設置框架不可以改變大小
this.setTitle("用戶登錄");//設置框架標題
this.pnluser.setLayout(null);//設置面板布局管理
this.pnluser.setBackground(Color.cyan);//設置面板背景顏色
this.lbluserLogIn.setText("用戶登錄");//設置標簽標題
this.lbluserLogIn.setFont(new Font("宋體",Font.BOLD | Font.ITALIC,14));//設置標簽字體
this.lbluserLogIn.setForeground(Color.RED);//設置標簽字體顏色
this.lbluserName.setText("用戶名:");
this.lbluserPWD.setText("密 碼:");
this.btnSub.setText("登錄");
this.btnReset.setText("重置");
this.lbluserLogIn.setBounds(120,15,60,20);//設置標簽x坐標120,y坐標15,長60,寬20
this.lbluserName.setBounds(50,55,60,20);
this.lbluserPWD.setBounds(50,85,60,25);
this.txtName.setBounds(110,55,120,20);
this.pwdPwd.setBounds(110,85,120,20);
this.btnSub.setBounds(85,120,60,20);
this.btnSub.addActionListener(new ActionListener()//匿名類實現ActionListener介面
{
public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e);
}
}
);
this.btnReset.setBounds(155,120,60,20);
this.btnReset.addActionListener(new ActionListener()//匿名類實現ActionListener介面
{
public void actionPerformed(ActionEvent e){
btnreset_ActionEvent(e);
}
}
);
this.pnluser.add(lbluserLogIn);//載入標簽到面板
this.pnluser.add(lbluserName);
this.pnluser.add(lbluserPWD);
this.pnluser.add(txtName);
this.pnluser.add(pwdPwd);
this.pnluser.add(btnSub);
this.pnluser.add(btnReset);
this.add(pnluser);//載入面板到框架
this.setVisible(true);//設置框架可顯
}
public void btnsub_ActionEvent(ActionEvent e){
String name = txtName.getText();
String pwd = String.valueOf(pwdPwd.getPassword());
if(name.equals("")){
JOptionPane.showMessageDialog(null,"賬號不能為空","錯誤",JOptionPane.ERROR_MESSAGE);
return;
}else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"密碼不能為空","錯誤",JOptionPane.ERROR_MESSAGE);
return;
}else if(true){
this.dispose();
}else{
JOptionPane.showMessageDialog(null,"賬號或密碼錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
return;
}
}
public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("");
pwdPwd.setText("");
}
public static void main(String[] args){
new UserLogIn();
}
}
3. java怎樣實現登錄驗證
1.打開編程工具:
打開java編程的界面,採用的是eclipse軟體;
4. java編寫用戶登錄
代碼發給你,如果什麼都不懂的話,估計你也難以運行成功,而且圖片也沒有,要自己會改就能運行了。這些代碼希望能幫到你!
資料庫文件:
create database hotel_Flyer
go
use hotel_Flyer
create table userInfo(
id int primary key identity(1,1),
userName varchar(20),
password varchar(10),
realName nvarchar(20),
sex nvarchar(2),
age int,
favorite nvarchar(100),
telNum varchar(12),
email varchar(50),
address nvarchar(200)
)
go
insert into userInfo(userName,password) values('李連傑','123')
insert into userInfo(userName,password) values('成龍','123')
insert into userInfo(userName,password) values('甄子丹','123')
insert into userInfo(userName,password) values('趙文卓','123')
insert into userInfo(userName,password) values('吳京','123')
insert into userInfo(userName,password) values('周星馳','123')
insert into userInfo(userName,password) values('劉德華','123')
載入驅動類:
public class ConTosql {
Connection con=null;
public Connection getConnection() throws Exception{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con= DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=hotel_Flyer", "sa", "sa");
return con;
}
}
連接資料庫類:
public class DengluUC {
Connection con=null;
PreparedStatement stm=null;
ResultSet rs=null;
public boolean checkUser(String user,String pwd){
boolean tof=false;
try {
ConToSQL cts=new ConToSQL();
con=cts.getConnection();
stm=con.prepareStatement("select password from userInfo where userName=?");
stm.setString(1, user);
rs=stm.executeQuery();
if(rs.next()){
if(rs.getString(1).equals(pwd)){
tof= true;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} /*finally{
try {
rs.close();
stm.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return tof;
}*/
return tof;
}
}
登錄類代碼:
package com.hbsoft.hotel_Flyer.login;
import com.hbsoft.hotel_Flyer.usermanagerForm.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.text.html.Option;
import com.hbsoft.hotel_Flyer.proc.Process;
import java.awt.Point;
import java.sql.*;
import com.hbsoft.hotel_Flyer.database.*;
public class Denglu {
private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="61,26"
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JComboBox jComboBox = null;
private JLabel jL4 = null;
private JLabel jLabel1 = null;
private JPasswordField jPasswordField = null;
private JButton jB1 = null;
private JButton jB2 = null;
private JComboBox jComboBox1 = null;
private JLabel jLabel = null;
private JLabel jLabel2 = null;
private Connection con=null; // @jve:decl-index=0:
private Statement sts=null;
private ResultSet rs=null;
private DatabaseMetaData dmd=null;
private int rowCount=0;
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
public JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame("系統登陸");
jFrame.setSize(new Dimension(854, 539));
jFrame.setLocation(new Point(100, 100));
jFrame.setContentPane(getJContentPane());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setResizable(false);
}
return jFrame;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(3, 4, 845, 503));
jLabel2.setIcon(new ImageIcon("E:/java\uff088\uff09\u73ed\u5468\u67ab/\u7b2c\u5341\u7ec4\u5de5\u4f5c\u7ad9/hotel_Flyer/\u767b\u5f55\u80cc\u666f.jpg"));
jLabel2.setText("");
jLabel = new JLabel();
jLabel.setText("");
jLabel.setIcon(new ImageIcon(getClass().getResource("/com/hbsoft/hotel_Flyer/img/\u767b\u5f55\u80cc\u666f.jpg")));
jLabel.setBounds(new Rectangle(-1, 1, 851, 509));
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(438, 141, 363, 231));
jLabel1.setText("");
jL4 = new JLabel();
jL4.setBounds(new Rectangle(484, 341, 289, 28));
jL4.setHorizontalAlignment(SwingConstants.CENTER);
Timer t =new Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent e) {
Date d=new Date();
SimpleDateFormat df = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
jL4.setText(df.format(d));
}
});
t.start();
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setBorder(BorderFactory.createLineBorder(Color.cyan, 2));
jContentPane.setFont(new Font("\u96b6\u4e66", Font.PLAIN, 14));
jContentPane.add(getJPanel(), null);
jContentPane.add(jL4, null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJPasswordField(), null);
jContentPane.add(getJB1(), null);
jContentPane.add(getJB2(), null);
jContentPane.add(getJComboBox1(), null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel2, null);
jContentPane.add(jLabel, null);
}
return jContentPane;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.setBounds(new Rectangle(0, 0, 415, 0));
}
return jPanel;
}
/**
* This method initializes jB1
*
* @return javax.swing.JButton
*/
void ThisDispose(){
this.getJFrame().dispose();
}
/**
* This method initializes jPasswordField
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getJPasswordField() {
if (jPasswordField == null) {
jPasswordField = new JPasswordField();
jPasswordField.setBounds(new Rectangle(570, 263, 201, 29));
}
return jPasswordField;
}
/**
* This method initializes jB1
*
* @return javax.swing.JButton
*/
/*void ThisdDispose(){
this.getJFrame().dispose();
}*/
private JButton getJB1() {
if (jB1 == null) {
jB1 = new JButton();
jB1.setBounds(new Rectangle(489, 313, 103, 35));
jB1.setText("登陸");
jB1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(new DengluUC().checkUser(jComboBox1.getSelectedItem().toString(), new String(jPasswordField.getPassword()))){
//message.setText("登錄成功");
//message.setForeground(new Color(255,0,0));
new Process((String)jComboBox1.getSelectedItem()).setVisible(true);
ThisDispose();
}else{
if(new String(jPasswordField.getPassword()).length()==0){
JOptionPane.showMessageDialog(null, "密碼不能為空");
}else{
JOptionPane.showMessageDialog(null, "密碼不正確,請重新輸入");
}
}
}
});
}
return jB1;
}
/**
* This method initializes jB2
*
* @return javax.swing.JButton
*/
private JButton getJB2() {
if (jB2 == null) {
jB2 = new JButton();
jB2.setBounds(new Rectangle(642, 312, 103, 35));
jB2.setText("退出");
jB2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return jB2;
}
/**
* This method initializes jComboBox1
*
* @return javax.swing.JComboBox
*/
public void getResultSet(){
try{
con=new ConToSQL().getConnection();
sts=con.createStatement();
rs=sts.executeQuery("select * from userInfo");
}catch(Exception e){
e.printStackTrace();
}
}
public int getRowCount(){
try {
rs.last();
if (rs.isLast()) {
rowCount = rs.getRow();
}
} catch (Exception e) {
e.printStackTrace();
}
return rowCount;
}
public void closeData(){
try{
rs.close();
sts.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
private JComboBox getJComboBox1() {
getResultSet();
String [] arr=new String[7];
int i=0;
try {
while (rs.next()) {
if(i<arr.length){
arr[i] = rs.getString(2);
i++;}
}
} catch (Exception e) {
e.printStackTrace();
}
if (jComboBox1 == null) {
jComboBox1 = new JComboBox(arr);
closeData();
jComboBox1.setBounds(new Rectangle(569, 218, 201, 29));
}
return jComboBox1;
}
public static void main(String [] args) throws Exception{
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");
javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");
new Denglu().getJFrame().setVisible(true);
}
}
5. Java中登陸時如何實現的
如果時java web開發的話,那就是跟資料庫掛鉤了。用戶輸入用戶名和密碼,然後後台程序和資料庫保存的用戶名和密碼進行比較,如果成功,就進入允許的界面,如果不成功,重新回到登錄界面,或者返回網站首頁。
希望能幫到你!!!
6. java語言實現用戶注冊和登錄
//這個是我寫的,裡面有連接資料庫的部分。你可以拿去參考一下
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class LoginFrm extends JFrame implements ActionListener// throws Exception
{
JLabel lbl1 = new JLabel("用戶名:");
JLabel lbl2 = new JLabel("密碼:");
JTextField txt = new JTextField(5);
JPasswordField pf = new JPasswordField();
JButton btn1 = new JButton("確定");
JButton btn2 = new JButton("取消");
public LoginFrm() {
this.setTitle("登陸");
JPanel jp = (JPanel) this.getContentPane();
jp.setLayout(new GridLayout(3, 2, 5, 5));
jp.add(lbl1);
jp.add(txt);
jp.add(lbl2);
jp.add(pf);
jp.add(btn1);
jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn1) {
try {
Class.forName("com.mysql.jdbc.Driver");// mysql資料庫
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/Car_zl", "root", "1");// 資料庫名為Car_zl,密碼為1
System.out.println("com : "+ con);
Statement cmd = con.createStatement();
String sql = "select * from user where User_ID='"
+ txt.getText() + "' and User_ps='"
+ pf.getText() + "'" ;
ResultSet rs = cmd
.executeQuery(sql);// 表名為user,user_ID和User_ps是存放用戶名和密碼的欄位名
if (rs.next()) {
JOptionPane.showMessageDialog(null, "登陸成功!");
} else
JOptionPane.showMessageDialog(null, "用戶名或密碼錯誤!");
} catch (Exception ex) {
}
if (ae.getSource() == btn2) {
System.out.println("1111111111111");
//txt.setText("");
//pf.setText("");
System.exit(0);
}
}
}
public static void main(String arg[]) {
JFrame.(true);
LoginFrm frm = new LoginFrm();
frm.setSize(400, 200);
frm.setVisible(true);
}
}
7. java實現簡單登錄界面
自己寫的比較規范的代碼,都有注釋:
import javax.swing.JFrame;//框架
import javax.swing.JPanel;//面板
import javax.swing.JButton;//按鈕
import javax.swing.JLabel;//標簽
import javax.swing.JTextField;//文本框
import java.awt.Font;//字體
import java.awt.Color;//顏色
import javax.swing.JPasswordField;//密碼框
import java.awt.event.ActionListener;//事件監聽
import java.awt.event.ActionEvent;//事件處理
import javax.swing.JOptionPane;//消息窗口
public class UserLogIn extends JFrame{
public JPanel pnluser;
public JLabel lbluserLogIn;
public JLabel lbluserName;
public JLabel lbluserPWD;
public JTextField txtName;
public JPasswordField pwdPwd;
public JButton btnSub;
public JButton btnReset;
public UserLogIn(){
pnluser = new JPanel();
lbluserLogIn = new JLabel();
lbluserName = new JLabel();
lbluserPWD = new JLabel();
txtName = new JTextField();
pwdPwd = new JPasswordField();
btnSub = new JButton();
btnReset = new JButton();
userInit();
}
public void userInit(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置關閉框架的同時結束程序
this.setSize(300,200);//設置框架大小為長300,寬200
this.setResizable(false);//設置框架不可以改變大小
this.setTitle("用戶登錄");//設置框架標題
this.pnluser.setLayout(null);//設置面板布局管理
this.pnluser.setBackground(Color.cyan);//設置面板背景顏色
this.lbluserLogIn.setText("用戶登錄");//設置標簽標題
this.lbluserLogIn.setFont(new Font("宋體",Font.BOLD | Font.ITALIC,14));//設置標簽字體
this.lbluserLogIn.setForeground(Color.RED);//設置標簽字體顏色
this.lbluserName.setText("用戶名:");
this.lbluserPWD.setText("密 碼:");
this.btnSub.setText("登錄");
this.btnReset.setText("重置");
this.lbluserLogIn.setBounds(120,15,60,20);//設置標簽x坐標120,y坐標15,長60,寬20
this.lbluserName.setBounds(50,55,60,20);
this.lbluserPWD.setBounds(50,85,60,25);
this.txtName.setBounds(110,55,120,20);
this.pwdPwd.setBounds(110,85,120,20);
this.btnSub.setBounds(85,120,60,20);
this.btnSub.addActionListener(new ActionListener()//匿名類實現ActionListener介面
{
public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e);
}
}
);
this.btnReset.setBounds(155,120,60,20);
this.btnReset.addActionListener(new ActionListener()//匿名類實現ActionListener介面
{
public void actionPerformed(ActionEvent e){
btnreset_ActionEvent(e);
}
}
);
this.pnluser.add(lbluserLogIn);//載入標簽到面板
this.pnluser.add(lbluserName);
this.pnluser.add(lbluserPWD);
this.pnluser.add(txtName);
this.pnluser.add(pwdPwd);
this.pnluser.add(btnSub);
this.pnluser.add(btnReset);
this.add(pnluser);//載入面板到框架
this.setVisible(true);//設置框架可顯
}
public void btnsub_ActionEvent(ActionEvent e){
String name = txtName.getText();
String pwd = String.valueOf(pwdPwd.getPassword());
if(name.equals("")){
JOptionPane.showMessageDialog(null,"賬號不能為空","錯誤",JOptionPane.ERROR_MESSAGE);
return;
}else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"密碼不能為空","錯誤",JOptionPane.ERROR_MESSAGE);
return;
}else if(true){
this.dispose();
}else{
JOptionPane.showMessageDialog(null,"賬號或密碼錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
return;
}
}
public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("");
pwdPwd.setText("");
}
public static void main(String[] args){
new UserLogIn();
}
}
8. 用java寫一個登陸界面代碼。
概述
具體框架使用jframe,文本框組件:JTextField;密碼框組件:JPasswordField;標簽組件:JLabel;復選框組件:JCheckBox;單選框組件:JRadioButton;按鈕組件JButton。
登錄界面:
Swing 是一個為Java設計的GUI工具包。
Swing是JAVA基礎類的一部分。
Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。
Swing提供許多比AWT更好的屏幕顯示元素。它們用純Java寫成,所以同Java本身一樣可以跨平台運行,這一點不像AWT。它們是JFC的一部分。它們支持可更換的面板和主題(各種操作系統默認的特有主題),然而不是真的使用原生平台提供的設備,而是僅僅在表面上模仿它們。這意味著你可以在任意平台上使用JAVA支持的任意麵板。輕量級組件的缺點則是執行速度較慢,優點就是可以在所有平台上採用統一的行為。
概念解析:
JFrame– java的GUI程序的基本思路是以JFrame為基礎,它是屏幕上window的對象,能夠最大化、最小化、關閉。
JPanel– Java圖形用戶界面(GUI)工具包swing中的面板容器類,包含在javax.swing 包中,可以進行嵌套,功能是對窗體中具有相同邏輯功能的組件進行組合,是一種輕量級容器,可以加入到JFrame窗體中。。
JLabel– JLabel 對象可以顯示文本、圖像或同時顯示二者。可以通過設置垂直和水平對齊方式,指定標簽顯示區中標簽內容在何處對齊。默認情況下,標簽在其顯示區內垂直居中對齊。默認情況下,只顯示文本的標簽是開始邊對齊;而只顯示圖像的標簽則水平居中對齊。
JTextField–一個輕量級組件,它允許編輯單行文本。
JPasswordField– 允許我們輸入了一行字像輸入框,但隱藏星號(*) 或點創建密碼(密碼)
JButton– JButton 類的實例。用於創建按鈕類似實例中的 "Login"。
9. java注冊登錄
不清楚LZ的意思,不連接資料庫么?