我希望能够在我拥有的显示窗口中输入,但是每当我添加时
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();
}
}
}