java做图书管理系统(4)fram类

  1 package com.java1234.view;
  2 
  3 import java.awt.EventQueue;
  4 import java.awt.Font;
  5 import java.awt.event.ActionEvent;
  6 import java.awt.event.ActionListener;
  7 
  8 import javax.swing.GroupLayout;
  9 import javax.swing.GroupLayout.Alignment;
 10 import javax.swing.ImageIcon;
 11 import javax.swing.JButton;
 12 import javax.swing.JFrame;
 13 import javax.swing.JLabel;
 14 import javax.swing.JOptionPane;
 15 import javax.swing.JPanel;
 16 import javax.swing.JPasswordField;
 17 import javax.swing.JTextField;
 18 import javax.swing.LayoutStyle.ComponentPlacement;
 19 import javax.swing.border.EmptyBorder;
 20 
 21 import com.java1234.dao.UserDao;
 22 import com.java1234.model.User;
 23 import com.java1234.util.DbUtil;
 24 import com.java1234.util.StringUtil;
 25 import com.mysql.jdbc.Connection;
 26 
 27 
 28 public class LogOnFrm extends JFrame {
 29 
 30     private JPanel contentPane;
 31     private final JTextField textField = new JTextField();
 32     private JPasswordField passwordTxt;
 33 
 34     private DbUtil dbUtil = new DbUtil();
 35     private UserDao userDao = new UserDao();
 36     private JTextField userNameTxt;
 37 
 38 
 39     /**
 40      * Launch the application.
 41      */
 42     public static void main(String[] args) {
 43         EventQueue.invokeLater(new Runnable() {
 44             public void run() {
 45                 try {
 46                     LogOnFrm frame = new LogOnFrm();
 47                     frame.setVisible(true);
 48                 } catch (Exception e) {
 49                     e.printStackTrace();
 50                 }
 51             }
 52         });
 53     }
 54 
 55     /**
 56      * Create the frame.
 57      */
 58     public LogOnFrm() {
 59         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 60         setAlwaysOnTop(true);
 61         setTitle("管理员登录");
 62         setBounds(100, 100, 450, 300);
 63         contentPane = new JPanel();
 64         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
 65         setContentPane(contentPane);
 66 
 67         JLabel label = new JLabel("图书管理系统");
 68         label.setFont(new Font("黑体", Font.BOLD, 25));
 69         label.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/logo.png")));
 70 
 71         JLabel lblNewLabel = new JLabel("用户名:");
 72         lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 13));
 73         lblNewLabel.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/userName.png")));
 74 
 75         JLabel lblNewLabel_1 = new JLabel("密  码:");
 76         lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 13));
 77         lblNewLabel_1.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/password.png")));
 78         textField.setColumns(10);
 79 
 80         passwordTxt = new JPasswordField();
 81 
 82         JButton btnNewButton = new JButton("登录");
 83         btnNewButton.addActionListener(new ActionListener() {
 84             public void actionPerformed(ActionEvent e) {
 85                 loginActionPerformed(e);    
 86             }
 87         });
 88         btnNewButton.setFont(new Font("宋体", Font.PLAIN, 13));
 89 
 90         btnNewButton.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/login.png")));
 91 
 92         JButton btnNewButton_1 = new JButton("重置");
 93         btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 13));
 94         btnNewButton_1.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/reset.png")));
 95         btnNewButton_1.addActionListener(new ActionListener() {
 96             public void actionPerformed(ActionEvent e) {
 97                 resetValueActionPerformed(e);
 98             }
 99         });
100 
101         userNameTxt = new JTextField();
102         userNameTxt.setColumns(10);
103         GroupLayout gl_contentPane = new GroupLayout(contentPane);
104         gl_contentPane.setHorizontalGroup(
105             gl_contentPane.createParallelGroup(Alignment.LEADING)
106                 .addGroup(gl_contentPane.createSequentialGroup()
107                     .addContainerGap()
108                     .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
109                         .addGroup(gl_contentPane.createSequentialGroup()
110                             .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
111                                 .addGroup(gl_contentPane.createSequentialGroup()
112                                     .addComponent(textField, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
113                                     .addGap(223))
114                                 .addGroup(gl_contentPane.createSequentialGroup()
115                                     .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
116                                         .addComponent(lblNewLabel_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
117                                         .addComponent(btnNewButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
118                                     .addGap(50))
119                                 .addGroup(gl_contentPane.createSequentialGroup()
120                                     .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
121                                     .addGap(36)))
122                             .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
123                                 .addComponent(btnNewButton_1)
124                                 .addGroup(Alignment.LEADING, gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
125                                     .addComponent(passwordTxt, Alignment.LEADING)
126                                     .addComponent(userNameTxt, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)))
127                             .addGap(63))
128                         .addGroup(gl_contentPane.createSequentialGroup()
129                             .addComponent(label, GroupLayout.PREFERRED_SIZE, 320, GroupLayout.PREFERRED_SIZE)
130                             .addContainerGap())))
131         );
132         gl_contentPane.setVerticalGroup(
133             gl_contentPane.createParallelGroup(Alignment.LEADING)
134                 .addGroup(gl_contentPane.createSequentialGroup()
135                     .addGap(18)
136                     .addComponent(label)
137                     .addPreferredGap(ComponentPlacement.UNRELATED)
138                     .addComponent(textField, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
139                     .addGap(14)
140                     .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
141                         .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
142                         .addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
143                     .addGap(45)
144                     .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
145                         .addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
146                         .addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
147                     .addGap(27)
148                     .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
149                         .addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
150                         .addComponent(btnNewButton))
151                     .addContainerGap(13, Short.MAX_VALUE))
152         );
153         contentPane.setLayout(gl_contentPane);
154 
155         //设置居中显示
156         this.setLocationRelativeTo(null);
157     }
158 
159     /**
160      * 登录事件处理
161      * @param e
162      */
163     protected void loginActionPerformed(ActionEvent e) {
164         // TODO Auto-generated method stub
165         String userName = this.userNameTxt.getText();
166         String password = new String(this.passwordTxt.getPassword());
167         if(StringUtil.isEmpty(userName)){
168             JOptionPane.showMessageDialog(null,"用户名不能为空");
169             return;
170         }
171         if(StringUtil.isEmpty(password)){
172             JOptionPane.showMessageDialog(null,"密码不能为空");
173             return;
174         }
175         User user = new User(userName,password);
176         Connection con = null;
177         try{
178             con = dbUtil.getCon();
179             User currentUser=userDao.login(con, user);
180             if(currentUser!=null){
181                 //JOptionPane.showMessageDialog(null,"登录成功");   
182                 dispose();
183                 new MainFrm().setVisible(true);
184             }else{
185                 JOptionPane.showMessageDialog(null,"用户名密码错误");
186             }
187 
188         }catch(Exception e1){
189             e1.printStackTrace();
190 
191         }
192 
193     }
194 
195     /**
196      * 重置事件处理
197      * @param evt
198      */
199 
200     private void resetValueActionPerformed(ActionEvent evt) {
201         // TODO Auto-generated method stub
202         this.userNameTxt.setText("");
203         this.passwordTxt.setText("");
204     }
205 }

 

posted on 2017-11-25 18:23  jun俊  阅读(780)  评论(0)    收藏  举报

导航