您的位置:首页 > 编程语言

项目代码

2015-09-21 09:11 316 查看
1. 登录界面LoginFace类的代码:

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类的代码:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: