我正在创建一个个人Java终端,我需要它做一些我现在还不太了解的事情。我需要程序来监听输入到JTextArea的内容,同时也知道程序将始终显示
[USERNAME]@[OPERATING SYSTEM]:~$
当“Enter”被击中时。而且我还需要程序来设置如上所述的部分,是不可编辑的,并且在永久声明之后设置允许字符输入。 如果有人能够帮助我,我的程序在下面,那么就有了监听器的代码,很可能需要进行大量的编辑。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class testGUI extends JFrame {
boolean _active = true;
String _username = System.getProperty("user.name").toLowerCase();
String _os = System.getProperty("os.name").trim().toLowerCase();
JTextArea _commandLine = new JTextArea(_username + "@" + _os + ":~$ ");
public testGUI() {
super("Java Terminal");
setSize(800,600);
setLocation(100,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
GUISetup();
setVisible(true);
}
public void GUISetup() {
add(_commandLine);
_commandLine.addActionListener(new CommandLineListener());
}
public static void main(String[] args) {
new testGUI();
}
}
听众代码如下。
try {
while(_active) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(_username + "@" + _os + ":~$ ");
String command = br.readLine();
if(command.equals("cf")) {
new commandCreateFile();
} else if(command.equals("cof")) {
new commandCompile();
} else if(command.equals("help")) {
System.out.println("Commands");
System.out.println(" cf - Creates .java files, does not compile.");
System.out.println(" ccf - Creates .java files, compiles on creation.");
System.out.println(" help - Shows help documentation.");
} else if(command.equals("exit")) {
System.out.print("Are you sure you want to exit? (Y/N) ");
String exit = br.readLine();
if(exit.equalsIgnoreCase("y")) {
System.exit(0);
}
} else if(command.isEmpty()) {
// do nothing.
} else {
System.out.println("\"" + command + "\" does not exist. Please review the \"help\" menu");
}
}
} catch(Exception ex) {
System.out.println("There was a problem: " + ex);
}
答案 0 :(得分:3)
使用DocumentListener
JTextArea's
附加的Document
和DocumentFilter
附带的Document
来检查允许哪些修改。