2012-03-03 123 views
2

我正在创建一个个人Java终端,我需要它来做一些我现在不太了解的事情。我需要的程序来听什么被输入到JTextArea中,同时还知道,该方案将始终显示如何收听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); 
    } 

回答

3

使用DocumentListener连接到JTextArea'sDocument和连接到Document一个DocumentFilter检查哪个编辑是允许的。

+0

这我不明白,通过使用JTextArea的文件,我需要附加一个适当的文件吗? – 2012-03-04 05:22:17