Code
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.util.HashMap;
@SuppressWarnings({ "serial" })
public class Slenderman extends JPanel implements ActionListener {
/** Text fields
* JTextField is for input
* JTextArea is for output
*/
protected JTextField textField;
public JTextArea textArea;
/** Newline
* This is for newline
*/
private final static String newline = "\n";
/** Command Stuff
* For command storage etc.
*/
HashMap commandMap = new HashMap();
public String text;
public TextGame() {
super(new GridBagLayout());
textField = new JTextField(20);
textField.addActionListener(this);
textArea = new JTextArea(5, 20);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
/** ActionPerformed
*/
public void actionPerformed(ActionEvent evt) {
text = textField.getText();
commandList();
if (commandMap.get(text) == null) {
errorCode();
} else {
textArea.append(commandMap.get(text) + newline);
}
textField.selectAll();
textArea.setCaretPosition(textArea.getDocument().getLength());
}
/** CommandList/adding
* Syntax: commandMap.put(The question/command you want, the answer it gives);
*/
public void commandList(){
}
public void errorCode(){
textArea.append("Cant do that. Try something else." + newline);
}
/** Shows Gui
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
JFrame frame = new JFrame("Text Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TextGame());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}