import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Login extends JFrame implements ActionListener
{
//声明实例变量
private JLabel LogImg,user,pass;
private JTextField input_user;
private JPanel groupPanel;
private JPasswordField PSWField;
private JButton buttonOK;
private JButton buttonCancel;
private static int flag = 0;
//构造方法
public Login()
{
setAlwaysOnTop(true);
setLayout(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
user = new JLabel("用 户:");
pass = new JLabel("密 码:");
user.setForeground(Color.blue);
pass.setForeground(Color.blue);

buttonOK = new JButton();
buttonOK.setBounds(430,145,55,20);
Icon rollover = new ImageIcon("source\\login2.png");
Icon general = new ImageIcon("source\\login1.png");
buttonOK.setIcon(general);
buttonOK.setRolloverIcon(rollover);

buttonCancel = new JButton();
buttonCancel.setBounds(430,200,55,20);
Icon rollover2 = new ImageIcon("source\\exit2.png");
Icon general2 = new ImageIcon("source\\exit1.png");
buttonCancel.setIcon(general2);
buttonCancel.setRolloverIcon(rollover2);
buttonCancel.addActionListener(this);

input_user = new JTextField(10);
PSWField = new JPasswordField();

groupPanel = new JPanel(new GridLayout(4,1));
groupPanel.setBounds(410,5,95,100);
groupPanel.setBorder(BorderFactory.createLoweredBevelBorder());

groupPanel.add(user);
groupPanel.add(input_user);
groupPanel.add(pass);
groupPanel.add(PSWField);

LogImg = new JLabel();
LogImg.setBounds(2,2,400,273);
LogImg.setIcon(new ImageIcon("source\\login.jpg"));
buttonOK.addActionListener(this);

add(LogImg);
add(groupPanel);
add(buttonOK);
add(buttonCancel);

setResizable(false);
setSize(520,308);
CenterOfScreen.CenterOfScree(this);
setAlwaysOnTop(true);
setVisible(true);
}
//actionPerformed为覆盖接口中的方法,实现鼠鼠标响
public void actionPerformed(ActionEvent e)
{
    JButton tempButton = (JButton)e.getSource();
    if(tempButton == buttonOK)
    {
     judgeUser();
    }
    else
    {
     System.exit(0);
    }

}
//方法judgeUser()判断用户名和密码是否正确
public void judgeUser()
{
String tempUser,tempPsw;
tempUser = input_user.getText().trim();
tempPsw = new String(PSWField.getPassword());
if(tempUser.equals(""))
{
    JOptionPane.showMessageDialog(this, "用户名不能为空!","错误", JOptionPane.ERROR_MESSAGE);
    return;
}
ResultSet rs = null;
Statement st = ConnectDB.getSt();
try
{
String sqlString ="select * from id";
rs = st.executeQuery(sqlString);
while(rs.next())
{
String str = rs.getString("user");
if(str.equals(tempUser))
{
System.out.println("用户名正确!");
if(tempPsw.equals(rs.getString("password")))
{
System.out.println("登陆成功!");
flag = 2;
return;
}
else
{
    JOptionPane.showMessageDialog(this, "用户名或密码出错!","错误", JOptionPane.ERROR_MESSAGE);
    flag = 1;
    break;
}
}    
}
if(flag == 0)
JOptionPane.showMessageDialog(this, "用户名或密码出错!","错误", JOptionPane.ERROR_MESSAGE);
input_user.setText("");
PSWField.setText("");
}catch(SQLException e)
{ JOptionPane.showMessageDialog(this, "SQL出错"+e.getMessage(),"错误", JOptionPane.INFORMATION_MESSAGE); }
}
//主函数入口
public static void main(String[] args)
{
ConnectDB.connectData();
Login log = new Login();
System.out.println("flag ="+flag);
if(flag ==2)
log.setVisible(false);  //这里好象有点问题,总关闭不了本窗口。
}
}

解决方案 »

  1.   

    应该写一个DB接口,
    不应该在代码中随处是这种sql文。
      

  2.   

    不应该都写在一个文件中,整一个DB类。
    还有:
    //方法judgeUser()判断用户名和密码是否正确
        public void judgeUser()这个方法既然是判断用,咋还void型? 不规范啊。
      

  3.   

    一个登录窗口就这么长的代码,看来写JAVA代码真的好烦哦
      

  4.   


       Icon rollover2 = new ImageIcon("source\\exit2.png");
       Icon general2 = new ImageIcon("source\\exit1.png");
       buttonCancel.setIcon(general2);
       buttonCancel.setRolloverIcon(rollover2);
          //===>>>>写成这样比较好
           buttonCancel.setIcon(new ImageIcon("source\\exit1.png"));
           buttonCancel.setRolloverIcon(new ImageIcon("source\\exit2.png"));
      //  而且不要变量名后面加数字序号,有些变量名居然还大写字母开头
      // java中一般{放在同一行
      

  5.   

    从代码的规范性来说..很不规范
    从代码的安全性来说..SQL语句表露无疑
    从JAVA三特性:封装.继承.多态来说..还有很多需要改进的地方
    网上查查JAVA规范吧..代码写多了..其实也就有了自己的风格了
      

  6.   

    真的非常感谢大家,没想到大家这么热心,我最近JAVA碰到了低谷,写个小程序,也让我有了点信心,欢迎大家继续批评。
      

  7.   

    if(tempButton == buttonOK)
            {
                judgeUser();
            }
            else
            {
                System.exit(0);
            }
      

  8.   

    log.setVisible(false);
    这一句,为什么不直接就写退出算了,反正是登录不成功。
    System.exit(0)就行了。
    当然,退出时要释放数据库连接RS等资源。
    catch(SQLException e)一节中,flag似应给值,看是直接给个2让它退出算了或是别的什么处理,要不出错不易退出。