import javaawt;
import javaxswing;
import javaawtevent;
import javasql;
class LoginFrm extends JFrame implements ActionListener// throws Exception
{
JLabel lbl1 = new JLabel("用户名:");
JLabel lbl2 = new JLabel("密码:");
JTextField txt = new JTextField(5);
JPasswordField pf = new JPasswordField();
JButton btn1 = new JButton("确定");
JButton btn2 = new JButton("取消");
public LoginFrm() {
thissetTitle("登陆");
JPanel jp = (JPanel) thisgetContentPane();
jpsetLayout(new GridLayout(3, 2, 5, 5));
jpadd(lbl1);
jpadd(txt);
jpadd(lbl2);
jpadd(pf);
jpadd(btn1);
jpadd(btn2);
btn1addActionListener(this);
btn2addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (aegetSource() == btn1) {
try {
ClassforName("commysqljdbcDriver");// mysql数据库
Connection con = DriverManagergetConnection(
"jdbc:mysql://localhost/Car_zl", "root", "1");// 数据库名为Car_zl,密码为1
Systemoutprintln("com : "+ con);
Statement cmd = concreateStatement();
String sql = "select from user where User_ID='"
+ txtgetText() + "' and User_ps='"
+ pfgetText() + "'" ;
ResultSet rs = cmd
executeQuery(sql);// 表名为user,user_ID和User_ps是存放用户名和密码的字段名
if (rsnext()) {
JOptionPaneshowMessageDialog(null, "登陆成功!");
} else
JOptionPaneshowMessageDialog(null, "用户名或密码错误!");
} catch (Exception ex) {
}
if (aegetSource() == btn2) {
Systemoutprintln("1111111111111");
//txtsetText("");
//pfsetText("");
Systemexit(0);
}
}
}
public static void main(String arg[]) {
JFramesetDefaultLookAndFeelDecorated(true);
LoginFrm frm = new LoginFrm();
frmsetSize(400, 200);
frmsetVisible(true);
}
}
说说servlet里面的方法:
public void ValidateUserPass(String user,String pass){
RequestDispathcher rd =null
DBFactory db=DBFactoryImplgetDBFactoryInstance();//得到数据库链接
flg=dbfindUser(user,pass);
// 这里是不存在用户
if(flghasNext()==-1){
// 登录时错误了,一般我们会给用户一个提示
sessionsetAttirbute("msg","对不起,用户名或密码错误");
RequestDispathcher rd = reqgetRequesDispatcher("loginjsp");
rdforward(request, reponse);//将请求对象和响应对象传递进来
} // 这里是存在当前用户
else{
//当然这里登录成功时,我们要把当前用户写到session里面保存
sessionsetAttirbute("userName",user);
//这个请求转发语句
requestsendRedirect("indexhtml");
}
}
// 上述代码,你可以参考下我的方法,我也很久没做JAVA开发了,我现在从事前端UI开发,本来我想在写一个用struts 2登录的程序的,可我现在忘得差不多了,上面我所用到的属性建议你自己好好的研究一下,往后你将学到struts2 hibernate,Spring等一系列优秀的开源框架,说白了,这些东西的底层还是这些,只不过这些框架做了一些封装隔离。上述代码建议你重点理解一下:请求转发和重定向的区别。
java实现的简单登录页面,从一个按钮点击后跳转的页面的jframe写法:import javaawt;
import javaawtevent;
import javaxswing;
public class jb{
public static void main(String args[]){
JFrame f=new JFrame("点我跳转");
Container contentPane=fgetContentPane();
contentPanesetLayout(new GridLayout(1,2));
Icon icon=new ImageIcon("bjpg");
JLabel label2=new JLabel("a",icon,JLabelCENTER);
label2setHorizontalTextPosition(JLabelCENTER);
contentPanesetLayout(new FlowLayout( FlowLayoutCENTER,10,10));
JButton bb=new JButton("");
bbaddActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFrame bf=new JFrame("新窗体");
Icon icon=new ImageIcon("enterjpg");
JLabel label2=new JLabel(icon);
bfgetContentPane()add(label2);
bfsetSize(300,360);
bfshow();
}});
contentPaneadd(label2);
contentPaneadd(bb);
fpack();
fshow();
}}/ Mainjava
Created on __DATE__, __TIME__
/
import javaxswingJOptionPane;
/
@author __USER__
/
public class Main extends javaxswingJFrame {
/ Creates new form Main /
public Main() {
initComponents();
}
/ 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() {
jPanel1 = new javaxswingJPanel(); //标签
jLabel1 = new javaxswingJLabel();
jLabel2 = new javaxswingJLabel();
jLabel3 = new javaxswingJLabel();
jButton1 = new javaxswingJButton(); //登录按钮
jButton2 = new javaxswingJButton(); //
jTextField1 = new javaxswingJTextField(); //文本框
jTextField2 = new javaxswingJTextField();
setDefaultCloseOperation(javaxswingWindowConstantsEXIT_ON_CLOSE); //关闭时的 *** 作,即退出程序
setTitle("登录界面"); //设置标题:欢迎使用股票管理系统
setResizable(false); //设置窗口不可调节大小
jLabel1setFont(new javaawtFont("微软雅黑", 0, 18)); //设置标签的字体
jLabel1
setText("您好,请输入信息");
jLabel2setText("用户名");
jLabel3setText("用户密码");
jButton1setText("登录");
jButton1addActionListener(new javaawteventActionListener() { //采用匿名内部类来实现按钮监听功能
public void actionPerformed(javaawteventActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2setText("退出");
jButton2addActionListener(new javaawteventActionListener() { //同上
public void actionPerformed(javaawteventActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javaxswingGroupLayout jPanel1Layout = new javaxswingGroupLayout( //层次布局管理器,下面n长的代码,就是设置位置,自己看吧,呵呵,这个必须的,就不多写了
jPanel1);
jPanel1setLayout(jPanel1Layout);
jPanel1Layout
setHorizontalGroup(jPanel1Layout
createParallelGroup(
javaxswingGroupLayoutAlignmentLEADING)
addGroup(
javaxswingGroupLayoutAlignmentTRAILING,
jPanel1LayoutcreateSequentialGroup()
addContainerGap(159, ShortMAX_VALUE)
addComponent(jLabel1)addGap(140, 140,
140))
addGroup(
jPanel1Layout
createSequentialGroup()
addGap(110, 110, 110)
addGroup(
jPanel1Layout
createParallelGroup(
javaxswingGroupLayoutAlignmentLEADING)
addComponent(jLabel2)
addComponent(jLabel3))
addGap(54, 54, 54)
addGroup(
jPanel1Layout
createParallelGroup(
javaxswingGroupLayoutAlignmentLEADING)
addGroup(
jPanel1Layout
createSequentialGroup()
addComponent(
jTextField2,
javaxswingGroupLayoutDEFAULT_SIZE,
191,
ShortMAX_VALUE)
addGap(
112,
112,
112))
addGroup(
jPanel1Layout
createSequentialGroup()
addComponent(
jTextField1,
javaxswingGroupLayoutPREFERRED_SIZE,
191,
javaxswingGroupLayoutPREFERRED_SIZE)
addContainerGap())))
addGroup(
jPanel1Layout
createSequentialGroup()
addGap(141, 141, 141)
addComponent(
jButton1,
javaxswingGroupLayoutPREFERRED_SIZE,
68,
javaxswingGroupLayoutPREFERRED_SIZE)
addGap(89, 89, 89)
addComponent(
jButton2,
javaxswingGroupLayoutPREFERRED_SIZE,
72,
javaxswingGroupLayoutPREFERRED_SIZE)
addContainerGap(145, ShortMAX_VALUE)));
jPanel1Layout
setVerticalGroup(jPanel1Layout
createParallelGroup(
javaxswingGroupLayoutAlignmentLEADING)
addGroup(
jPanel1Layout
createSequentialGroup()
addContainerGap()
addComponent(jLabel1)
addGap(55, 55, 55)
addGroup(
jPanel1Layout
createParallelGroup(
javaxswingGroupLayoutAlignmentLEADING)
addComponent(jLabel2)
addComponent(
jTextField1,
javaxswingGroupLayoutPREFERRED_SIZE,
javaxswingGroupLayoutDEFAULT_SIZE,
javaxswingGroupLayoutPREFERRED_SIZE))
addGap(41, 41, 41)
addGroup(
jPanel1Layout
createParallelGroup(
javaxswingGroupLayoutAlignmentBASELINE)
addComponent(jLabel3)
addComponent(
jTextField2,
javaxswingGroupLayoutPREFERRED_SIZE,
javaxswingGroupLayoutDEFAULT_SIZE,
javaxswingGroupLayoutPREFERRED_SIZE))
addGap(43, 43, 43)
addGroup(
jPanel1Layout
createParallelGroup(
javaxswingGroupLayoutAlignmentBASELINE)
addComponent(
jButton1,
javaxswingGroupLayoutPREFERRED_SIZE,
33,
javaxswingGroupLayoutPREFERRED_SIZE)
addComponent(
jButton2,
javaxswingGroupLayoutPREFERRED_SIZE,
33,
javaxswingGroupLayoutPREFERRED_SIZE))
addContainerGap(48, ShortMAX_VALUE)));
javaxswingGroupLayout layout = new javaxswingGroupLayout(
getContentPane());
getContentPane()setLayout(layout);
layoutsetHorizontalGroup(layoutcreateParallelGroup(
javaxswingGroupLayoutAlignmentLEADING)addGroup(
layoutcreateSequentialGroup()addContainerGap()addComponent(
jPanel1, javaxswingGroupLayoutDEFAULT_SIZE,
javaxswingGroupLayoutDEFAULT_SIZE, ShortMAX_VALUE)
addContainerGap()));
layoutsetVerticalGroup(layoutcreateParallelGroup(
javaxswingGroupLayoutAlignmentLEADING)addGroup(
layoutcreateSequentialGroup()addContainerGap()addComponent(
jPanel1, javaxswingGroupLayoutPREFERRED_SIZE,
javaxswingGroupLayoutDEFAULT_SIZE,
javaxswingGroupLayoutPREFERRED_SIZE)
addContainerGap(javaxswingGroupLayoutDEFAULT_SIZE,
ShortMAX_VALUE)));
pack();
setLocationRelativeTo(null); //居中
}// </editor-fold>
//GEN-END:initComponents
private void jButton1ActionPerformed(javaawteventActionEvent evt) { //事件处理,不建议这样做
// TODO add your handling code here:
String name = thisjTextField1getText()trim(); //获得用户名,去空格
String password = thisjTextField2getText()trim(); //获得密码,去空格
if (nameequals("admin") && passwordequals("123456")) { //如果符合条件其实这里应该去和数据库关联,这是死代码
// Showinformation sf=new Showinformation(); //我估计是另外一个框架,就是登录成功之后的框架
// sfsetVisible(true);
// sfshow(); //这个方法就直接秒杀,不用了,因为sfsetVisible(true)已经被调用了,这里就多次一举了
} else { //用户名或者密码验证失败
JOptionPaneshowMessageDialog(this, "用户名或者密码错误啦~", "提示", //提示框
JOptionPaneINFORMATION_MESSAGE);
thisjTextField1setText(""); //清空输入框
thisjTextField2setText("");
}
}
private void jButton2ActionPerformed(javaawteventActionEvent evt) { //事件处理,其实可以将jButton1ActionPerformed放在一起处理,只要获得按钮的标签内容 (JButton)(evtgetSuorse())getText()就做对比就ko了
// TODO add your handling code here:
Systemexit(0);
}
/
@param args the command line arguments
/
public static void main(String args[]) {
javaawtEventQueueinvokeLater(new Runnable() { //好方法,呵呵使用该方式的原因是:awt是单线程模式的,所有awt的组件只能在(推荐方式)事件处理线程中访问,从而保证组件状态的可确定性。java核心编程技术中每个实例基本都用到了
public void run() {
new Main()setVisible(true);
}
});
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javaxswingJButton jButton1;
private javaxswingJButton jButton2;
private javaxswingJLabel jLabel1;
private javaxswingJLabel jLabel2;
private javaxswingJLabel jLabel3;
private javaxswingJPanel jPanel1;
private javaxswingJTextField jTextField1;
private javaxswingJTextField jTextField2;}
// End of variables declaration//GEN-END:variables
你修改一下吧,从记事本获得用户名判断一下就行了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)