java用戶登錄代碼
⑴ 登錄界面的java代碼,分別有教師登錄,管理員登錄,學生登錄,右邊是用戶名和密碼,見圖。
分三個包,自己建個包,導進去就ok了,資料庫是access的。
package 登錄;
import java.awt.EventQueue;
public class Cilent {
private JFrame frame;
private JTextField textField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cilent window = new Cilent();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Cilent() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("登陸界面");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
JLabel lblNewLabel = new JLabel("用戶名");
lblNewLabel.setBounds(38, 43, 80, 34);
frame.getContentPane().add(lblNewLabel);
textField = new JTextField();
textField.setBounds(155, 42, 227, 37);
frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel label = new JLabel("密碼");
label.setBounds(38, 115, 80, 34);
frame.getContentPane().add(label);
passwordField = new JPasswordField();
passwordField.setBounds(155, 115, 227, 37);
frame.getContentPane().add(passwordField);
JButton btnNewButton = new JButton("登錄");
btnNewButton.setBounds(60, 187, 115, 34);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
UserCheck UC=new UserCheck(textField.getText(),String.valueOf(passwordField.getPassword()));
if(UC.getI()!=0) //有此用戶
{
frame.setVisible(false);
}
else
{
textField.setText("");
passwordField.setText("");
}
}
});
JButton button = new JButton("取消");
button.setBounds(242, 187, 115, 34);
frame.getContentPane().add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
textField.setText("");
passwordField.setText("");
}
});
}
}
/*****************************************************************/
package 登錄;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import 操作處理.UsersCL;
/**@author 20111024
* 檢測登錄的用戶在資料庫中有無,若沒有,則提示沒有此用戶,
* 若有,則判斷級別:普通用戶還是管理員。
*/
public class UserCheck {
private int i=0; //用戶級別:0不是用戶、1是管理員、2是普通用戶
UserCheck(String name ,String password)
{
String jdriver="sun.jdbc.odbc.JdbcOdbcDriver";
String connectDB="jdbc:odbc:Students";
Statement stmt=null;
ResultSet rs=null;
Connection con=null;
try {
Class.forName(jdriver);
con=DriverManager.getConnection(connectDB);
stmt=con.createStatement();
String query="select * from users where name='"+name+"' and passwd='"+password+"'";
rs=stmt.executeQuery(query);
if(rs.next())
{
//資料庫中有此用戶,訪問成功
i=Integer.parseInt(rs.getString(3));
UsersCL UL=new UsersCL(i);
}
else
{
i=0; //沒有用戶是默認是0級
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int getI() {
return i;
}
}
/********************************************************************************************/
package 操作處理;
import java.awt.EventQueue;
public class UsersCL implements ActionListener{
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private int i=0;
private JLabel label_3;
private JTextField textField_4;
public UsersCL(int i) {
this.i=i;
frame = new JFrame();
frame.setTitle("用戶處理界面");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
frame.setVisible(true);
JLabel lblNewLabel = new JLabel("學號");
lblNewLabel.setBounds(24, 32, 74, 29);
frame.getContentPane().add(lblNewLabel);
JLabel label = new JLabel("姓名");
label.setBounds(24, 71, 74, 29);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("年齡");
label_1.setBounds(24, 110, 74, 29);
frame.getContentPane().add(label_1);
label_3 = new JLabel("性別");
label_3.setBounds(24, 149, 74, 29);
frame.getContentPane().add(label_3);
JLabel label_2 = new JLabel("狀態");
label_2.setBounds(24, 195, 74, 29);
frame.getContentPane().add(label_2);
textField = new JTextField();
textField.setBounds(101, 34, 113, 25);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(101, 73, 113, 25);
frame.getContentPane().add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(101, 112, 113, 25);
frame.getContentPane().add(textField_2);
textField_3 = new JTextField();
textField_3.setEditable(false);
textField_3.setColumns(10);
textField_3.setBounds(101, 199, 288, 25);
frame.getContentPane().add(textField_3);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(101, 149, 113, 25);
frame.getContentPane().add(textField_4);
if(1==i)
{
JButton btnNewButton = new JButton("追加");
btnNewButton.setBounds(276, 41, 113, 29);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(this);
btnNewButton.setActionCommand("追加");
JButton button_1 = new JButton("刪除");
button_1.setBounds(276, 145, 113, 29);
frame.getContentPane().add(button_1);
button_1.addActionListener(this);
button_1.setActionCommand("刪除");
}
JButton button = new JButton("查詢");
button.setBounds(276, 91, 113, 29);
frame.getContentPane().add(button);
button.addActionListener(this);
button.setActionCommand("查詢");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String name,age,sex,query=null;
int num,age1,count=0;
num=Integer.parseInt(textField.getText());
name=textField_1.getText();
age1=Integer.parseInt(textField_2.getText());
sex=textField_4.getText();
if(e.getActionCommand().equals("追加"))
{
query="insert into students values("+num+","+"'"+name+"',"+age1+",'"+sex+"');";
count=1;
}
else if(e.getActionCommand().equals("查詢"))
{
query="select * from students where XSB="+num+";";
count=2;
}
else if(e.getActionCommand().equals("刪除"))
{
query="delete from students where XSB="+num+" and name="+"'"+name+"'";
count=3;
}
Statement stmt=null;
ResultSet rs=null;
Connection con=null;
String jdriver="sun.jdbc.odbc.JdbcOdbcDriver";
String connectDB="jdbc:odbc:Students";
String query1=null;
try {
Class.forName(jdriver);
con=DriverManager.getConnection(connectDB);
stmt=con.createStatement();
if(count==1)
{
query1="select * from students where XSB="+num+";";
rs=stmt.executeQuery(query1);
if(rs.next())
textField_3.setText("已經由此記錄,不能追加!");
else
{
stmt.executeUpdate(query);
textField_3.setText("已經追加完成!");
}
}
else if(2==count)
{
stmt.executeQuery(query);
rs=stmt.executeQuery(query);
if(rs.next())
{
textField_3.setText("已查找到此記錄!");
}
else
{
textField_3.setText("沒有此記錄,可以追加!");
}
}
else if(3==count)
{
query1="select * from students where XSB="+num+" and name="+"'"+name+"'";
rs=stmt.executeQuery(query1);
if(rs.next())
{
stmt.executeUpdate(query);
textField_3.setText("已刪除此記錄!");
}
else
textField_3.setText("無此記錄!");
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally{
//關閉資源
if(stmt!=null){
try {
stmt.close();
} catch (Exception e2) {
// TODO: handle exception
}
stmt=null;
}
if(con!=null){
try {
con.close();
} catch (Exception e2) {
// TODO: handle exception
}
con=null;
}
}
}
}
⑵ 用JAVA編寫一個用戶或注冊登錄界面。請哪位高手能夠寫下具體的代碼,謝謝
效果圖
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>先鋒圖書館管理系統-登錄</title>
<style>
*{
margin:0;
padding:0;
list-style:none;
}
#top{
width:1000px;
height:95px;
margin:0auto;
margin-top:25px;
}
#top_top{
width:1000px;
height:65px;
background:deepskyblue;
}
#top_top_left{
width:300px;
height:65px;
float:left;
}
#top_top_left>label{
width:200px;
height:65px;
color:white;
float:right;
}
#top_top_left>#a2{
padding-left:10px;
padding-top:20px;
font-size:16px;
}
#top_bottom{
width:1000px;
height:30px;
}
#top_bottom_left{
width:340px;
height:30px;
line-height:30px;
font-size:12px;
background:skyblue;
color:white;
text-indent:2em;
float:left;
}
#top_bottom_right{
width:660px;
height:30px;
line-height:30px;
font-size:12px;
color:blueviolet;
text-align:center;
float:right;
background:lightskyblue;
}
#content{
width:1000px;
height:600px;
margin:0auto;
background:#587FBA;
}
#content>#text{
width:1000px;
height:50px;
line-height:50px;
padding-top:100px;
font-size:36px;
font-family:"楷體";
font-weight:bold;
text-align:center;
}
#content>#login{
width:480px;
height:210px;
margin-top:20px;
margin-left:260px;
background:#85A0CB;
}
#content>#login>img{
float:left;
}
#content>#login>#select{
width:305px;
height:210px;
float:right;
}
#content>#login>#select>div{
width:230;
height:30px;
margin-left:30px;
}
#content>#login>#select>#d1{
margin-top:30px;
margin-bottom:3px;
}
#content>#login>#select>p{
font-size:14px;
margin-left:95px;
}
#bottom{
width:1000px;
height:35px;
line-height:35px;
margin:0auto;
background:deepskyblue;
text-align:center;
color:white;
}
</style>
</head>
<body>
<divid="top">
<divid="top_top">
<divid="top_top_left">
<imgsrc="img/test/a13.png"width="78px"height="65px"><labelid="a2">先鋒圖書館系統管理平台</label>
</div>
</div>
<divid="top_bottom">
<divid="top_bottom_left">當前位置:首頁>系統管理>登錄</div>
<divid="top_bottom_right">當前時間:<labelid="lable"></label></div>
</div>
</div>
<divid="content">
<divid="text">歡迎登錄先鋒圖書館管理系統</div>
<divid="login">
<imgsrc="img/test/a14.png"width="175px"height="210px"/>
<formid="select">
<divid="d1">用戶名: <inputtype="text"/></div>
<div>密 碼: <inputtype="password"/></div>
<p>
<inputtype="radio"name="user"value="read"/>讀者
<inputtype="radio"name="user"value="admin"/>管理員
</p><br/>
<p>
<inputtype="button"value="確定"style="width:50px;"onclick="put()"/>
<inputtype="reset"value="重置"style="width:50px;"/>
</p>
</form>
</div>
</div>
<divid="bottom">欣欣科技有限公司版權所有</div>
</body>
<scripttype="text/javascript"src="JQuery/jquery.js"></script>
<scripttype="text/javascript"src="js/GetCurrentTime.js"></script>
<script>
//驗證用戶名和密碼
functionput(){
vard=$("#select>div>input");//獲取用戶名和密碼
varname=d[0].value;
varpass=d[1].value;
varuser=null;
varr=document.getElementsByName("user");//獲取用戶類型
for(i=0;i<r.length;i++){
if(r[i].checked){
user=r[i].value;
}
}
//console.log(name+","+pass+","+user);//輸出測試
if(user==null){
window.alert("請選擇用戶類型!");
}elseif(user=="admin"&&name!="admin"){
window.alter("用戶名錯誤!");
}elseif(user=="admin"&&name=="admin"&&pass!="123456"){
window.alert("密碼錯誤!");
}elseif(name=="admin"&&pass=="123456"&&user=="admin"){
window.location.href="work_02_welcome.html";//在js中在本頁面中打開新鏈接
}else{
window.alert("用戶名錯誤");
}
}
</script>
</html>
⑶ 求JAVA實現用戶登錄界面代碼
求麥克實現登錄用戶密碼,電費就點擊x加y就可以了。
⑷ 用Java編寫注冊登錄程序
什麼都不說了 直接給你代碼吧
package com.moliying.ui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
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);
frame.setBounds(450, 300, 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("系統管理員登錄"));
c.add(titlePanel, "North");
// 中部表單
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
JLabel a1 = new JLabel("用戶名:");
a1.setBounds(50, 20, 50, 20);
JLabel a2 = new JLabel("密 碼:");
a2.setBounds(50, 60, 50, 20);
fieldPanel.add(a1);
fieldPanel.add(a2);
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");
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(username.getText().toString());
}
});
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
}
public static void main(String[] args) {
// new Login();
String ss = "abbabbbaabbbccba";
System.out.println(ss.split("b").length);
}
}
⑸ java寫的用戶登陸實例,用eclipse開發的具體步奏和代碼
java寫的用戶登錄實例,實際頁面展示使用的jsp,那麼下面是jsp的登錄頁面代碼:
1、login.jsp代碼
<%
string name = request.getparameter("username");
string pwd = request.getparameter("password");
//out.println(name+pwd);
string sql ="select * from info where username='"+name+"' and password='"+pwd+"'";
//out.println(sql);
statement stm= null;
resultset rs =null;
try
{
stm = conn.createstatement();
rs = stm.executequery(sql);
if(rs.next())
{
session.setattribute("username",name);
response.sendredirect("index.html");
}
else
{
response.sendredirect("index1.html");
}
}
catch(sqlexception e)
{
e.printstacktrace();
}
%>
<!--登錄的表單-->
<form name="form1" method="post" action="login.jsp">
<p>
<label for="username"></label> 用戶名
<input type="text" name="username" id="username">
</p>
<p>
<label for="passwrod"></label> 密碼
<input type="text" name="passwrod" id="passwrod">
</p>
<p>
<input type="submit" name="button" id="button" value="提交">
</p>
</form>
2、用戶信息表,存放用戶名和密碼:
user_info 表
create table if not exists `test` (
`id` int(8) not null auto_increment,
`username` char(150) default null,
`password` varchar(32),
`times` int(4) not null,
primary key (`id`)
) engine=myisam default charset=utf8 auto_increment=1 ;
⑹ 求一個用Java寫的登陸界面的源代碼,輸入用戶名,密碼,登錄後顯示歡迎登錄,加分
這個是我之前做著玩的,只做了個登錄,不要介意,登錄名:admin,密碼:admin。
⑺ 用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"。
⑻ 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);
}
}
⑼ 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();
}
}
⑽ 用java語言寫一個用戶登陸界面
//這個是我寫的,裡面有連接資料庫的部分。你可以拿去參考一下
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
importjava.sql.*;
//throwsException
{
JLabellbl1=newJLabel("用戶名:");
JLabellbl2=newJLabel("密碼:");
JTextFieldtxt=newJTextField(5);
JPasswordFieldpf=newJPasswordField();
JButtonbtn1=newJButton("確定");
JButtonbtn2=newJButton("取消");
publicLoginFrm(){
this.setTitle("登陸");
JPaneljp=(JPanel)this.getContentPane();
jp.setLayout(newGridLayout(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);
}
publicvoidactionPerformed(ActionEventae){
if(ae.getSource()==btn1){
try{
Class.forName("com.mysql.jdbc.Driver");//mysql資料庫
Connectioncon=DriverManager.getConnection(
"jdbc:mysql://localhost/Car_zl","root","1");//資料庫名為Car_zl,密碼為1
System.out.println("com:"+con);
Statementcmd=con.createStatement();
Stringsql="select*fromuserwhereUser_ID='"
+txt.getText()+"'andUser_ps='"
+pf.getText()+"'";
ResultSetrs=cmd
.executeQuery(sql);//表名為user,user_ID和User_ps是存放用戶名和密碼的欄位名
if(rs.next()){
JOptionPane.showMessageDialog(null,"登陸成功!");
}else
JOptionPane.showMessageDialog(null,"用戶名或密碼錯誤!");
}catch(Exceptionex){
}
if(ae.getSource()==btn2){
System.out.println("1111111111111");
//txt.setText("");
//pf.setText("");
System.exit(0);
}
}
}
publicstaticvoidmain(Stringarg[]){
JFrame.(true);
LoginFrmfrm=newLoginFrm();
frm.setSize(400,200);
frm.setVisible(true);
}
}