0

我正在尝试在 java 中进行简单的反应测试。当屏幕变绿时,我按空格女巫应该更改布尔值“单击为假并停止测量时间的循环。实际上,键监听器什么都不做。我是否将键监听器添加到正确的组件(jpanel 面板)?还有其他问题吗?

    import java.awt.Color;
    import java.awt.RenderingHints.Key;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;        
    import javax.swing.*;

    public class mainCheck {
    // //////////////////////////////////////
    public static void timeKeeper() {
         boolean clicked=false;
        long time = 10000;
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        panel.setBackground(Color.GREEN);
        while (time > 0 && !clicked) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            time--;
        }
        panel.setBackground(Color.gray);
        long time2= 10000-time;
        JLabel x = new JLabel("" +time2+"");
        panel.add(x);    
    }

    // //////////////////////////////////////
    static boolean clicked;
    JFrame frame;
    static JPanel panel;

    public mainCheck() {
        frame = new JFrame();
        panel = new JPanel();
        clicked = false;
        Handler handler = new Handler();
        frame.addKeyListener(handler);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);
        frame.setVisible(true);
    }
    // //////////////////////////////////////

    public static void main(String[] args) {
        mainCheck f = new mainCheck();
        panel.getActionMap();           
        f.timeKeeper();
    }

    // //////////////////////////////////////
    public class Handler implements KeyListener {

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

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

        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub
            if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                clicked = false;
                System.out.println("space pressed");
            }    
        }    
    }
}
4

3 回答 3

3
  • 不要使用Thread.sleep(5000);阻塞事件调度线程,并且在睡眠期间,您可能会将所有事件丢失到已经可见的 Swing GUI

  • 改用摇摆计时器

  • Thread.sleep(1); 可能是空间环境的适当延迟,non_human,Native OS 中非常短的攻击延迟(8-14 毫秒,取决于 Native OS)

  • JLabel x = new JLabel("" +time2+"");并且panel.add(x);在 AWT/Swing 中没有任何通知器,其中一些,任何,部分被删除或添加,必须通过使用方法和JComponents通知使用LayoutManager(在 API 中JPanel具有FlowLayout),例如revalidaterepaint

.

JLabel x = new JLabel("" +time2+"");
panel.add(x);
panel.revalidate();
panel.repaint();
于 2013-08-26T11:21:30.167 回答
0
  • 问题是您的“ e.getKeyCode() ”始终为“ 0 ”,因此请更改为“ e.getKeyChar() ”和“ (char)32
  • 另一个问题是你把clicked = false并且必须是“
  • 你遇到的最后一个问题是在“ timeKeeper() ”中你必须删除“ boolean ”,因为它已经被声明了

    Bad
    public static void timeKeeper() {
    boolean clicked=false
    ....}
    
    Good
    public static void timeKeeper() {
    clicked=false
    ....}
    

这是正确的代码:

import java.awt.Color;
import java.awt.event.*;        
import javax.swing.*;

public class mainCheck {
// //////////////////////////////////////
public static void timeKeeper() {
    clicked=false;
    long time = 10000;
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    panel.setBackground(Color.GREEN);
    while (time > 0 && !clicked) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        time--;
    }
    panel.setBackground(Color.gray);
    long time2= 10000-time;
    JLabel x = new JLabel("" +time2+"");
    panel.add(x);    
}

// //////////////////////////////////////
static boolean clicked;
JFrame frame;
static JPanel panel;
Handler handler = new Handler();

public mainCheck() {
    frame = new JFrame();
    panel = new JPanel();
    clicked = false;

    frame.addKeyListener(handler);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.setVisible(true);
}
// //////////////////////////////////////

public static void main(String[] args) {
    mainCheck f = new mainCheck();
    panel.getActionMap();           
    f.timeKeeper();
}

// //////////////////////////////////////
public class Handler implements KeyListener {

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

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

    @Override
    public void keyTyped(KeyEvent e) {
        if (e.getKeyChar() == (char)32) {
            clicked = true;
            System.out.println("space pressed");
        }    
    }  
}
}
于 2013-08-26T19:02:31.207 回答
-1

使用 Thread.sleep() 来减少时间值是一个非常糟糕的主意。改用一个Timer对象:

public void myTimer(){
    Timer myTimer = new Timer(delay, new ActionListener(){
        public void actionPerformed(ActionEvent e){
            //Stuff to do
        }
    });
}

delay您延迟计时器的时间量在哪里。您使用 myTimer.start(); 启动计时器;

于 2013-08-26T12:12:32.367 回答