Simple User-Input AKA a DOS application.
I've been doing some programming using the simple DOS commands, and reading keyboard input to read strings into variables.
I've got an Applet at the moment with a text area (several rows high), and a text field (single line), and I've got the text area reading a string variable into it, and the text field appending it's value to the variable on the event that you hit enter..
But the issue I'm having is that, unlike the DOS commands, I can't get the processing to stop and wait for an input, then continue...
If anyone could help out, it would be great, Thanks!
Here's my code at the moment... the commented out lines in the input class are wherre im playing around at the moment...
Any help or references appreciated.
Thanks :)
// File: Console.java
package MyConsoleIO;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Console extends JApplet
implements ActionListener{
private JTextField inputBox;
private JTextArea textBox;
private String gotString = "";
private Game game;
public void trace(String msg){System.out.println(msg);}
public void println(String msg){
textBox.append("\n"+msg);
trace("Tried to print: "+msg);
}
public void init() {
game = new Game();
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
private void createGUI() {
JPanel contentPane = new JPanel(new GridBagLayout());
contentPane.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.BLACK),
BorderFactory.createEmptyBorder(10,10,5,5)));
setContentPane(contentPane);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(0,0,5,5);
//// Label
//JLabel receiverLabel = new JLabel(">",
//JLabel.TRAILING);
//add(receiverLabel, c);
// Text Field
inputBox = new JTextField("Type Something", 10);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
add(inputBox, c);
inputBox.addActionListener(this);
// Button
JButton button = new JButton(">");
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.anchor = GridBagConstraints.LINE_START; //stick to the
//text field
c.fill = GridBagConstraints.NONE; //keep the button
//small
c.weightx = 0.0;
add(button, c);
button.addActionListener(this);
// Text Box
textBox = new JTextArea(30, 80);
textBox.setEditable(false);
c.anchor = GridBagConstraints.CENTER; //reset to the default
c.fill = GridBagConstraints.BOTH; //make this big
c.weighty = 1.0;
add(new JScrollPane(textBox), c);
}
public void actionPerformed(ActionEvent event) {
textBox.append("\n"+inputBox.getText());
gotString = inputBox.getText();
inputBox.setText("");
Input.inString = gotString;
}
public void start(){
textBox.append(Output.messagelist);
}
}
// File: Game.java
package MyConsoleIO;
public class Game{
public Game(){
Output.println("Game Started!");
String name = Input.getString("Please enter your name: ");
Output.println(name + ", ay... how odd.");
Output.println("GAME OVER");
}
}
// File: Input.java
package MyConsoleIO;
public class Input{
static public String inString = "";
public static String getString(String message){
Output.println(message);
//while(inString.length() == 0){
//// wait for some input...
//}
return inString;
}
}
// File: Output.java
package MyConsoleIO;
public class Output {
public static String messagelist = "";
public static void println(String line){
messagelist += "\n" + line;
}
}