问题
I'm developing a multiplayer game, and at the menu, I need to add a field, where the player can type the host-ip.
I'm making the menu with Graphics2D, and I'm updating it in a loop:
JFrame screenFrame = screen.getFullScreenWindow();
while (menuIsRunning) {
Graphics2D g = screen.getGraphics();
renderer.drawMenu(g, screen.getWidth(), screen.getHeight(), menuObjects);
screenFrame.getLayeredPane().paintComponents(g);
g.dispose();
screen.update();
}
And there I want to add a JTextField too, like this:
Container contentPane = screenFrame.getContentPane();
contentPane.setLayout(null);
JTextField text = new JTextField("Enter text here", 28);
contentPane.add(text);
text.setBounds(10,10,20,20);
screenFrame.validate();
And it appears on the screen, but when I'm typing in it, after a few sec, it just freezes or something (no error message). I can't type anymore, and all the other keyActions don't work either (like escape: exit the game)
I tried to see something with focusListener:
text.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
System.out.println("focus gained");
}
@Override
public void focusLost(FocusEvent e) {
System.out.println("focus lost");
}
});
It says when the focus gained, but when textfield freezes it doesn't say anything about focus lost. If I just click in the textfield, I can still use other functions, but when I begint to type it will freeze.
Anybody knows why?
来源:https://stackoverflow.com/questions/19652974/add-jtextfield-to-game-menu-graphics2d