我写了个登陆的对话框,想要输入用户名和密码后,连接指定的数据库。可在实现keyListener()接口的时候出现点问题。我是这样写的:package Test;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Logon extends JFrame implements ActionListener,KeyListener{  JLabel usernameLabel = new JLabel("Username :",JLabel.LEFT);
  JTextField username = new JTextField(20);
  JLabel passwordLabel = new JLabel("Password :",JLabel.LEFT);
  JPasswordField password = new JPasswordField(20);
  JButton connect = new JButton("Connect");
  JButton cancel = new JButton("Cancel");
  String uname;
  String pword;void buildConstraints(GridBagConstraints gbc ,
                        int gx,
                        int gy,
                        int gw,
                        int gh,
                        int wx,
                        int wy){
    gbc.gridx = gx;
    gbc.gridy = gy;
    gbc.gridwidth = gw;
    gbc.gridheight = gh;
    gbc.weightx = wx;
    gbc.weighty = wy;
  }  public Logon() {
    super("Database Logon");
    setBounds(400,300,270,170);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel pane = new JPanel();
    GridBagLayout gb = new GridBagLayout(); //create a grid layout
    GridBagConstraints constraint = new GridBagConstraints(); //create constraint
    pane.setLayout(gb);    //put the "usernameLabel" to the grid layout
    buildConstraints(constraint,0,0,1,1,25,25);
    constraint.fill = GridBagConstraints.NONE;
    constraint.anchor = GridBagConstraints.EAST;
    gb.setConstraints(usernameLabel,constraint);
    pane.add(usernameLabel);    //put the "username" to the grid layout
    username.addKeyListener(this);
    buildConstraints(constraint,1,0,1,1,75,0);
    constraint.fill = GridBagConstraints.HORIZONTAL;
    gb.setConstraints(username,constraint);
    pane.add(username);    //put the "passwordLabel" to the grid layout
    buildConstraints(constraint,0,1,1,1,0,25);
    constraint.fill = GridBagConstraints.NONE;
    constraint.anchor = GridBagConstraints.EAST;
    gb.setConstraints(passwordLabel,constraint);
    pane.add(passwordLabel);    //put the "password" to the grid layout
    password.addKeyListener(this);
    password.setEchoChar('*');
    buildConstraints(constraint,1,1,1,1,0,0);
    constraint.fill = GridBagConstraints.HORIZONTAL;
    gb.setConstraints(password,constraint);
    pane.add(password); 
    //put the "connect" button to the grid layout
     connect.addActionListener(this);
    buildConstraints(constraint,0,3,1,1,0,25);
    constraint.fill = GridBagConstraints.NONE;
    constraint.anchor = GridBagConstraints.CENTER;
    gb.setConstraints(connect,constraint);
    pane.add(connect);    //put the "cancel" button to the grid layout
    cancel.addActionListener(this);
    buildConstraints(constraint,1,3,1,1,0,25);
    constraint.fill = GridBagConstraints.NONE;
    constraint.anchor = GridBagConstraints.WEST;
    gb.setConstraints(cancel,constraint);
    pane.add(cancel);    setContentPane(pane);
    show();
  }
  public static void main(String[] args){
    Logon log = new Logon();  }
  public void keyPressed(KeyEvent kevt){}
  public void keyReleased(KeyEvent kevt){}
  
  public void keyTyped(KeyEvent kevt){
    Object ksrc = kevt.getSource();
    if(ksrc == username){
     uname = kevt.paramString();
    }
    if (ksrc == password){
     pword = kevt.paramString();
    }
  }
  
  public void actionPerformed(ActionEvent evt){
    Object src = evt.getSource();
    //若点击“Cancel”按钮,则退出程序
    if (src == cancel)
     System.exit(0);
   //若点击“Connect”按钮,则连接数据库,并将查询的结果写入到一个文本文件中
    else if (src == connect)      {DBConnection conn = new DBConnection("jdbc:oracle:thin:@test:1521:test",uname,pword);
      System.exit(0);
    }    }
}
我调试了一下代码,uname只有一个字符。比如我输入“test”,那么uname会分别为“t”、“e”、“s”、“t”。是我的程序哪里有问题吗?怎样才能使uname的值为输入的“test”?望高手指教!

