0

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

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);
}
}

输入.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();
}
}
}
4

2 回答 2

0

You get this exception when you modify an ArrayList while it is being iterated, and you are updating the list from one thread and iterating it from another. A quick fix is making all methods that touch the list synchronized so that no two threads can enter them at the same time; that is:

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


public synchronized void addCharToArray(Character c){
    ....
}
于 2013-09-26T23:46:59.633 回答
0

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

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

“事件调度线程”的概念在此处进行了更详细的解释:http: //docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

然而,影响在于构建和展示一个 Swing 应用程序。对应用程序的 main 方法或 Applet 中的方法的调用不会在事件分派线程上调用。因此,在构建和显示应用程序或小程序时,必须注意将控制权转移到事件调度线程。

有关该主题的更多信息 - 这是一个很好的相关答案: 在事件调度线程上构建 Swing/AWT 小部件是否安全?

于 2013-09-27T01:09:52.173 回答