大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍了java通过JFrame做一个登录系统的界面完整代码示例,具有一定借鉴价值,需要的朋友可以参考下。
辽中网站建设公司创新互联公司,辽中网站设计制作,有大型网站制作公司丰富经验。已为辽中上千多家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的辽中做网站的公司定做!
在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) {
分三个包,自己建个包,导进去就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;
}
}
}
}
/**
* 打开打印窗口
* url:链接页面或action动作
* Banglu
*/
function printWindow(url){
var sURL = url;
var sFeatures = "toolbar=no, menubar=no, scrollbars=no,resizable=yes, "
+ "location=no, status=no, titlebar=no, width=800, height=600, top=100, left=100";
window.open(sURL,'notoolbar',sFeatures);
}function exportWindow(url){
var sURL = url;
var sFeatures = "toolbar=no, menubar=no, scrollbars=no,resizable=yes, "
+ "location=no, status=no, titlebar=no, width=800, height=600, top=50, left=50";
var objwin=window.open(sURL,'export'+randomNum(),sFeatures);
objwin.close();
}
/**
* 打开模态窗口
* url:链接页面或action动作
* width:打开模态窗口的宽度
* height:打开模态窗口的高度
* 注意:打开模态窗口的页面中要在head后面加上
* meta http-equiv="Pragma" content="no-cache":禁止模态窗口缓存
* base target="_self"/:模态窗口中的表单在本窗口中提交
* a onClick='window.location = "view-source:" + window.location.href'b源文件/b/a 可以查看模态窗口的源文件
* Banglu
*/
function modalWindow(url, width, height){
var sURL = url;
var sFeatures = "dialogWidth:" + width + "px; dialogHeight:" + height + "px; "
+ "help:no; scroll:yes; center:yes; status:no;resizable:yes";
window.showModalDialog(sURL, window, sFeatures);
}/**
* 打开普通窗口
* url:链接页面或action动作
* width:宽度
* height:高度
* Banglu
*/
function openWindow(url, width, height){
var sURL=url;
var sFeatures = "scrollbars=yes, status=yes, resizable=yes,"
+ "toolbar=yes, menubar=yes, location=yes, titlebar=yes"
if(width!=null){
sFeatures+=", width="+width;
}
if(height!=null){
sFeatures+=", height="+height;
}
window.open(sURL, 'open'+randomNum(), sFeatures);
}/**
* 打开窗口
* url:链接页面或action动作
* width:宽度
* height:高度
Banglu
*/
function openNoBarWindow(url, width, height){
var sURL=url;
var sFeatures = "scrollbars=no, status=no, resizable=no,"
+ "toolbar=no, menubar=no, location=no, titlebar=no"
if(width!=null){
sFeatures+=", width="+width;
sFeatures+=", left="+(screen.width-width)/2;
}
if(height!=null){
sFeatures+=", height="+height;
sFeatures+=", top="+(screen.height-height-100)/2;
}
window.open(sURL, 'openNoBar'+randomNum(), sFeatures);
}
/**
* 打开全屏窗口
* url:链接页面或action动作
* Banglu
*/
function openFullWindow(url){
var sURL=url;
var sFeatures = "toolbar=no, menubar=no, scrollbars=no, resizable=yes, "
+ "location=no, status=no, titlebar=no, width="+(screen.width-10)+", "
+ "height="+(screen.height-60)+", top=0, left=0";
window.open(sURL, 'full'+randomNum(), sFeatures);
}/**
* 打开主窗口
* url:链接页面或action动作
* Banglu
*/
function openMainWindow(url){
var sURL=url;
var sFeatures = "toolbar=no, menubar=no, scrollbars=no, resizable=yes, "
+ "location=no, status=no,titlebar=no, width="+(screen.width-10)+", "
+ "height="+(screen.height-60)+", top=0, left=0";
window.open(sURL, 'main', sFeatures);
}
/**
* 设置链接
* url:连接的jsp页面或action动作
* Banglu
*/
function link(url, frameID){
if(frameID==null){
window.location.href = url;
}
else{
window.frames[frameID].location = url
}
}/**
* 回车代替tab
* Banglu
*/
function handleKey(){
var gk = window.event.keyCode;
if (gk==13) {
if(window.event.srcElement.tagName!='TEXTAREA'){
window.event.keyCode=9;
return;
}
}
}/**
* 全屏显示
* Banglu
*/
function fullScreen(){
window.dialogHeight=window.screen.availHeight;
window.dialogWidth=window.screen.availWidth;
}
function Resize_dialog(t,l,w,h) {
window.dialogTop = t+"px";
window.dialogLeft = l+"px";
window.dialogHeight = h+"px";
window.dialogWidth = w+"px";
}