解决方案 »

  1.   

    package tony.test;import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class Logon extends JFrame implements ActionListener, KeyListener {    JLabel usernameLabel = new JLabel("Username :", JLabel.LEFT);
        JTextField username = new JTextField(20);
        JLabel passwordLabel = new JLabel("Password :", JLabel.LEFT);
        JPasswordField password = new JPasswordField(20);
        JButton connect = new JButton("Connect");
        JButton cancel = new JButton("Cancel");
        String uname;
        String pword;    void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw,
                int gh, int wx, int wy) {
            gbc.gridx = gx;
            gbc.gridy = gy;
            gbc.gridwidth = gw;
            gbc.gridheight = gh;
            gbc.weightx = wx;
            gbc.weighty = wy;
        }    public Logon() {
            super("Database Logon");
            setBounds(400, 300, 270, 170);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            JPanel pane = new JPanel();
            GridBagLayout gb = new GridBagLayout(); // create a grid layout
            GridBagConstraints constraint = new GridBagConstraints(); // create
            // constraint
            pane.setLayout(gb);        // put the "usernameLabel" to the grid layout
            buildConstraints(constraint, 0, 0, 1, 1, 25, 25);
            constraint.fill = GridBagConstraints.NONE;
            constraint.anchor = GridBagConstraints.EAST;
            gb.setConstraints(usernameLabel, constraint);
            pane.add(usernameLabel);        // put the "username" to the grid layout
            username.addKeyListener(this);
            buildConstraints(constraint, 1, 0, 1, 1, 75, 0);
            constraint.fill = GridBagConstraints.HORIZONTAL;
            gb.setConstraints(username, constraint);
            pane.add(username);        // put the "passwordLabel" to the grid layout
            buildConstraints(constraint, 0, 1, 1, 1, 0, 25);
            constraint.fill = GridBagConstraints.NONE;
            constraint.anchor = GridBagConstraints.EAST;
            gb.setConstraints(passwordLabel, constraint);
            pane.add(passwordLabel);        // put the "password" to the grid layout
            password.addKeyListener(this);
            password.setEchoChar('*');
            buildConstraints(constraint, 1, 1, 1, 1, 0, 0);
            constraint.fill = GridBagConstraints.HORIZONTAL;
            gb.setConstraints(password, constraint);
            pane.add(password);        // put the "connect" button to the grid layout
            connect.addActionListener(this);
            buildConstraints(constraint, 0, 3, 1, 1, 0, 25);
            constraint.fill = GridBagConstraints.NONE;
            constraint.anchor = GridBagConstraints.CENTER;
            gb.setConstraints(connect, constraint);
            pane.add(connect);        // put the "cancel" button to the grid layout
            cancel.addActionListener(this);
            buildConstraints(constraint, 1, 3, 1, 1, 0, 25);
            constraint.fill = GridBagConstraints.NONE;
            constraint.anchor = GridBagConstraints.WEST;
            gb.setConstraints(cancel, constraint);
            pane.add(cancel);        setContentPane(pane);
            show();
        }    public static void main(String[] args) {
            Logon log = new Logon();    }    public void keyPressed(KeyEvent kevt) {
        }    public void keyReleased(KeyEvent kevt) {
        }    public void keyTyped(KeyEvent kevt) {
            Object ksrc = kevt.getSource();
            if (ksrc == username) {
                uname = kevt.paramString();
            }
            if (ksrc == password) {
                pword = kevt.paramString();
            }
        }    public void actionPerformed(ActionEvent evt) {
            Object src = evt.getSource();
            // 若点击“Cancel”按钮,则退出程序
            if (src == cancel) {
                System.exit(0);
            }
            // 若点击“Connect”按钮,则连接数据库,并将查询的结果写入到一个文本文件中
            else if (src == connect) {
                //test code
                uname = username.getText().trim();
                pword = password.getText().trim();
                System.out.println(uname);
                System.out.println(pword);
                
                // TODO: write your login code here 
            }    }
    }
      

  2.   

    pword = password.getText().trim();
    该语句有问题,提示“getText() in javax.swing.JPasswordFiled has been deprecated”。password和username不是同一类型的对象。
      

  3.   

    把pword = password.getText().trim();
    改成            pword = new String(password.getPassword());
                pword.trim();
      

  4.   

    我将:
    public void keyPressed(KeyEvent kevt) {
        }    public void keyReleased(KeyEvent kevt) {
        }    public void keyTyped(KeyEvent kevt) {
            Object ksrc = kevt.getSource();
            if (ksrc == username) {
                uname = kevt.paramString();
            }
            if (ksrc == password) {
                pword = kevt.paramString();
            }
        }
    给去掉,也还是可以运行的。那我就奇怪了,KeyListener什么时候用呀?
      

  5.   

    对事件还是不是了解的太清楚。还请高手指教一二。最好能给个教程的url。谢了!