十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
java swing 登陆界面code
成都创新互联专注于巴彦淖尔企业网站建设,响应式网站,商城网站建设。巴彦淖尔网站建设公司,为巴彦淖尔等地区提供建站服务。全流程定制制作,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务
/*
* Login.java
*
* Created on __DATE__, __TIME__
*/
package com.agen.library.window;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.agen.library.factory.DAOFactory;
import com.agen.library.util.GlobalUser;
import com.agen.library.vo.User;
/**
*
* @author __USER__
*/
public class Login extends javax.swing.JFrame {
/**
*
*/
private static final long serialVersionUID = -2176093732040600809L;
/** Creates new form Login */
public Login() {
super("易云图书管理软件V1.0");
Image ime = Toolkit.getDefaultToolkit().getImage(
getClass().getResource("/images/ico.png"));
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setIconImage(ime);
initComponents();
setLocationRelativeTo(null);
this.setResizable(false);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// GEN-BEGIN:initComponents
// editor-fold defaultstate="collapsed" desc="Generated Code"
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
formKeyPressed(evt);
}
public void keyTyped(java.awt.event.KeyEvent evt) {
formKeyTyped(evt);
}
});
jLabel2.setFont(new java.awt.Font("微软雅黑", 0, 14));
jLabel2.setText("\u7528\u6237\u540d\uff1a");
jLabel3.setFont(new java.awt.Font("微软雅黑", 0, 14));
jLabel3.setText("\u5bc6 \u7801\uff1a");
jTextField1.setFont(new java.awt.Font("微软雅黑", 0, 14));
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jPasswordField1.setFont(new java.awt.Font("微软雅黑", 0, 12));
jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
jPasswordField1KeyTyped(evt);
}
});
jButton1.setBackground(new java.awt.Color(223, 216, 216));
jButton1.setFont(new java.awt.Font("微软雅黑", 0, 14));
jButton1.setText("\u767b\u9646");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setBackground(new java.awt.Color(223, 216, 216));
jButton2.setFont(new java.awt.Font("微软雅黑", 0, 14));
jButton2.setText("\u53d6\u6d88");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel1.setIcon(new javax.swing.ImageIcon(
getClass().getResource("/images/login_main.jpg"))); // NOI18N
jMenu1.setText("File");
jMenu1.setFont(new java.awt.Font("微软雅黑", 0, 14));
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_Q,
java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setText("Exit");
jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jMenuItem1MouseClicked(evt);
}
});
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
jMenu2.setText("Help");
jMenu2.setFont(new java.awt.Font("微软雅黑", 0, 14));
jMenuItem2.setText("About");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem2);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
false)
.addComponent(
jLabel2,
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.LEADING,
layout.createSequentialGroup()
.addComponent(
jLabel3)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(10,
10,
10)
.addComponent(
jButton1)
.addGap(47,
47,
47)
.addComponent(
jButton2))
.addComponent(
jPasswordField1)
.addComponent(
jTextField1,
javax.swing.GroupLayout.DEFAULT_SIZE,
197,
Short.MAX_VALUE))
.addContainerGap()))));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(
jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
24,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(
jPasswordField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
23,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)));
pack();
}// /editor-fold
// GEN-END:initComponents
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
About.start();
}
private void jPasswordField1KeyTyped(java.awt.event.KeyEvent evt) {
if (evt.getKeyChar() == '\n') {
String name = jTextField1.getText(); // 获取用户名
String pass = String.valueOf(jPasswordField1.getPassword());// 获取密码
User user = null;
// 未输入用户名
if (name.equals("") || name == null) {
JOptionPane.showMessageDialog(this, "用户名不允许为空!", "cuowu", 0);
return;
}
try {
user = DAOFactory.getIUserDAOInstance().findById(name);
if (user != null) {
if (user.getPass() != null user.getPass().equals(pass)) {
GlobalUser.LOGIN_USER = user; // 记录当前用户
// 进入主界面
Main.start();
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
return;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (user == null) {
JOptionPane.showMessageDialog(this, "用户名或密码错误!", "消息", 0);
return;
}
}
}
private void formKeyTyped(java.awt.event.KeyEvent evt) {
}
private void formKeyPressed(java.awt.event.KeyEvent evt) {
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTextField1.setText("");
jPasswordField1.setText("");
jTextField1.requestFocus();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String name = jTextField1.getText(); // 获取用户名
String pass = String.valueOf(jPasswordField1.getPassword());// 获取密码
User user = null;
// 未输入用户名
if (name.equals("")) {
JOptionPane.showMessageDialog(this, "用户名不允许为空!");
return;
}
try {
user = DAOFactory.getIUserDAOInstance().findById(name);
if (user != null) {
if (user.getPass() != null user.getPass().equals(pass)) {
GlobalUser.LOGIN_USER = user; // 记录当前用户
// 进入主界面
Main.start();
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!", "消息", 0);
return;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (user == null) {
JOptionPane.showMessageDialog(this, "用户名或密码错误!", "消息", 0);
return;
}
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
if (JOptionPane.showConfirmDialog(this, "你确定要退出吗?", "提示",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
private void jMenuItem1MouseClicked(java.awt.event.MouseEvent evt) {
System.exit(1);
}
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
// System.out.println(Login.class.getResource("src/images/images/login_main.jpg"));
// new javax.swing.ImageIcon(
// Login.class.getResource("../../../../images/login_main.jpg"));
// new Login().setVisible(true);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
// GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}
将图书信息封装成一个类,根据图书对象中保存的信息进行组装sql语句
如:语句初始化为select * from 图书 where 1=1
组装语句
用一个集合(如:columns)保存要用到的字段名
if(null!=书名){
sql语句append( and 书名=? );
集合.add("书名");
}
if(id0){
append( and id=? );
add("id");
}
if(null!=上架日期){
append( and 上架日期=?);
add("上架日期");
}
遍历集合(columns)
for(int i=0;icolumns.size();i++){
setXX(i+1,对象.getXX());
}
//再执行就o了
%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=gb2312" /
title无标题文档/title
head
body bgcolor="cyan"
center
h1 align="center" class="STYLE1"请输入要查询的关键字/h1
form action="jie.jsp" method="post"
span class="STYLE2"请选择查询条件/span
select name="condition"
option values ="author"作者/option
option values ="name" selected="selected"书名/option
/select
span class="STYLE3"请输入/span
input type="text" name="name" /br /
input type="submit" value="提交" /
/form
/body
/html
跳转后的页面:
%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %
html
head
meta http-equiv="Content-Type" content="text/html; charset=gb2312" /
title无标题文档/title
head
%@ page import="test.BookCo" %
%@ page import="java.sql.*" %
body
jsp:useBean id="Co" class="test.BookCo" scope="application"
/jsp:useBean
center
h1图书信息/h1
table border="1" width="80%"
th书号/thth书名/thth作者/thth出版社/thth书籍简介/th
%
String name=request.getParameter("name");
String condition=request.getParameter("condition");
Co.setKeyword(name,condition);
ResultSet rs=Co.Boo();
while(rs.next())
{
out.print("trtd"+rs.getInt("bId")+"/td");
out.print("td"+rs.getString("bName")+"/td");
out.print("td"+rs.getString("bAuthor")+"/td");
out.print("td"+rs.getString("bPress")+"/td");
out.print("td"+rs.getString("bInfo")+"/td/tr");
}
rs.close();
%
/table
/center
/body
/html
javabean部分:
package test;
import java.sql.*;
import java.io.*;
import java.util.*;
public class BookCo{
public String name,condition;
Connection con=null;
String strurl="jdbc:odbc:bookstore";
Statement stmt=null;
ResultSet rs=null;
public BookCo()
{
name="";
condition="";
}
public void setKeyword(String s,String ss)
{
name=s;
condition=ss;
try{
byte[] b=name.getBytes("iso-8859-1");
name=new String(b);
byte[] bb=condition.getBytes("iso-8859-1");
condition=new String(bb);
}
catch(Exception e){System.out.println("error1:"+e.getMessage());}
}
public ResultSet Boo()
{
String drivername="sun.jdbc.odbc.JdbcOdbcDriver";
String sql="select * from books";
try {
Class.forName(drivername);
con=DriverManager.getConnection(strurl);
stmt=con.createStatement();
if(condition.equals("作者")){
sql="select * from books where bAuthor='"+name+"'";
}
else
if(condition.equals("书名"))
{
sql="select * from books where bName='"+name+"'";
}
rs=stmt.executeQuery(sql);
}
catch (Exception e){
System.out.println("connection fail!");
}
return rs;
}}
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Book {
private int no;
private String name;
private double value;
public Book() {
}
public Book(int no, String name, double value) {
this.no = no;
this.name = name;
this.value = value;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
}
class BookList {
private ListBook bookList;
public BookList() {
bookList = readXML();
}
public long getCount() {
return bookList.size();
}
public ListBook getBookList() {
return bookList;
}
public void setBookList(ListBook bookList) {
this.bookList = bookList;
}
public void add(Book book) {
bookList.add(book);
}
public boolean delete(String name) {
Book book = query(name);
return bookList.remove(book);
}
public void update(Book bookBefore, Book bookAfter) {
bookList.remove(bookBefore);
add(bookAfter);
}
public Book query(String name) {
Book temp = null;
for (Book book : bookList) {
if (book.getName().equals(name)) {
temp = book;
}
}
return temp;
}
public synchronized void writeXmlDocument(Book book) {
try {
File file = new File("D:\\book.xml");
Document document = null;
Element root = null;
if (!file.exists()) {
// 新建student.xml文件并新增内容
document = DocumentHelper.createDocument();
root = document.addElement("Books");//添加根节点
} else {
SAXReader saxReader = new SAXReader();
document = saxReader.read(file);
root = document.getRootElement();//获得根节点
}
Element secondRoot = root.addElement("Book");//二级节点
//为二级节点添加属性,属性值为对应属性的值
secondRoot.addElement("no").setText(book.getNo() + "");
secondRoot.addElement("name").setText(book.getName() + "");
secondRoot.addElement("value").setText(book.getValue() + "");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriter writer = new XMLWriter(new FileOutputStream("D:\\book.xml"), format);
writer.write(document);
writer.close();
document.clearContent();
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized ListBook readXML() {
ListBook list = new ArrayListBook();//创建list集合
File file = null;
try {
file = new File("D:\\book.xml");//读取文件
if (file.exists()) {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
List nodeList = document.selectNodes("Books/Book");
for (int i = 0; i nodeList.size(); i++) {
Element el = (Element) nodeList.get(i);
Book book = new Book();
book.setNo(Integer.parseInt(el.elementText("no")));
book.setName(el.elementText("name"));
book.setValue(Double.parseDouble(el.elementText("value")));
list.add(book);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
class Test {
public static void main(String args[]) {
BookList bl = new BookList();
boolean bBreak = true;
while (bBreak) {
System.out.println("请输入操作代码:");
System.out.println("1:添加 2:删除 3:修改 4:查询 5:书籍统计 6:退出");
Scanner sc = new Scanner(System.in);
int code = sc.nextInt();
if (code == 1) {
System.out.println("请输入编号");
int no = sc.nextInt();
System.out.println("请输入书名");
String name = sc.next();
System.out.println("请输入售价");
double value = sc.nextDouble();
Book book = new Book(no, name, value);
bl.add(book);
bl.writeXmlDocument(book);
} else if (code == 2) {
System.out.println("请输入要删除的书籍名");
String name = sc.next();
if (bl.delete(name)) {
System.out.println("删除成功");
} else {
System.out.println("书籍不存在");
}
} else if (code == 3) {
System.out.println("请输入要修改的书籍名");
String name = sc.next();
Book bookBefore = bl.query(name);
System.out.println("请输入新的编号");
int newNo = sc.nextInt();
System.out.println("请输入新的书名");
String newName = sc.next();
System.out.println("请输入新的售价");
double value = sc.nextDouble();
Book bookAfter = new Book(newNo, newName, value);
bl.update(bookBefore, bookAfter);
} else if (code == 4) {
System.out.println("请输入要查询的书籍名");
String name = sc.next();
Book book = bl.query(name);
System.out.println("编号:" + book.getNo() + " 书名:" + book.getName() + " 售价:" + book.getValue());
} else if (code == 5) {
ListBook list = bl.getBookList();
System.out.println("总书籍数:" + bl.getCount());
for (Book book : list) {
System.out.println("编号:" + book.getNo() + " 书名:" + book.getName() + " 售价:" + book.getValue());
}
} else if (code == 6) {
bBreak = false;
}
}
}
}
jar 包 dom4j.jar jaxen-1.1.4.jar