Feliz
Feliz

Reputation: 1

Using KeyEvents with Java.awt

I am trying to create a text-game and have decided the best way to do this is to use a UI instead of the cin.nextLine() method and the compiler output. What I've been trying to do is to add a KeyEvent to my TextField for user input where when the user presses the enter key their text goes into the TextArea I've set above just under any other text and the input goes away. I've also been trying to add the String that they typed into the input just before pressing the enter key into a String variable that will be processed later in if/else statements.

I've tried adding the text main.input.setText(""); but that did not work either. As well, I've attempted to add the word public before the deceleration and initialisation of the TextField but that came up with an error of its own.I also don't exactly know how I should properly access the text inside of the TextField to add it to the TextArea above and to process it as a String variable.

import java.awt.*;
import java.awt.event.*;

public class UI extends Frame implements KeyListener
{
    public void test()
    {
        Frame main=new Frame();
        main.setSize(1366,768);
        main.setLayout(null);
        main.setVisible(true);
        TextArea mainText=new TextArea("Come on and do something!");
        mainText.setBounds(10,30,1366,728);
        main.setBackground(Color.white);
        mainText.setEditable(false);
        TextField input=new TextField("");
        input.setBounds(10,738,1366,20);
        main.add(input);
        main.add(mainText);
        input.addKeyListener(this);
    }
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_ENTER) {
        Toolkit.getDefaultToolkit().beep();
        input.setText("");
    }
}

    public static void main(String[] args)
    {
        UI set = new UI();
        set.test();
    }
}

I expected this code to set the TextField to blank and I'd hoped to go further to find out how to add the code to a variable and to the Text Area. Instead, I get this error message:

Error: cannot find symbol
  symbol:   variable input
  location: class UI

EDIT: Alright so I didn't know awt was out of date, so I did change it to a Swing version. I did also remove the extending as well as changing the KeyListener to an ActionListener. However, what occurs now is that the ActionListener is not being properly added to the JTextField. Here's my updated code below:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UI
{
    private JFrame main;
    private JTextArea mainText;
    public UI()
    {
        main=new JFrame("Text Game");
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setSize(1366,768);
        mainText=new JTextArea("Come on and do something!");
        mainText.setBounds(10,100,1366,728);
        mainText.setEditable(false);
        JTextField input=new JTextField("");
        input.setBounds(10,700,1366,20);
        input.addActionListener(this);
        main.add(input);
        main.add(mainText);
        main.setVisible(true);
    }

        public static void main(String[] args)
        {
            new UI();
        }
}

Any ideas for what I've done wrong now?

Upvotes: 0

Views: 111

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

The basic problem is one of context. input is declared as a local variable within the context of the constructor, so it can not be accessed from any other context

 public void test()
    {
        //...
        TextField input=new TextField("");
        //...
    }

To be honest, this is pretty basic Java 101, you should start by having a look at Understanding Class Members. This is a concept you should already understand before embarking on GUI development (IMHO)

Observations

  • Avoid KeyListener (generally), especially with text components
  • Use an ActionListener instead
  • AWT is out-of-date, consider Swing or, better yet, JavaFX
  • Avoid null layouts, they will waste your time
  • As a general rule, you should not be extending directly from top level containers like Frame. They lock you into a single use case, reduce re-usability and you're not really adding any new functionality to the class anyway

Upvotes: 1

Related Questions