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");
}
}
}
}
2 Replies - 509 Views - Last Post: 29 November 2016 - 08:52 AM
#1
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!
Replies To: Coding commands for JFrame using JTextArea and JTextField
#2
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
#3
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();
Page 1 of 1

New Topic/Question
Reply


MultiQuote




|