我在写一个银行自动提款机的小项目 第一次写 有点儿问题不知道怎么解决 请大家帮帮忙了 下面是一部分代码:
这是一个窗口类:
package com.xxgcdx.bank;
import java.awt.*;
import java.awt.event.*;import javax.swing.*;public class Bank {
JFrame frame = new JFrame("银行系统");
JButton button1 = new JButton("确  定");
JButton button2 = new JButton("取  消");
JLabel label1 = new JLabel("卡  号:");
JLabel label2 = new JLabel("密  码:");
JLabel label3 = new JLabel("请确保旁边无人!");
JTextField text1 = new JTextField("");
JPasswordField text2 = new JPasswordField("");

JLabel label4 = new JLabel("密码错误!请重新输入...(不超过三次)"); public Bank() {
init();
} private void init() {
frame.setLayout(null);
frame.add(button1);
frame.add(button2);
frame.add(label1);
frame.add(label2);
frame.add(label3);
frame.add(text1);
frame.add(text2);
frame.setBounds(150, 100, 500, 450);
button1.setBounds(100,300,90,50);
button2.setBounds(300,300,90,50);
label1.setBounds(40, 30, 100, 80);
label2.setBounds(40, 100, 100, 80);
label3.setBounds(280,180,160,40);
text1.setBounds(140,50,320,50);
text2.setBounds(140,118,320,50);
text2.setEchoChar('*');
button1.setFont(new Font(Font.DIALOG,Font.BOLD,20));     //设置每个组件字体
button2.setFont(new Font(Font.DIALOG,Font.BOLD,20));
text1.setFont(new Font(Font.DIALOG,Font.BOLD,35));
text2.setFont(new Font(Font.DIALOG,Font.BOLD,35));
label1.setFont(new Font(Font.DIALOG,Font.BOLD,25));
label2.setFont(new Font(Font.DIALOG,Font.BOLD,25));
label3.setFont(new Font(Font.DIALOG,Font.BOLD,18));
frame.setVisible(true); text1.addKeyListener(new KeyAdapter() {   //卡号输入框限制
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (Character.isDigit(c) && 
text1.getText().trim().length() < 16) //只允许数字,且长度不大于16
return;
e.consume();       //如果满足则响应事件

}
}); text2.addKeyListener(new KeyAdapter() {              //密码输入框限定
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (Character.isDigit(c) && 
text2.getText().trim().length() < 6) //只允许数字,且长度不大于6
return;
e.consume(); }
}); button2.addActionListener(new ActionListener() {     //设置取消按钮事件响应
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "取  消") {
text1.setText("");
text2.setText("");
}
}
});
}

public void wrong() {
frame.add(label4);
label4.setBounds(60,250,250,40);
}}这是用户类:
import java.awt.event.*;public class User extends Bank implements MyData {
private Card card;

public User(Card card) {
this.card = card;
while(card.get_account() == text1.getText()) {
if(card.get_password() == text2.getText()) {
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("ok");
}
});
}
else {
//bank.wrong();
}
}

}
public static void main(String[] args) {
User u = new User(new Card(account,password,name,999.99));

}
}现在的问题是我红色那部分代码怎么没有响应
就是一切输入都对的情况下应该在控制台打印ok  但是没有反应啊 我查API  它说JTextField不能对改变做出监听 要实现什么DocumentListener接口 
不知道怎么办了 我也不知道是不是这儿原因 请大家帮帮解决下 要怎么才能监听到文本框的输入
如果是要实现DocumentListener接口,那键盘监听器接口怎么办?
也请大家指点下程序不合理之处
小弟的第一个项目  请大家不要见笑
小弟先谢谢大家了

解决方案 »

  1.   

    please use PlaitDocument object to monitor the JTextField component activityit's very easy to use, more information just google internet
      

  2.   

    在基于 JTextComponent 的组件中,通过 DocumentEvent 将更改从模型传播到 DocumentListeners。DocumentEvent 给出了更改的位置和更改种类(如果需要)。代码片段可能看起来如下所示:DocumentListener myListener = ??;
    TextField myArea = ??;
    myArea.getDocument().addDocumentListener(myListener);
      

  3.   

    如楼上所说,重点在于xxx.getDocument().addxxxlistener