this.addKeyListener(输入);不起作用,但是frame.addKeyListener确实有效(仅限一段时间)

时间:2013-09-26 23:35:04

标签: java eclipse

我希望能够输入我拥有的显示窗口,但每当我添加

this.addKeyListener(input);

绝对没有,但是

frame.addKeyListener(input);

有效,但只是暂时的。经过一些输入后,它会抛出错误:

Exception in thread "Thread-1" java.util.ConcurrentModificationException<br>
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)<br>
at java.util.AbstractList$Itr.next(Unknown Source)<br>
at net.textBasedGame.Display.getInputAsString(Display.Java:52)<br>
at net.textBasedGame.Display.render(Display.java:81)
at net.textBasedGame.Display.run(Display.java:37)
at java.lang.Thread.run(Unknown Source)

我的Display.java代码在这里,下面是Input.java,我只是不确定究竟是什么导致

this.addKeyListener(input); 

不能工作。

package net.textBasedGame;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import.javax.swing.JFrame;

public class Display extends Canvas implements Runnable {

public static final int GAMEWIDTH = 600;
public static final int GAMEHEIGHT = 600;
private boolean waitingForInput;
public boolean running;
private Input input;
private ArrayList<Character> userInput = new ArrayList<Character>();

public void run() {
init();
running = true;
waitingForInput = true;
while(running){
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(2);
continue;
}
Graphics g = bs.getDrawGraphics();
render(g);
bs.show();
}
}

public String getInputAsString(){
String result = "";
for(Character c: userInput){
result += c;
}
return result;

}

public void addCharToArray(Character c){
userInput.add(c);
}

public void setWaitingforInputfalse(){
waitingForInput = false;
}

public boolean isWaitingForInput(){
return waitingForInput
}

public void render(Graphics g){
Graphics 2D g1 = (Graphics2D) g;
g1.setColor(Color.BLACK);
g1.fillRect(0, 0, GAMEWIDTH, GAMEHEIGHT);
g1.setColor(Color.BLUE);
g1.fillRect(100, 100, 50, 50);
g1.setColor(Color.Blue);
g1.fillRect(450, 100, 50, 50);
g1.setFont(new Font("Arial", Font.PLAIN, 30));
g1.setColor(Color.RED);
g1.drawString(getInputAsString(), 10, 300);
}

static public void main(String [] argv){
new Display().start();
}

public void start(){
Thread t = new Thread(this);
t.setPriority(Thread.MAX_PRIORITY);
t.start();
}
public void init(){
input = new Input(this);
JFrame frame = new JFrame("Text Based Game");
frame.setSize(GAMEWIDTH, GAMEHEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.IgnoreRepaint(true);
frame.setLocationRelativeTo(null);
frame.addKeyListener(input);
frame.add(this);
}
}

Input.java

package net.textBasedGame;

import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class Input implements KeyListener {

private Display dis;
private int keyCode;

public Input(Display display){
dis = display;
}

public void keyPressed(KeyEvent e) {

}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(Key event e){
int keyCode = e.getKeyCode();
Character keyLetter = e.getKeyChar();
if(Character.*isLetterOrDigit*(keyLetter) || keyLetter.equals('?') || Character.isSpaceChar(keyLetter)){

}
if(dis.isWaitingForInput()){
dis.addCharToArray(keyLetter);
}

else if(keyCode == KeyEvent.VK_ENTER){
dis.setWaitingforInputfalse();
}
}
}

2 个答案:

答案 0 :(得分:0)

在迭代时修改ArrayList时会出现此异常,并且您正在从一个线程更新列表并从另一个线程迭代它。快速解决方法是使所有触及列表的方法同步,以便没有两个线程可以同时输入它们;那就是:

public synchronized String getInputAsString(){
    ....
}


public synchronized void addCharToArray(Character c){
    ....
}

答案 1 :(得分:0)

你的问题是,一般来说,Swing(JFrame)不是线程安全的。除非另有说明,否则必须在事件派发线程上访问所有Swing组件和相关类。

  

Swing事件处理代码在称为事件的特殊线程上运行   派遣线程。大多数调用Swing方法的代码也会运行   这个帖子。这是必要的,因为大多数Swing对象方法都是   不是“线程安全”:从多个线程调用它们冒险线程   干扰或内存一致性错误。

“Event Dispatch Thread”的概念在此解释得更详细: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

  

然而,影响在于构建和展示Swing   应用。调用应用程序的主要方法或方法   在事件派发线程上不调用Applet。因此,关心   必须采取将控制转移到事件调度线程时   构建和显示应用程序或小程序。

有关该主题的更多信息 - 这是一个很好的相关答案: Is it safe to construct Swing/AWT widgets NOT on the Event Dispatch Thread?