0

MY CODE

package pack.TAgame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class AL_Test {

public static void main(String[] args){
    //JFrame
JFrame Frame = new JFrame("AL Test");
Frame.setSize(720,480);
Frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Frame.setVisible(true);
    Frame.setResizable(false);
    //Text Area
JTextArea textArea = new JTextArea();
textArea.setBackground(Color.BLACK);
textArea.setForeground(Color.GRAY);
JLabel jl = new JLabel();
Frame.add(textArea);
textArea.setEditable(false);
    //Text Field
JTextField jt = new JTextField(50);
JPanel jp = new JPanel();
jp.add(jt);
Frame.add(jt, BorderLayout.SOUTH);

class UserInput extends JFrame implements ActionListener{
    public UserInput() {
    }
    public void actionPerformed(ActionEvent e) {
               JTextField jt = new JTextField();
               JTextArea textArea = new JTextArea();
               jt.addActionListener(this);
               String text = jt.getText();
               textArea.append("\n>What does the scouter say about his power level?");
           }
        }
    }
}

This maybe a little like my other post...(In my code for the actionlistener part I gave up and I might have some useless code in it) so basically I want a JTextField that when I type a certain command to print something into my JTextArea. (Without the use of a button (i.e. Press enter))

Garry
  • 4,493
  • 3
  • 28
  • 48
eels11
  • 13
  • 5
  • you can use other listeners for mouse and keyboard – Garry Aug 28 '15 at 09:24
  • Add a document change listener on jtextField and fill the jtextArea depending on your checks,,See similar question http://stackoverflow.com/questions/3953208/value-change-listener-to-jtextfield – Anudeep Gade Aug 28 '15 at 09:28

1 Answers1

1

Start with the TextDemo.java code from the Swing tutorial on How to Use Text Fields. This example does exactly what you want:

  1. it adds an ActionListener to the text field. The ActionListener is invoked when Enter is pressed and focus is on the text field.

  2. The ActionListener will take the text from the text field and append it to a text area.

As a bonus you will also learn how to better structure your code by adding the components to a panel and then add the panel to the frame. The variables will be defined in the class as well.

camickr
  • 321,443
  • 19
  • 166
  • 288