项目代码
2015-09-21 09:11
316 查看
1. 登录界面LoginFace类的代码:
2. 主程序界面 MainEXE类的代码:
package SaleSystem; import java.awt.Container; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; //连接数据库部分的包 import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; public class LoginFace extends JFrame { //用户名 private JTextField username;//用户名的文本框 //密码 private JPasswordField password;//密码的文本框 //小容器 private JLabel jl1; private JLabel jl3; private JLabel jl4; //小按钮 private JButton bu1; private JButton bu2; /* * 构造方法 */ public LoginFace() { // 设置窗口标题 this.setTitle("用户登录"); // 窗体组件初始化 init(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置布局方式为绝对定位 this.setLayout(null); this.setBounds(0, 0, 355, 265); // 窗体大小不能改变 this.setResizable(false); // 居中显示 this.setLocationRelativeTo(null); // 窗体可见 this.setVisible(true); } /* * 初始化方法 */ public void init() { // 创建一个容器 Container con = this.getContentPane(); jl1 = new JLabel(); // 设置背景图片 jl1.setBounds(0, 0, 355, 265); jl3 = new JLabel("用户名"); jl3.setBounds(50, 70, 70, 20); // 用户号码登录输入框 username = new JTextField(15); username.setBounds(100, 70, 150, 20); // 用户号码登录输入框旁边的文字 jl4 = new JLabel("密码"); jl4.setBounds(55, 100, 70, 20); // 密码输入框 password = new JPasswordField(15); password.setBounds(100, 100, 150, 20); // 按钮设定 bu1 = new JButton("登录"); bu1.setBounds(80, 200, 65, 20); // 给按钮添加1个事件 bu1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try{ Class.forName("com.mysql.jdbc.Driver"); }catch(ClassNotFoundException e1){ e1.printStackTrace(); } //先尝试加载连接驱动 Connection con=null; //定义一个连接对象 try{ con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "test","test"); }catch(SQLException e1){ e1.printStackTrace(); } //若没有抛出异常,则说明连接成功了 Statement st; try{ st=con.createStatement(); String s1=username.getText().trim(); String s2=password.getText().trim(); String sql="select *from user where username='"+s1+"' and password='"+s2+"' "; ResultSet rs = st.executeQuery(sql); //System.out.print("登陆成功\n"); if(rs.next()){ setVisible(false); new MainEXE(); } else{ JOptionPane.showMessageDialog(null, "登录失败 请重新输入"); } } catch(SQLException e1){ e1.printStackTrace(); } } }); bu2 = new JButton("退出"); bu2.setBounds(190, 200, 65, 20); bu2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //关闭登录界面 System.exit(0);//退出程序 } }); // 所有组件用容器装载 jl1.add(jl3); jl1.add(jl4); jl1.add(bu1); jl1.add(bu2); con.add(jl1); con.add(username); con.add(password); } public static void main(String[] args) { // TODO 自动生成的方法存根 new LoginFace(); } }
2. 主程序界面 MainEXE类的代码:
相关文章推荐
- Python常用功能汇总
- 求一个不含0的数列(可以有正数和负数)的最大子序列乘积java
- 网页语言有html,php.jsp,无论什么语言浏览器总是能正常显示,这个解析工作是浏览器完成的吗?
- C#进阶系列——DDD领域驱动设计初探(一):聚合
- c#中简单工厂模式
- Java类加载器深入探索
- Ogre源代码浅析——插件(Plugin)工作原理
- 【转载】ASP.Net中的IsPostBack
- 并发编程之可变状态
- 自动化测试【Maven+Eclipse+Selenium+Java环境搭建和测试】
- 埃奇沃思de盒子
- Java求素数和最大公约数的简单代码示例
- java中对象和对象的引用
- Python+OpenCV学习(19)---摄像机位姿估计
- SenchaTouch2.3.1 正在使用listpaging以及pullrefresh插入 分页演示样品做
- C语言enum枚举类型详解
- django template中load的作用
- C++中变量的类型
- Java关键字final、static使用总结
- 黑马程序员----C语言学习笔记之运算符2