import java.awt.*;
import java.awt.event.*;public class Mark__524 implements ActionListener,KeyListener
{
static Frame f;
static TextField tf=new TextField("I Love you,How are you?",20);
static TextArea ta=new TextArea(5,23);
static Button b1=new Button("Exit");
static Button b2=new Button("End");
int tr=0,fa=0,w=0;
char[] ch=new char[23];

public static void main(String args[])
{
new Mark__524().init();
}

public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub }
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

} public void keyTyped(KeyEvent e) 
{
// TODO Auto-generated method stub
if(e.getKeyChar()==ch[w] && w!=tf.getText().length())
tr++;
fa++;
w++;
}
public void init()
{
f=new Frame("打字练习");
f.setLayout(new FlowLayout());
f.setSize(200,200);
f.add(tf);f.add(ta);f.add(b1);f.add(b2);
f.setVisible(true);

ta.addKeyListener(this);
b1.addActionListener(new Mark__524());
b2.addActionListener(new Mark__524());

String str=tf.getText();
ch=str.toCharArray(); 

tf.setEnabled(false);
}
public void actionPerformed(ActionEvent e1) 
{
Button b=(Button)e1.getSource();
if(b==b1)
{
f.setVisible(false);
f.dispose();
System.exit(0);
}
if(b==b2)
ta.append("\n您的正确个数:"+tr+"\n您的错误个数:"+fa);
}
}运行程序,方法public void keyTyped(KeyEvent e)中语句并未被执行?成员变量tr,fa,w,无自加效果,在按按钮b2后获得tr,fa无论如何都是0

解决方案 »

  1.   

    感觉楼主的设计上就不是很恰当。
    可以试着把监听器和主类分开。
    if(b==b1)这儿建议使用equals()。
      

  2.   

    import java.awt.*;
    import java.awt.event.*;public class Mark_524 implements ActionListener, KeyListener {
    static Frame f;
    static TextField tf = new TextField("I Love you,How are you?", 20);
    static TextArea ta = new TextArea(5, 23);
    static Button b1 = new Button("Exit");
    static Button b2 = new Button("End");
    int tr = 0, fa = 0, w = 0;
    char[] ch = new char[23]; public static void main(String args[]) {
    new Mark__524().init();
    } public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub } public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub } public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub
    if (e.getKeyChar() == ch[w] && w != tf.getText().length())
    tr++;
    fa++;
    w++;
    } public void init() {
    f = new Frame("打字练习");
    f.setLayout(new FlowLayout());
    f.setSize(200, 200);
    f.add(tf);
    f.add(ta);
    f.add(b1);
    f.add(b2);
    f.setVisible(true); ta.addKeyListener(this);
    b1.addActionListener(new Mark__524());
    b2.addActionListener(new Mark__524()); String str = tf.getText();
    ch = str.toCharArray(); tf.setEnabled(false);
    } public void actionPerformed(ActionEvent e1) {
    Button b = (Button) e1.getSource();
    if (b == b1) {
    f.setVisible(false);
    f.dispose();
    System.exit(0);
    }
    if (b == b2)
    ta.append("\n您的正确个数:" + tr + "\n您的错误个数:" + fa);
    }
    }先看看怎么贴代码。
      

  3.   

    package Mark;import java.awt.*;
    import java.awt.event.*;public class Mark_525  implements ActionListener, KeyListener{
        
    private TextArea t1,t2;
    private Button   bt1;
    private Frame    f;
    int n;//用于统计用户敲击键盘的次数
    int index;//用于用户已输入字母的个数
    public void init()
    {
    t1=new TextArea("I Love you, How are you?",5,100);
    t2=new TextArea("",5,100);
    bt1=new Button("退出");
    f=new Frame("打字练习程序");
    bt1.addActionListener(new Mark_525());
    t2.addKeyListener(this);
    f.setLayout(new FlowLayout());
    f.add(t1);
    f.add(t2);
    f.add(bt1);

    f.setSize(200,300);
    f.setVisible(true);


    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
            new Mark_525().init();
    }
    public void actionPerformed(ActionEvent arg0)
    {
    Button b=(Button)arg0.getSource();
    System.exit(0);
    }
    public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub

    }
    public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

    }
    public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub
    n++;
    if(index!=t1.getText().length())
    {
        if(String.valueOf(e.getKeyChar()).equals(t1.getText().substring(index,index+1)))
        {
         String s=t1.getText().substring(0,index);
         t2.setText(s);
         index++;
        }
    }
    else if(index==t1.getText().length())
    {

    }
    }}