0

我正在为java开发一个代码编辑器,它将用于并行计算和分布式计算。我正在寻找 Javafx 中 RSyntaxTextArea 的替代品,bcz 我试图在 Javafx 中实现它,但效果不佳,例如有时一半的文本区域不显示并且光标在文本区域中滞后。

Tab textTab = new Tab("Sample Tab");
RSyntaxTextArea ta= new RSyntaxTextArea();
SwingNode sn = new SwingNode();

String text="";
ta.setText(text);
ta.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
ta.setAntiAliasingEnabled(true);
ta.setCodeFoldingEnabled(true);

  RTextScrollPane sp = new RTextScrollPane(ta);
  sn.setContent(sp);
  textTab.setContent(sn);

我是 Javafx 的新手,所以我不太了解如何解决这些问题。它也不符合 Javafx 的美感。

4

2 回答 2

3

试试Tomas Mikula 的RichTextFX框架中的CodeArea(或更一般地说是)组件。StyleClassedTextArea

于 2014-06-09T14:32:02.263 回答
1

我已经从 RichTextFX 创建了我自己的类这里是代码

 import java.time.Duration;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import javafx.concurrent.Task;
 import org.fxmisc.richtext.CodeArea;
 import org.fxmisc.richtext.PlainTextChange;
 import org.fxmisc.richtext.StyleSpans;
 import org.fxmisc.richtext.StyleSpansBuilder;
 import org.reactfx.EventStream;

 /**
  *
  * @author Nika
  */
 public class SyntaxTextArea {

 private static final String[] KEYWORDS = new String[]{
    "abstract", "assert", "boolean", "break", "byte",
    "case", "catch", "char", "class", "const",
    "continue", "default", "do", "double", "else",
    "enum", "extends", "final", "finally", "float",
    "for", "goto", "if", "implements", "import",
    "instanceof", "int", "interface", "long", "native",
    "new", "package", "private", "protected", "public",
    "return", "short", "static", "strictfp", "super",
    "switch", "synchronized", "this", "throw", "throws",
    "transient", "try", "void", "volatile", "while"
 };

 private static final Pattern KEYWORD_PATTERN = Pattern.compile("\\b(" +    String.join("|", KEYWORDS) + ")\\b");

private CodeArea codeArea;
private ExecutorService executor;

public SyntaxTextArea() {

    executor = Executors.newSingleThreadExecutor();
    codeArea = new CodeArea();
    EventStream<PlainTextChange> textChanges = codeArea.plainTextChanges();
    textChanges
            .successionEnds(Duration.ofMillis(500))
            .supplyTask(this::computeHighlightingAsync)
            .awaitLatest(textChanges)
            .subscribe(this::applyHighlighting);

   codeArea.getStylesheets().add(org.fxmisc.richtext.demo.JavaKeywordsAsync.class.getResource("java-keywords.css").toExternalForm());
}

public void setText(String text) {
    codeArea.replaceText(0, 0, text);

}

public String getText() {
  return  codeArea.getText();

}
public void appendText(String text) {
    codeArea.appendText(text);

}

public  CodeArea getNode(){
return codeArea;
}

public void setStyling(){

}
private Task<StyleSpans<Collection<String>>> computeHighlightingAsync() {
    String text = codeArea.getText();
    Task<StyleSpans<Collection<String>>> task = new Task<StyleSpans<Collection<String>>>() {
        @Override
        protected StyleSpans<Collection<String>> call() throws Exception {
            return computeHighlighting(text);
        }
    };
    executor.execute(task);
    return task;
}

private void applyHighlighting(StyleSpans<Collection<String>> highlighting) {
    codeArea.setStyleSpans(0, highlighting);
}

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
    Matcher matcher = KEYWORD_PATTERN.matcher(text);
    int lastKwEnd = 0;
    StyleSpansBuilder<Collection<String>> spansBuilder
            = new StyleSpansBuilder<>();
    while (matcher.find()) {
        spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
        spansBuilder.add(Collections.singleton("keyword"), matcher.end() - matcher.start());
        lastKwEnd = matcher.end();
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
    return spansBuilder.create();
}

 }

但是您必须在库中添加来自 RichTextFX 的 JAR 文件。但是我不具备有关 css 的知识。所以我无法在样式或主题方面改进它(我想将 Monokai 外观设置为语法文本区域,但别担心我有一天会找到方法。或者我确实在我之前找到了请分享。)谢谢大家感谢您的建议和帮助,尤其是@James_D。

于 2014-07-17T05:33:07.347 回答