gpt4 book ai didi

java - 禁用按键事件

转载 作者:行者123 更新时间:2023-12-01 12:57:32 25 4
gpt4 key购买 nike

我正在制作一个测验应用程序,当一个团队按下他们的按钮(键盘键)时,我希望我的 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);
}
}

最佳答案

为此使用全局变量。

静态 boolean isPressed = false

按键事件

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

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

关于java - 禁用按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23759524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com