Add JTextField to Game Menu (graphics2d)

房东的猫 提交于 2019-12-24 01:45:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!