java swing编程基础(二)

LoginFrame.java
/*
* LoginFrame.java
*
* Created on __DATE__, __TIME__
*/

package com.ruif.view;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import com.ruif.dao.UserDAO;
import com.ruif.model.User;
import com.ruif.util.DBUtil;
import com.ruif.util.StringUtil;

/**
*
* @author __USER__
*/
public class LoginFrame extends javax.swing.JFrame
{

/**
*
*/
private static final long serialVersionUID = 938007790081424935L;

DBUtil dbUtil = new DBUtil();
UserDAO userDao = new UserDAO();

/** Creates new form LoginFrame */
public LoginFrame()
{
initComponents();
this.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()
{

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField_name = new javax.swing.JTextField();
jButton_login = new javax.swing.JButton();
jButton_reset = new javax.swing.JButton();
jPasswordField_pwd = new javax.swing.JPasswordField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setIconImage(getIconImage());

jLabel1.setFont(new java.awt.Font("宋体", 1, 24));
jLabel1.setText("\u6e29\u5ea6\u76d1\u6d4b\u7cfb\u7edf\u767b\u9646");

jLabel2.setIcon(new ImageIcon(
"F:\\WorkSpaces\\ruif_swing\\images\\username.png")); // NOI18N

jLabel2.setText("\u7528\u6237\u540d\uff1a");

jLabel3.setIcon(new ImageIcon(
"F:\\WorkSpaces\\ruif_swing\\images\\password.png")); // NOI18N
jLabel3.setText("\u5bc6 \u7801\uff1a");

jButton_login.setIcon(new ImageIcon(
"F:\\WorkSpaces\\ruif_swing\\images\\login.png")); // NOI18N
jButton_login.setText("\u767b\u9646");
jButton_login.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton_loginActionPerformed(evt);
}
});

jButton_reset.setIcon(new javax.swing.ImageIcon(
"F:\\WorkSpaces\\ruif_swing\\images\\reset.png")); // NOI18N
jButton_reset.setText("\u91cd\u7f6e");
jButton_reset.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton_resetActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(70, 70,
70)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
jLabel2)
.addComponent(
jLabel3)
.addComponent(
jButton_login,
javax.swing.GroupLayout.PREFERRED_SIZE,
94,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
false)
.addComponent(
jPasswordField_pwd,
javax.swing.GroupLayout.Alignment.LEADING,
0,
0,
Short.MAX_VALUE)
.addComponent(
jTextField_name,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE,
151,
Short.MAX_VALUE))
.addComponent(
jButton_reset,
javax.swing.GroupLayout.PREFERRED_SIZE,
94,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(
layout.createSequentialGroup()
.addGap(97, 97,
97)
.addComponent(
jLabel1)))
.addContainerGap(92, Short.MAX_VALUE)));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addComponent(jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
46,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(56, 56, 56)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(
jTextField_name,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(
jPasswordField_pwd,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(29, 29, 29)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
jButton_login,
javax.swing.GroupLayout.PREFERRED_SIZE,
32,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
jButton_reset,
javax.swing.GroupLayout.DEFAULT_SIZE,
34, Short.MAX_VALUE))
.addContainerGap()));

pack();
}// </editor-fold>
//GEN-END:initComponents

private void jButton_loginActionPerformed(java.awt.event.ActionEvent evt)
{

String name = this.jTextField_name.getText().trim();
String password = new String(this.jPasswordField_pwd.getPassword());
if (StringUtil.isEmpty(name))
{
JOptionPane.showMessageDialog(null, "用户名不能为空!");
}
if (StringUtil.isEmpty(password))
{
JOptionPane.showMessageDialog(null, "用户密码不能为空!");
}

User user = new User(name, password);
try
{
User currentUser = userDao.login(dbUtil.getConn(), user);
if (currentUser != null)
{
JOptionPane.showMessageDialog(null, "登陆成功!");
this.dispose();
new MainFrame().setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "登陆失败!");
}

}

private void jButton_resetActionPerformed(java.awt.event.ActionEvent evt)
{
resetValue();
}

private void resetValue()
{
this.jTextField_name.setText("");
this.jPasswordField_pwd.setText("");
}

/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new LoginFrame().setVisible(true);
}
});
}

//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton_login;
private javax.swing.JButton jButton_reset;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPasswordField jPasswordField_pwd;
private javax.swing.JTextField jTextField_name;
// End of variables declaration//GEN-END:variables

}


--来自网页客户端
二维码.jpg
已邀请:

要回复问题请先登录注册

Template error, template file not found