以下写的登陆功能有点问题,能帮我改改吗??package s1.pro.ch1;import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import s1.pro.DAO.DBManager;
import java.sql.*;public class LoginFrame extends JFrame {
public LoginFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
this.setTitle("登陆");
lblName.setText("用户名:");
lblName.setBounds(new Rectangle(80, 80, 50, 25));
lblPassword.setText("密 码:");
lblPassword.setBounds(new Rectangle(80, 140, 50, 25));
txtName.setBounds(new Rectangle(200, 80, 90, 25));
Password.setBounds(new Rectangle(200, 140, 90, 25));
btnReset.setBounds(new Rectangle(235, 200, 80, 25));
btnReset.setText("重置");
btnReset.addActionListener(new LoginFrame_btnReset_actionAdapter(this));
btnSubmit.setBounds(new Rectangle(95, 200, 80, 25));
btnSubmit.setText("登陆");
btnSubmit.addActionListener(new LoginFrame_btnSubmit_actionAdapter(this));
dlgMessage.setBounds(new Rectangle(46, 248, 262, 90)); this.getContentPane().add(lblName);
this.getContentPane().add(btnReset);
this.getContentPane().add(btnSubmit);
this.getContentPane().add(txtName);
this.getContentPane().add(lblPassword);
this.getContentPane().add(Password);
this.getContentPane().add(Password);
}
JTextField txtName = new JTextField();
JPasswordField Password = new JPasswordField();
JLabel lblName = new JLabel();
JLabel lblPassword = new JLabel();
JButton btnSubmit = new JButton();
JButton btnReset = new JButton();
JOptionPane dlgMessage = new JOptionPane(); public void btnSubmit_actionPerformed(ActionEvent e) { String inputName = new String(txtName.getText());
String inputPassword = new String(Password.getPassword()); if (inputName.length() == 0 || inputPassword.length() == 0) {
JOptionPane.showMessageDialog(this, "用户名或密码不能为空!");
} try {
Connection con = new DBManager().getConnection();
PreparedStatement pstmt = con.prepareStatement(
"select * from OPERATOR");
ResultSet rs = pstmt.executeQuery();
//System.out.println(rs.next()); while (rs.next()) {
String name = rs.getString(1).trim();
String pass = rs.getString(2).trim();
//System.out.println("程序运行到1");
//System.out.println(name);
//System.out.println(pass); if (inputName.equals(name) && inputPassword.equals(pass)) {
//System.out.println("程序运行到2");
MM frame = new MM();
Dimension screenSize = Toolkit.getDefaultToolkit().
getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2); frame.setVisible(true);
dispose();
} else {
continue;
}
}
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
} catch (SQLException ex) {
ex.printStackTrace();
}
} public void btnReset_actionPerformed(ActionEvent e) {
txtName.setText("");
Password.setText("");
}
}
class LoginFrame_btnReset_actionAdapter implements ActionListener {
private LoginFrame adaptee;
LoginFrame_btnReset_actionAdapter(LoginFrame adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.btnReset_actionPerformed(e);
}
}
class LoginFrame_btnSubmit_actionAdapter implements ActionListener {
private LoginFrame adaptee;
LoginFrame_btnSubmit_actionAdapter(LoginFrame adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.btnSubmit_actionPerformed(e);
}
}
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import s1.pro.DAO.DBManager;
import java.sql.*;public class LoginFrame extends JFrame {
public LoginFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
this.setTitle("登陆");
lblName.setText("用户名:");
lblName.setBounds(new Rectangle(80, 80, 50, 25));
lblPassword.setText("密 码:");
lblPassword.setBounds(new Rectangle(80, 140, 50, 25));
txtName.setBounds(new Rectangle(200, 80, 90, 25));
Password.setBounds(new Rectangle(200, 140, 90, 25));
btnReset.setBounds(new Rectangle(235, 200, 80, 25));
btnReset.setText("重置");
btnReset.addActionListener(new LoginFrame_btnReset_actionAdapter(this));
btnSubmit.setBounds(new Rectangle(95, 200, 80, 25));
btnSubmit.setText("登陆");
btnSubmit.addActionListener(new LoginFrame_btnSubmit_actionAdapter(this));
dlgMessage.setBounds(new Rectangle(46, 248, 262, 90)); this.getContentPane().add(lblName);
this.getContentPane().add(btnReset);
this.getContentPane().add(btnSubmit);
this.getContentPane().add(txtName);
this.getContentPane().add(lblPassword);
this.getContentPane().add(Password);
this.getContentPane().add(Password);
}
JTextField txtName = new JTextField();
JPasswordField Password = new JPasswordField();
JLabel lblName = new JLabel();
JLabel lblPassword = new JLabel();
JButton btnSubmit = new JButton();
JButton btnReset = new JButton();
JOptionPane dlgMessage = new JOptionPane(); public void btnSubmit_actionPerformed(ActionEvent e) { String inputName = new String(txtName.getText());
String inputPassword = new String(Password.getPassword()); if (inputName.length() == 0 || inputPassword.length() == 0) {
JOptionPane.showMessageDialog(this, "用户名或密码不能为空!");
} try {
Connection con = new DBManager().getConnection();
PreparedStatement pstmt = con.prepareStatement(
"select * from OPERATOR");
ResultSet rs = pstmt.executeQuery();
//System.out.println(rs.next()); while (rs.next()) {
String name = rs.getString(1).trim();
String pass = rs.getString(2).trim();
//System.out.println("程序运行到1");
//System.out.println(name);
//System.out.println(pass); if (inputName.equals(name) && inputPassword.equals(pass)) {
//System.out.println("程序运行到2");
MM frame = new MM();
Dimension screenSize = Toolkit.getDefaultToolkit().
getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2); frame.setVisible(true);
dispose();
} else {
continue;
}
}
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
} catch (SQLException ex) {
ex.printStackTrace();
}
} public void btnReset_actionPerformed(ActionEvent e) {
txtName.setText("");
Password.setText("");
}
}
class LoginFrame_btnReset_actionAdapter implements ActionListener {
private LoginFrame adaptee;
LoginFrame_btnReset_actionAdapter(LoginFrame adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.btnReset_actionPerformed(e);
}
}
class LoginFrame_btnSubmit_actionAdapter implements ActionListener {
private LoginFrame adaptee;
LoginFrame_btnSubmit_actionAdapter(LoginFrame adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.btnSubmit_actionPerformed(e);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货