2012-03-03 2 views
2

나는 개인용 자바 터미널을 만들고 있는데, 지금 당장은 이해할 수없는 몇 가지 일을해야한다. 또한 알고 동안 프로그램이 항상 JTextArea를 듣는 법

[USERNAME]@[OPERATING SYSTEM]:~$ 

때 타격을 받았다 "ENTER"가 표시됩니다의 JTextArea에에 입력되는 내용을 듣고 프로그램이 필요합니다. 또한 위의 내용을 편집 할 수 없도록 설정하고 영구 입력 후에 문자 입력 허용을 설정하는 프로그램이 필요합니다. 누군가 나를 도와 줄 수 있다면, 내 프로그램은 아래에 있으며, 청취자를위한 코드가있을 것입니다. 청취자를위한 코드는 대부분 편집이 많이 필요할 것입니다.

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

사용 DocumentListenerJTextArea'sDocument에 부착하고 DocumentFilter이 허용되는 편집 확인하기 위해 Document에 연결합니다.

+0

JTextArea의 문서를 사용하여이 문서에 적절한 문서를 첨부해야합니까? –

관련 문제