日期:2014-05-20  浏览次数:20896 次

一个文本框事件控制按钮启用状态的问题
我想在登录窗口的用户名中输入数据时登录按钮为启用状态,用户名中无数据时登录按钮为未启用状态,我说的不是在用户名中输入数据为按回车才后登录才为启用状态,是在用户名中输入数据时为启用状态

各位大神千万别因为结帖率的问题就不指点啊 ,我的帖子就两个没有结帖,一个是昨天发的,还有一个就是现在这个啊 

代码如下:
Java code

/**
 * @(#)Login.java
 *
 *
 * @author 
 * @version 1.00 2012/3/17
 */
 
 import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
 import java.sql.*;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 

 
 class FrameTest extends JFrame {
     public FrameTest() {
         super("请登录");
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setVisible(true);
         setSize(320, 240);
         
     }
     
 }
 
 class PanelTest extends FrameTest {
     JPanel panelObj;
     public PanelTest() {
         panelObj = new JPanel();
         getContentPane().add(panelObj);
         
     }
     
 }
 
 class ComponentTest extends PanelTest {
     JLabel loginName;
     JLabel loginPassword;
     JTextField textName;
     JPasswordField textPassword;
     JButton buttonLogin;
     JButton buttonReset;
     
     public ComponentTest() {
         loginName     = new JLabel("用户名:");
         loginPassword = new JLabel("密码:");
         buttonLogin   = new JButton("登录");
         buttonReset   = new JButton("重置");
         textName      = new JTextField(10);
         textPassword  = new JPasswordField(10);
         
         panelObj.add(loginName);
         panelObj.add(textName);
         panelObj.add(loginPassword);
         panelObj.add(textPassword);
         panelObj.add(buttonLogin);
         panelObj.add(buttonReset);
                  
     }
     
 }
 
 public class Login extends ComponentTest {
     GridBagLayout gbLayoutObj; 
    GridBagConstraints gbcObj;
    boolean b = false;
    
    public Login() {
        
        gbLayoutObj   = new GridBagLayout();
        gbcObj        = new GridBagConstraints();
        panelObj.setLayout(gbLayoutObj);
        
        gbcObj.anchor = GridBagConstraints.EAST;
        gbcObj.gridx  = 1;
        gbcObj.gridy  = 1;
        gbLayoutObj.setConstraints(loginName,gbcObj);
        gbcObj.gridy  = 2;
        gbLayoutObj.setConstraints(loginPassword,gbcObj);
        
        gbcObj.anchor = GridBagConstraints.WEST;
        gbcObj.gridx  = 2;
        gbcObj.gridy  = 1;
        gbLayoutObj.setConstraints(textName,gbcObj);
        gbcObj.gridy  = 2;
        gbLayoutObj.setConstraints(textPassword,gbcObj);
        
        gbcObj.anchor = GridBagConstraints.SOUTH;
        gbcObj.gridx  = 1;
        gbcObj.gridy  = 3;
        gbLayoutObj.setConstraints(buttonLogin,gbcObj);
        gbcObj.gridx  = 2;
        gbLayoutObj.setConstraints(buttonReset,gbcObj);
        
        buttonLogin.setEnabled(true);
        
        buttonReset.addActionListener (
             new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                     Login.this.Reset();
                 }
             }
         );
         
         //textName.addActionListener(this);
         
         buttonLogin.addActionListener (
             new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                     Login.this.f();
                 }
             }
         );
         
    }
    
    protected void Reset() {
        textName.setText("");
        textPassword.setText("");
    }
    
   /* public void keyPressed(KeyEvent e) {
        buttonLogin.setEnabled(true);
    }
    
    public void keyReleased(KeyEvent e) {
        
    }
    
    public void keyTyped(KeyEvent e) {
        
    }*/
    
    protected boolean f() {        
        String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=J://course/AddressBook/Test/DataBase/Login.mdb";
                
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection(url);
            Statement st = con.createStatement();
            String name = textName.getText().trim();
            String pwd = textPassword.getText().trim();
            ResultSet rs = st.executeQuery("select * from LoginMessage where ID = '" +
                 name + "'");
            if(rs.next()) {
                rs.close();
                rs = st.executeQuery("select * from LoginMessage where ID = '" +
                    name + "' and Pwd = '" + pwd + "'");
                if(rs.next()) {
                    JOptionPane.showMessageDialog(new JPanel(), "恭喜!!!登录成功");
                    setVisible(false);
                    TestFrame frame = new TestFrame();
                
                    // Show frame.
                    frame.show(true);
                }
                else {
                    JOptionPane.showMessageDialog(new JPanel(), "密码输入错误!请确认后再次输入");
                }
            }
            else {
                JOptionPane.showMessageDialog(new JPanel(), "用户名不存在");
            }
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        
        return b;

    }
    
    
 }

/*class LoginData {
    public boolean f () {        
        String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=J://course/AddressBook/Test/DataBase/Login.mdb";
        boolean bz = false;    
                
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection(url);
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from LoginMessage where ID = " +
                 textName.getText() + "and Pwd = " + textPassword.getText());
            while(rs.next()) {
                bz = true;
                rs.close();
                st.close();
                con.close();
            }
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        
        return bz;
    }
}*/