2012-04-20 2 views
2

Java에서이 오픈 소스 메시지 콘솔에 대한 코드가 있지만 간단히 컴파일되지 않습니다. IDE를 사용하여 작동하는지 누구에게 알 수 있습니까? 컴파일시메시지 콘솔 Java

import java.io.*; 
    import java.awt.*; 
    import javax.swing.*; 
    import javax.swing.event.*; 
    import javax.swing.text.*; 

    /* 
    * Create a simple console to display text messages. 
    * 
    * Messages can be directed here from different sources. Each source can 
    * have its messages displayed in a different color. 
    * 
    * Messages can either be appended to the console or inserted as the first 
    * line of the console 
    * 
    * You can limit the number of lines to hold in the Document. 
    */ 
    public class MessageConsole 
    { 
    private JTextComponent textComponent; 
    private Document document; 
    private boolean isAppend; 
    private DocumentListener limitLinesListener; 

    public MessageConsole(JTextComponent textComponent) 
    { 
    this(textComponent, true); 
    } 

    /* 
    * Use the text component specified as a simply console to display 
      * text messages. 
    * 
    * The messages can either be appended to the end of the console or 
    * inserted as the first line of the console. 
    */ 
    public MessageConsole(JTextComponent textComponent, boolean isAppend) 
    { 
    this.textComponent = textComponent; 
    this.document = textComponent.getDocument(); 
    this.isAppend = isAppend; 
    textComponent.setEditable(false); 
    } 

    /* 
    * Redirect the output from the standard output to the console 
    * using the default text color and null PrintStream 
      */ 
    public void redirectOut() 
    { 
    redirectOut(null, null); 
    } 

    /* 
    * Redirect the output from the standard output to the console 
    * using the specified color and PrintStream. When a PrintStream 
    * is specified the message will be added to the Document before 
    * it is also written to the PrintStream. 
      */ 
    public void redirectOut(Color textColor, PrintStream printStream) 
    { 
    ConsoleOutputStream cos = new ConsoleOutputStream(textColor, printStream); 
    System.setOut(new PrintStream(cos, true)); 
    } 

    /* 
    * Redirect the output from the standard error to the console 
    * using the default text color and null PrintStream 
    */ 
    public void redirectErr() 
    { 
    redirectErr(null, null); 
    } 

    /* 
    * Redirect the output from the standard error to the console 
     * using the specified color and PrintStream. When a PrintStream 
    * is specified the message will be added to the Document before 
    * it is also written to the PrintStream. 
    */ 
    public void redirectErr(Color textColor, PrintStream printStream) 
    { 
      ConsoleOutputStream cos = new ConsoleOutputStream(textColor, printStream); 
    System.setErr(new PrintStream(cos, true)); 
    } 

    /* 
    * To prevent memory from being used up you can control the number of 
    * lines to display in the console 
      * 
    * This number can be dynamically changed, but the console will only 
    * be updated the next time the Document is updated. 
    */ 
    public void setMessageLines(int lines) 
    { 
    if (limitLinesListener != null) 
    document.removeDocumentListener(limitLinesListener); 

    limitLinesListener = new LimitLinesDocumentListener(lines, isAppend); 
    document.addDocumentListener(limitLinesListener); 
    } 

    /* 
    * Class to intercept output from a PrintStream and add it to a Document. 
    * The output can optionally be redirected to a different PrintStream. 
    * The text displayed in the Document can be color coded to indicate 
    * the output source. 
    */ 
    class ConsoleOutputStream extends ByteArrayOutputStream 
    { 
    private SimpleAttributeSet attributes; 
    private PrintStream printStream; 
    private StringBuffer buffer = new StringBuffer(80); 
    private boolean isFirstLine; 

    /* 
    * Specify the option text color and PrintStream 
    */ 
    public ConsoleOutputStream(Color textColor, PrintStream printStream) 
    { 
    if (textColor != null) 
    { 
     attributes = new SimpleAttributeSet(); 
     StyleConstants.setForeground(attributes, textColor); 
    } 

    this.printStream = printStream; 

    if (isAppend) 
     isFirstLine = true; 
    } 

    /* 
    * Override this method to intercept the output text. Each line of text 
    * output will actually involve invoking this method twice: 
    * 
    * a) for the actual text message 
    * b) for the newLine string 
    * 
    * The message will be treated differently depending on whether the line 
    * will be appended or inserted into the Document 
    */ 
    public void flush() 
    { 
    String message = toString(); 

    if (message.length() == 0) return; 

    if (isAppend) 
     handleAppend(message); 
    else 
     handleInsert(message); 

    reset(); 
    } 

    /* 
    * We don't want to have blank lines in the Document. The first line 
      * added will simply be the message. For additional lines it will be: 
    * 
    * newLine + message 
    */ 
    private void handleAppend(String message) 
    { 
    if (message.endsWith("\r") 
    || message.endsWith("\n")) 
    { 
     buffer.append(message); 
      } 
     else 
     { 
      buffer.append(message); 
     clearBuffer(); 
     } 
      } 
     /* 
     * We don't want to merge the new message with the existing message 
     * so the line will be inserted as: 
    * 
    * message + newLine 
    */ 
private void handleInsert(String message) 
{ 
    buffer.append(message); 

    if (message.endsWith("\r") 
    || message.endsWith("\n")) 
    { 
    clearBuffer(); 
    } 
    } 

/* 
* The message and the newLine have been added to the buffer in the 
    * appropriate order so we can now update the Document and send the 
    * text to the optional PrintStream. 
    */ 
    private void clearBuffer() 
{ 
    // In case both the standard out and standard err are being redirected 
    // we need to insert a newline character for the first line only 

    if (isFirstLine && document.getLength() != 0) 
    { 
     buffer.insert(0, "\n"); 
    } 

    isFirstLine = false; 
    String line = buffer.toString(); 

    try 
    { 
    if (isAppend) 
    { 
    int offset = document.getLength(); 
    document.insertString(offset, line, attributes); 
    textComponent.setCaretPosition(document.getLength()); 
    } 
    else 
    { 
    document.insertString(0, line, attributes); 
    textComponent.setCaretPosition(0); 
    } 
    } 
catch (BadLocationException ble) {} 

if (printStream != null) 
    { 
printStream.print(line); 
} 

buffer.setLength(0); 
} 
} 
} 

