重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
這篇文章主要介紹了java通過JFrame做一個登錄系統的界面完整代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
10余年的青川網站建設經驗,針對設計、前端、開發、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。網絡營銷推廣的優勢是能夠根據用戶設備顯示端的尺寸不同,自動調整青川建站的顯示方式,使網站能夠適用不同顯示終端,在瀏覽器中調整網站的寬度,無論在任何一種瀏覽器上瀏覽網站,都能展現優雅布局與設計,從而大程度地提升瀏覽體驗。創新互聯從事“青川網站設計”,“青川網站推廣”以來,每個客戶項目都認真落實執行。
在java的JFrame內通過創建匿名對象的方式做登錄界面
package com.sxt;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame{
JTextField txtname=new JTextField();
JPasswordField txtpass=new JPasswordField();
JButton bl=new JButton("登錄");
JButton bg=new JButton("關閉");
//構造無參構造器把主要的方法放在構造器里,然后在main方法里面調
public LoginFrame(){
setBounds(25,25,250,250);
Container c = getContentPane();
c.setLayout(new GridLayout(4,2,10,10));
c.add(new JLabel("用戶名"));
c.add(txtname);
c.add(new JLabel("密碼"));
c.add(txtpass);
c.add(bl);
c.add(bg);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//注意:此處是匿名內部類
bg.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
}
);
//注意:此處是匿名內部類
bl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
一步步更新:頁面
form action="regist.servlet" method="post"table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" class="tableAdd borTop" tr th width="14%" height="30" nowrap用戶名/th td class="pl5" INPUT id="sName" name="name" type="text" size="20" /td /tr tr th width="14%" height="30" nowrap密碼/th td class="pl5" INPUT name="password" type="password" size="20" /td /tr tr th width="14%" height="30" nowrap確認密碼/th td class="pl5" INPUT name="confrimPwd" type="password" size="20" /td /tr tr th width="14%" height="30" nowrap性別/th td class="pl5" 男INPUT name="sex" type="radio" value="1" checked="checked" size="20" 女INPUT name="sex" type="radio" value="0" size="20" /td /tr tr th width="14%" height="30" nowrap愛好/th td class="pl5" INPUT name="enjoy" type="checkbox" size="20" value="籃球"籃球 INPUT name="enjoy" type="checkbox" size="20" value="足球"足球 /td /tr tr th width="14%" height="30" nowrap生日/th td class="pl5" INPUT name="brithday" type="text" size="20" /td /tr tr th width="14%" height="30" nowrap備注/th td class="pl5" textarea rows="5" cols="200" name="remark"/textarea /td /tr tr th width="14%" height="30" nowrap /th td class="pl5" input type="submit" value="提交" input type="reset" value="重置" /td /tr/table/form
數據庫部分:
import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import com.mysql.jdbc.Connection;import com.mysql.jdbc.Statement;public class DataBaseUtil { public static Connection getConnection() throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.100.113/datebase", "username", "password"); return conn; } public static Statement getPS() throws ClassNotFoundException, SQLException { Statement statement = (Statement) getConnection().createStatement(); return statement; } public static void close(Connection conn,Statement st,ResultSet rs) throws SQLException{ if(rs != null) { rs.close(); } if(st != null) { st.close(); } if(conn != null) { conn.close(); } }}
效果圖
代碼
!DOCTYPE?html
html
head
meta?charset="UTF-8"
title先鋒圖書館管理系統-登錄/title
style
*{
margin:?0;
padding:?0;
list-style:?none;
}
#top{
width:?1000px;
height:?95px;
margin:?0?auto;
margin-top:?25px;
}
#top_top{
width:?1000px;
height:?65px;
background:?deepskyblue;
}
#top_top_left{
width:?300px;
height:?65px;
float:?left;
}
#top_top_leftlabel{
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:?0?auto;
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#loginimg{
float:?left;
}
#content#login#select{
width:?305px;
height:?210px;
float:?right;
}
#content#login#selectdiv{
width:?230;
height:?30px;
margin-left:?30px;
}
#content#login#select#d1{
margin-top:30px;
margin-bottom:?3px;
}
#content#login#selectp{
font-size:?14px;
margin-left:?95px;
}
#bottom{
width:?1000px;
height:?35px;
line-height:?35px;
margin:?0?auto;
background:?deepskyblue;
text-align:?center;
color:?white;
}
/style
/head
body
div?id="top"
div?id="top_top"
div?id="top_top_left"
img?src="img/test/a13.png"?width="78px"?height="65px"label?id="a2"先鋒圖書館系統管理平臺/label
/div
/div
div?id="top_bottom"
div?id="top_bottom_left"當前位置?:?首頁??系統管理??登錄/div
div?id="top_bottom_right"當前時間?:?label?id="lable"/label/div
/div
/div
div?id="content"
div?id="text"歡迎登錄先鋒圖書館管理系統/div
div?id="login"
img?src="img/test/a14.png"??width="175px"?height="210px"/
form?id="select"
div?id="d1"用戶名:?nbsp;nbsp;input?type="text"?//div
div密????nbsp;?碼:?nbsp;nbsp;input?type="password"?//div
p
input?type="radio"?name="user"?value="read"/讀者nbsp;nbsp;nbsp;nbsp;
input?type="radio"?name="user"?value="admin"/管理員
/pbr/
p
input?type="button"?value="確定"?style="width:?50px;"?onclick="put()"/nbsp;nbsp;nbsp;nbsp;
input?type="reset"?value="重置"?style="width:?50px;"/
/p
/form
/div
/div
div?id="bottom"欣欣科技有限公司版權所有/div
/body
script?type="text/javascript"?src="JQuery/jquery.js"/script
script?type="text/javascript"?src="js/GetCurrentTime.js"/script
script
//驗證用戶名和密碼
function?put(){
var?d?=?$("#selectdivinput");//獲取用戶名和密碼
var?name?=?d[0].value;
var?pass?=?d[1].value;
var?user?=?null;
var?r?=?document.getElementsByName("user");//獲取用戶類型
for(i=0;ir.length;i++){
if(r[i].checked){
user=r[i].value;
}
}
//console.log(name?+?","?+pass?+?","?+user);//輸出測試
if(user==null){
window.alert("請選擇用戶類型!");
}else?if(user=="admin"??name!="admin"){
window.alter("用戶名錯誤!");
}else?if(user=="admin"??name=="admin"??pass!="123456"){
window.alert("密碼錯誤!");
}else?if(name=="admin"??pass=="123456"??user=="admin"){
window.location.href="work_02_welcome.html";//在js中在本頁面中打開新鏈接
}else{
window.alert("用戶名錯誤");
}
}
/script
/html
昨天剛好有人提問了一個類似的問題,我把昨天的回答發給你,供你參考 , 主要是思路, 如果是作業的話, 最好不要和同學相同, 自己理解后, 重新寫1個,會比較好
效果圖
參考代碼和注釋
import?java.awt.*;
import?java.awt.event.*;
import?java.io.*;
import?javax.swing.*;
//該窗口繼承自JFrame,?實現了ActionListener接口
public?class?SingUpFrame?extends?JFrame?implements?ActionListener?{
//定義需要的組件
JTextField?jtfName,?jtfEmail;?
JPasswordField?jpf;
JRadioButton?jrb1,?jrb2;
JComboBoxString?jcb;
JButton?jbReset,?jbSingUp;
//常量
public?static?final?String?NEW_LINE?=?System.getProperty("line.separator");//?獲取系統的換行符
public?static?final?String?FILE_PATH?=?"d:/users.txt";//指定文件的路徑
//構造器
public?SingUpFrame()?{
JPanel?jp1?=?new?JPanel();
JLabel?jl1?=?new?JLabel("賬號");
jtfName?=?new?JTextField(15);
jp1.add(jl1);
jp1.add(jtfName);
JPanel?jp2?=?new?JPanel();
JLabel?jl2?=?new?JLabel("密碼");
jpf?=?new?JPasswordField(15);
jp2.add(jl2);
jp2.add(jpf);
JPanel?jp3?=?new?JPanel();
JLabel?jl3?=?new?JLabel("郵箱");
jtfEmail?=?new?JTextField(15);
jp3.add(jl3);
jp3.add(jtfEmail);
JPanel?jp4?=?new?JPanel();
JLabel?jl4?=?new?JLabel("性別");
ButtonGroup?bg?=?new?ButtonGroup();
jrb1?=?new?JRadioButton("男");
jrb1.setSelected(true);//?默認選中男性
jrb2?=?new?JRadioButton("女");
bg.add(jrb1);
bg.add(jrb2);
jp4.add(jl4);
jp4.add(jrb1);
jp4.add(jrb2);
JLabel?jl5?=?new?JLabel("年齡");
String[]?ary?=?new?String[12];
for?(int?i?=?18;?i??30;?i++)?{//?18~30可供選中的范圍
ary[i?-?18]?=?i?+?"";
}
jcb?=?new?JComboBoxString(ary);
jp4.add(jl5);
jp4.add(jcb);
JPanel?jpc?=?new?JPanel(new?GridLayout(4,?1));//?4行1列布局
jpc.add(jp1);
jpc.add(jp2);
jpc.add(jp3);
jpc.add(jp4);
add(jpc);
JPanel?jps?=?new?JPanel();
jbReset?=?new?JButton("重填");
jbReset.addActionListener(this);//給按鈕添加響應
jbSingUp?=?new?JButton("確定");
jbSingUp.addActionListener(this);//給按鈕添加響應
jps.add(jbReset);
jps.add(jbSingUp);
add(jps,?BorderLayout.SOUTH);
setTitle("注冊窗口");//?窗口標題
setSize(300,?285);//?窗口大小
setLocationRelativeTo(null);//?窗口居中
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//?當窗口關閉時,程序結束
}
@Override
public?void?actionPerformed(ActionEvent?e)?{
JButton?jb?=?(JButton)?e.getSource();
if?(jb?==?jbReset)?{
jtfName.setText("");
jpf.setText("");
jtfEmail.setText("");
jrb1.setSelected(true);
jcb.setSelectedIndex(0);//?選中第1個選項(18)
}?else?if?(jb?==?jbSingUp)?{
String?name?=?jtfName.getText().trim();
String?pswd?=?new?String(jpf.getPassword());
String?email?=?jtfEmail.getText().trim();
String?xb?=?jrb1.isSelected()???"男"?:?"女";
String?age?=?(String)?jcb.getSelectedItem();
if(name.equals("")||pswd.equals("")||email.equals(""))?{//對輸入的信息進行一個簡單的判斷
JOptionPane.showMessageDialog(null,?"請完整填寫所有的信息",?"提示",JOptionPane.WARNING_MESSAGE);??
return;
}
StringBuffer?sb?=?new?StringBuffer();
sb.append("賬號:"?+?name?+?NEW_LINE?+?"密碼:"?+?pswd?+?NEW_LINE?+?"郵箱:"?+?email?+?NEW_LINE?+?"性別:"?+?xb
+?NEW_LINE?+?"年齡:"?+?age+NEW_LINE);
boolean?flag?=?saveInfo(sb.toString());
if(flag)?{
JOptionPane.showMessageDialog(null,?"注冊成功?,保存成功");
}else?{
JOptionPane.showMessageDialog(null,?"Sorry!保存失敗.注冊出現了問題...",?"IO錯誤",JOptionPane.ERROR_MESSAGE);
}
}
}
//方法:保存信息到文件
public?boolean?saveInfo(String?info)?{
FileWriter?fw;
try?{
fw?=?new?FileWriter(FILE_PATH,?true);//追加文字到文件尾部
fw.write(info);
fw.close();
}?catch?(IOException?e)?{
e.printStackTrace();
return?false;//IO異常,保存不成功
}
return?true;//保存成功
}
//main方法
public?static?void?main(String[]?args)?{
new?SingUpFrame().setVisible(true);?//創建窗口并可見
}
}