我正在写一个游戏,当我与计算机发生碰撞并按E时,它应该打开一个带有问题的小JFrame
。但有时框架会打开但右键单击它时无法看到或关闭。或者它只显示一条小线,而不是更像这样:http://postimg.org/image/7upz61cjl。
有源代码:
public class Computer() extends AbstractActor {
public Computer() throws IOException {
super("computer", 0, 0, 80, 48, 100, "resources/sprites/computer.png");
question = new JLabel();
input = new JTextField();
confirm = new Button("Confirm");
getQuestion(new InputStreamReader(new DataInputStream(new FileInputStream("resources/questions.txt"))));
confirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (input.getText().equals(password) && net != null) {
getWorld().removeActor(net);
}
window.dispose();
window = null;
}
});
}
public void use(Ripley ripley) {
if (window == null && !(input.getText().equals(password))) {
window = new JFrame("Answer this to shut lasers.");
window.setLayout(new BoxLayout(window.getContentPane(), BoxLayout.Y_AXIS));
window.add(question);
window.add(input);
window.add(confirm);
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
window.setVisible(true);
window.setResizable(false);
window.setAlwaysOnTop(true);
window.pack();
window.setLocationRelativeTo(null);
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
window = null;
}
});
}
}
public void act() {
if (net == null) {
net = (LaserNet)getActorByName("laser net");
}
}
public void getQuestion(InputStreamReader file) throws IOException {
Random rand = new Random();
int num = -1;
while (num % 2 != 0)
num = rand.nextInt(10);
try (BufferedReader br = new BufferedReader(file)) {
String line;
int i = 0;
while ((line = br.readLine()) != null) {
if (i == num) {
question.setText(line);
line = br.readLine();
password = line;
}
i++;
}
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}