Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 禁用关键事件_Java_Swing_Jframe_Key Events_Focusable - Fatal编程技术网

Java 禁用关键事件

Java 禁用关键事件,java,swing,jframe,key-events,focusable,Java,Swing,Jframe,Key Events,Focusable,我正在做一个测验应用程序,当一个团队按下他们的按钮(键盘键)时,我希望我的JFrame停止听其他按钮,直到下一个问题或者答案是错误的 如何停止收听关键事件?我尝试将JFrame设置为不可聚焦。我尝试在update()函数中执行此操作 以下是我的JFrame实现代码: public class QuizView extends JPanel implements View { private Observable $model; private Controller $contr

我正在做一个测验应用程序,当一个团队按下他们的按钮(键盘键)时,我希望我的JFrame停止听其他按钮,直到下一个问题或者答案是错误的

如何停止收听关键事件?我尝试将JFrame设置为不可聚焦。我尝试在update()函数中执行此操作

以下是我的JFrame实现代码:

public class QuizView extends JPanel implements View {

    private Observable $model;
    private Controller $controller;     
    private Question $question;


    public QuizView(Observable model, Controller controller){
        setFocusable(true);

        $model = model;

        if (controller == null)
            $controller = new QuizController(model);
        else
            $controller = controller;

        $question = null;   

    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        this.setBackground(Color.WHITE);
        int h, w; /* Width & heigth */
        ArrayList<String> answers = null;
        String answer;
        String progression = ""; /* The progression : e.g. 2/25 , question 2 of 25 */
        int current, total;

        Multipleanswer multipleanswerQuestion;
        Multiplechoice multiplechoiceQuestion;

        /* If the question isn't empty */
        if (!($question == null)) {

            h = getHeight();
            w = getWidth();

            /* Set the font for the question itself */
            g.setFont(new Font("Serif", Font.PLAIN, 65));

            current = ((QuizModel) getModel()).getI() ; /* What question we are at */
            total = ((QuizModel) getModel()).getQuestions().size(); /* If the size of the arraylist is N, there are N + 1 questions */

            progression += current;
            progression += "/";
            progression += total;

            /* Draw the question*/
            g.drawString($question.getQuestion(), 250, 100);

            /* Set a smaller font */
            g.setFont(new Font("Serif", Font.PLAIN, 30));

            /* Draw the progression */
            g.drawString(progression, w - 50, 25);


            /* Set a smaller font for the answers */
            g.setFont(new Font("Serif", Font.PLAIN, 40));

            /* If the type of the question is multipleanswer */
            if ($question.getType() == QuestionType.MULTIPLEANSWER) {

                /* Cast it to multipleanswer question */
                multipleanswerQuestion = (Multipleanswer) $question;

                /* Get the answers */
                answers = multipleanswerQuestion.getAnswers();
            } else if ($question.getType() == QuestionType.MULTIPLECHOICE) {

                /* Cast it to multiplechoice question */
                multiplechoiceQuestion = (Multiplechoice) $question;

                /* Get the answers */
                answers = multiplechoiceQuestion.getAnswers();
            }

            /* Speed questions don't show answers so we only display answers if it's not a speed question */
            if($question.getType() != QuestionType.SPEED){
                /* Draw each answer */
                for (int i = 0; i < answers.size(); i++) {
                    answer = (i + 1) + ") " + answers.get(i);
                    g.drawString(answer, 250, 260 + (100 * i)); /* 300 is spacing from question , 100*i is spacing between answers */
                }
            }

        }
    }

    public void update(Observable arg0, Object arg1) {
        $question = (Question) arg1;

        maximizeFrame(); /* Maximize the frame first */


        /* Add a keylistener for every team */
        addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                int teamSize; /* team size*/
                teamSize = ((QuizModel) getModel()).getTeams().size();
                if (Character.getNumericValue(e.getKeyChar()) <= teamSize) { /* If you pressed a number under the teamsize we listen to it */
                    minimizeFrame(); /* Minimize the frame */

                    /* If this question has a media path we need to pause the audio/video, we also check if the user has installed vlcplayer and selected the right path */
                    if($question.getMediaPath() != null && QuizSoftwareModel.$vlcPath != null)
                        ((QuizController)getController()).pause(); /* Pause the video */

                    /* Give a pop up message to the admin that a team has pushed their button */
                    ((QuizController)getController()).showScoreView(Character.getNumericValue(e.getKeyChar()));
                }

            }
        }); 


        repaint();
    }


    /**
     * Maximize the parent JFrame so the teams can see the question
     */
    protected void maximizeFrame() {
        JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
        topFrame.setState(JFrame.NORMAL);

    }

    /**
     * Minimize the parent JFrame so the teams can't see the question anymore 
     */
    protected void minimizeFrame() {
        JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
        topFrame.setState(JFrame.ICONIFIED);
        topFrame.setFocusable(false); /* Don't make it focusable anymore */

    }

    @Override
    public void setController(Controller controller) {
        $controller = controller;

    }

    @Override
    public Controller getController() {
        return $controller;
    }

    @Override
    public void setModel(Observable model) {
        $model = model;

    }

    @Override
    public Observable getModel() {
        return $model;
    }

    @Override
    public Controller defaultController(Observable model) {
        return new QuizController(model);
    }
}
公共类QuizView扩展了JPanel实现视图{
私有可观测$模型;
私人控制器$控制器;
私人问题$问题;
公共QuizView(可观察模型、控制器){
设置聚焦(真);
$model=model;
如果(控制器==null)
$controller=新的QuizController(型号);
其他的
$controller=控制器;
$QUISE=null;
}
公共组件(图形g){
超级组件(g);
这个.背景(颜色.白色);
内高,宽;/*宽度和高度*/
ArrayList answers=null;
字符串回答;
String progression=“”;/*级数:例如,2/25,问题2/25*/
总电流;
多重问题;
多重回声问题;
/*如果问题不是空的*/
如果(!($QUOTE==null)){
h=getHeight();
w=getWidth();
/*设置问题本身的字体*/
g、 setFont(新字体(“衬线”,Font.PLAIN,65));
current=((QuizModel)getModel()).getI();/*我们的问题是什么*/
总计=((QuizModel)getModel()).getQuestions().size();/*如果arraylist的大小为N,则有N+1个问题*/
级数+=电流;
级数+=“/”;
进度+=总进度;
/*提出问题*/
g、 抽绳($question.getQuestion(),250100);
/*设置较小的字体*/
g、 setFont(新字体(“衬线”,Font.PLAIN,30));
/*画进度*/
g、 抽绳(级数,w-50,25);
/*为答案设置较小的字体*/
g、 setFont(新字体(“衬线”,Font.PLAIN,40));
/*如果问题的类型是多重答案*/
if($question.getType()==QuestionType.MULTIPLEANSWER){
/*把它抛给多人回答问题*/
multipleanswerQuestion=(Multipleanswer)$question;
/*得到答案*/
answers=multipleanswerQuestion.getAnswers();
}else if($question.getType()==QuestionType.MULTIPLECHOICE){
/*把它抛到多重回声问题上*/
多回音问题=(多回音问题)$问题;
/*得到答案*/
answers=multipleechoicequestion.getAnswers();
}
/*速度问题不显示答案,所以我们只在不是速度问题时显示答案*/
if($question.getType()!=QuestionType.SPEED){
/*画出每个答案*/
for(int i=0;i如果(Character.getNumericValue(e.getKeyChar())对此使用全局变量

静态布尔值isPressed=false

按键事件

 if(!isPressed){
          isPressed = true;
          performYourOperation()
}    

private void performYourOperation(){
  //DO your stuff
  isPressed = false
}