So I'm trying to have some form of input in my game and I understand all the keyPresses and whatnot but the only issue is I have no idea of adding a textfield to the game. JTextfield doesnt seem to appear so I started taking keypresses and adding them to a string but this proves to ether be too slow or too fast. Is there any way of adding a textfield to the game?
I'm using just plain java and swing.
Here is a code snippet:
JFrame frame = new JFrame("Game");
//Sorts out JFrame variables and settings.
frame.setLayout(null);
setBounds(0,0,800,600);
frame.add(this);
frame.setSize(800,600);
frame.setResizable(false);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
gameRunning = false;
System.exit(0);
}
});
frame.addKeyListener(this);
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
int columns = 25;
TextField localTextField = new TextField("Text", columns);
Insets insets = frame.getInsets();
Dimension size = localTextField.getPreferredSize();
localTextField.setBounds(25 + insets.left, 5 + insets.top,
size.width, size.height);
frame.add(localTextField);
frame.setVisible(true);
//Double Buffer set up.
createBufferStrategy(2);
strategy = getBufferStrategy();
Ok, so I pressume the only way of doing this is the way in which I'm currently doing it.While the text box is open it adds all keypesses together with a slight delay between each key press. I just thought that there might have been an easy method of doing this.