, 나는이 오류를받은 :

오류 :

Finally, you need to decide if you need to limit the number of lines contained in the console. The MessageConsole will use my LimitLinesDocumentListener described in an earlier blog entry.

: limitLinesDocumentListener는 IMO를 설명

+0

ide의 빌드 경로에 LimitLinesDocument Listener 클래스가 없습니다. – r0ast3d

+0

해당 코드에는 'LimitLinesDocumentListener' 클래스에 대한 정의가 없으며 Google 검색을 수행 할 때 아무 것도 나오지 않아 다른 사람을 위해 해당 코드가 컴파일되는 이유를 알 수 없습니다. –

+0

Jon Skeet이 google에서 pposted했다고 생각합니다 .-) – r0ast3d

답변

2

가져 오기 문제. 클래스 또는 인터페이스에 액세스 할 수 있어야합니다 (할당에 간단한 생성자가 있기 때문에 클래스라고 가정합니다 : limitLinesListener = new LimitLinesDocumentListener(lines, isAppend);) LimitLinesDocumentListener. 간단히 그 클래스는 프로그램에서 찾을 수 없습니다. 이 파일의 동일한 패키지에서 해당 클래스가 필요하거나 해당 클래스를 가져와야합니다.

7

blog post이 꽤 분명히 유형에 해결 될 수없는 LimitLinesDocumentListenerfrom the earlier blog post을 얻을 수 있습니다.

두 파일을 모두 가져 오면 문제없이 함께 컴파일됩니다.