import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.lang.*;
public class Dazi extends KeyAdapter
{
static int j=(int)(Math.random()*1000)%26;;
static JFrame f=new JFrame("Heart欢迎您进入打字练习板块:");
static String str[]={"A","B","C","D","E","F","G","H","I","G","K","L","M",
              "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
};
static JButton b[]=new JButton[str.length];
public Dazi()
{

f.setSize(600,600);
f.setLocation(50,50);
f.getContentPane().setLayout(new GridLayout(6,6));
loop();
}
public void loop()
{
for(int i=0;i<26;i++)
{
b[i]=new JButton(str[i]);
f.getContentPane().add(b[i]);
if(i!=j)
{
b[i].setVisible(false);
}
}
b[j].addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{

if(e.getKeyCode()==j+65)
{
b[j].setVisible(false);
System.out.println("Right!");
j=(int)(Math.random()*1000)%26;
loop();
}
else
System.out.println("Error!");
}
public static void main(String [] args)
{
Dazi d=new Dazi();

f.setVisible(true);
}
}
问题是:第一个字母显示时候按下对应键时候显示第二个字母,但是再按下对应键时候就不管事了 为什么?

解决方案 »

  1.   

    我是这样解决的,把你new的按钮添加在jpanel中.销毁的时候调用jpanel的disable方法就没问题了
      

  2.   

    在JFrame面板上监听 不要在JButton上监听
      

  3.   

    因为当前聚焦控件不在JButton 你点一下就能接受了
    但这样做没必要
      

  4.   

    public void loop()
    {
    for(int i=0;i <26;i++)
    {
    b[i]=new JButton(str[i]);
    f.getContentPane().add(b[i]);
    if(i!=j)
    {
    b[i].setVisible(false);
    }
    }
    b[j].addKeyListener(this);

    =====================================
    每一次都loop,每一次都在frame上加上若干button,额的神呀。
    聚焦也是有问题,顶6楼dracularking大大,学习
      

  5.   

    根据你的代码,简单凑出来一个,尽量不改变的原来的代码,呵呵
    主要是没一次击键后,要让jbutton获得焦点,requestFocus()这个是要点import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import java.lang.*;public class Dazi extends KeyAdapter {
    static int j = (int) (Math.random() * 1000) % 26;;
    static JFrame f = new JFrame("Heart欢迎您进入打字练习板块:");
    static String str[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "G",
    "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
    "X", "Y", "Z" };
    static JButton b[] = new JButton[str.length]; public Dazi() { f.setSize(600, 600);
    f.setLocation(50, 50);
    f.getContentPane().setLayout(new GridLayout(6, 6));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    initButtons();
    } /**
     * 这里只加载一次!!
     * */
    public void initButtons(){
    for (int i = 0; i < 26; i++) {
    b[i] = new JButton(str[i]);
    f.getContentPane().add(b[i]);
    // 暂时取消,全部显示看的更清楚
    // if (i != j) {
    // b[i].setVisible(false);
    // }

    }
    j = (int) (Math.random() * 1000) % 26;
    f.setVisible(true); // 移动到这里来
    b[j].setBackground(new Color(255,0,255));
    System.out.println(b[j].isFocusable());
    b[j].addKeyListener(this);
    b[j].requestFocus();
    } public void loop() {
    // for (int i = 0; i < 26; i++) {
    // b[i] = new JButton(str[i]);
    // f.getContentPane().add(b[i]);
    // if (i != j) {
    // b[i].setVisible(false);
    // }
    //
    // }
    for (int i = 0; i < 26; i++) {
    b[i].setVisible(true);
    }

    System.out.println(str[j]);
    b[j].setBackground(new Color(255,0,255)); // 增加一个颜色
    b[j].addKeyListener(this);
    b[j].requestFocus();
    } public void keyPressed(KeyEvent e) { if (e.getKeyCode() == j + 65) {
    b[j].setVisible(false);
    b[j].setBackground(null); // 恢复原始颜色
    System.out.println("Right!");
    j = (int) (Math.random() * 1000) % 26;
    loop();
    } else
    System.out.println("Error!");
    } public static void main(String[] args) {
    Dazi d = new Dazi();

    }
    }