2 Replies - 509 Views - Last Post: 29 November 2016 - 08:52 AM Rate Topic: -----

#1 AlluraCCFC   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 24-November 16

Coding commands for JFrame using JTextArea and JTextField

Posted 29 November 2016 - 08:02 AM

Hello! I am trying to make it read the users input when they press the Send button under actionPerformed and compare the strings in a separate method, commandList. Most of it is working but when I type 'register' it tells me it's an unrecognized command. Thanks!

package javaapplication1;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.util.ArrayList;

/**
 *
 * @author Allura
 */

public class MyClass extends JFrame {

    private JPanel mainPanel;
    private JTextArea gameArea;
    private JTextField inputField;
    private JButton sendButton;
    private String inputText;
    private GameData gameData = new GameData();
    
    public static void main(String[] args) {
        
        MyClass myclass = new MyClass();
        myclass.start();
    }
    
    public void start() {
        
        setTitle("Generation X - Allura");
        mainPanel = new JPanel();
        inputField = new JTextField(32);
        gameArea = new JTextArea(20, 40);
        sendButton = new JButton("Send");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JScrollPane scroller = new JScrollPane(gameArea);
        gameArea.setLineWrap(true);
        gameArea.setWrapStyleWord(true);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        
        mainPanel.add(scroller);
        mainPanel.add(inputField);
        mainPanel.add(sendButton);
        sendButton.addActionListener(new SendButtonListener());
        
        gameArea.setEditable(false);
        inputField.requestFocus();
        
        JMenuBar menuBar = new JMenuBar();
        JMenu gameMenu = new JMenu("Game");
        JMenuItem saveItem = new JMenuItem("Save");
        gameMenu.add(saveItem);
        menuBar.add(gameMenu);
        setJMenuBar(menuBar);
        getContentPane().add(BorderLayout.CENTER, mainPanel);   
        setSize(500, 600);
        setVisible(true);
    }
    
    class SendButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            inputText = gameArea.getText();
            if (!inputText.equals(null)) {
                inputField.setText("");
                gameData.commandList();
            }
        }
    }
    class GameData {
        public void commandList() {
            if (inputText.equals("register")) {
                gameArea.append("Choose your desired player name:\n");
            }
            else {
                gameArea.append("You have entered an invalid command!\n");
            }
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Coding commands for JFrame using JTextArea and JTextField

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: Coding commands for JFrame using JTextArea and JTextField

Posted 29 November 2016 - 08:45 AM

Quote

it tells me it's an unrecognized command.

What is the value that the computer is comparing to determine if what was entered is a valid command?
Print it out so you can see it and/or include it in the error message.

Quote

it tells me it's an unrecognized command.

Where is the text: "unrecognized" shown in the program? I can not see it.

This post has been edited by NormR: 29 November 2016 - 09:28 AM

Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Coding commands for JFrame using JTextArea and JTextField

Posted 29 November 2016 - 08:52 AM

Quote

inputText = gameArea.getText();

should afterwards have this applied

inputText = inputText.trim().toLowerCase();

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1