Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javafx中是否有RSyntaxTextArea的替代方案?_Java_Ide_Javafx 8_Code Editor_Rsyntaxtextarea - Fatal编程技术网

Javafx中是否有RSyntaxTextArea的替代方案?

Javafx中是否有RSyntaxTextArea的替代方案?,java,ide,javafx-8,code-editor,rsyntaxtextarea,Java,Ide,Javafx 8,Code Editor,Rsyntaxtextarea,我正在为java开发一个代码编辑器,它将用于并行计算和分布式计算。我正在寻找Javafx中RSyntaxTextArea的替代方案,bcz我试图在Javafx中实现它,但效果不好,比如有时文本区域的一半不显示,光标在文本区域中滞后 Tab textTab = new Tab("Sample Tab"); RSyntaxTextArea ta= new RSyntaxTextArea(); SwingNode sn = new SwingNode(); String text=""; ta.se

我正在为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的优点

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

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

我已经从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();
}

 }
import java.time.Duration;
导入java.util.Collection;
导入java.util.Collections;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
导入javafx.concurrent.Task;
导入org.fxmisc.richtext.codea;
导入org.fxmisc.richtext.PlainTextChange;
导入org.fxmisc.richtext.stylespan;
导入org.fxmisc.richtext.StyleSpansBuilder;
导入org.reactfx.EventStream;
/**
*
*@作者尼卡
*/
公共类SyntaxTextArea{
私有静态最终字符串[]关键字=新字符串[]{
“抽象”、“断言”、“布尔”、“中断”、“字节”,
“案例”、“捕获”、“字符”、“类”、“常量”,
“继续”、“默认”、“执行”、“加倍”、“其他”,
“枚举”、“扩展”、“最终”、“最终”、“浮点”,
“for”、“goto”、“if”、“implements”、“import”,
“instanceof”、“int”、“interface”、“long”、“native”,
“新”、“包”、“专用”、“受保护”、“公用”,
“返回”、“短”、“静态”、“严格FP”、“超级”,
“切换”、“同步”、“此”、“抛出”、“抛出”,
“暂时的”、“尝试的”、“无效的”、“不稳定的”、“暂时的”
};
私有静态最终模式关键字\u Pattern=Pattern.compile(\\b(“+String.join(“|”,关键字)+”\\b”);
私人密码区;
私人遗嘱执行人;
公共语法区域(){
executor=Executors.newSingleThreadExecutor();
codeArea=新的codeArea();
EventStream textChanges=codeArea.plainTextChanges();
文本更改
.演替结束(持续时间500百万)
.supplyTask(此::computeHighlightingAsync)
.最新(文本更改)
.订阅(此::applyHighlighting);
codeare.getStylesheets().add(org.fxmisc.richtext.demo.JavaKeywordsAsync.class.getResource(“java keywords.css”).toExternalForm());
}
公共void setText(字符串文本){
代码区.replaceText(0,0,text);
}
公共字符串getText(){
返回codeArea.getText();
}
公共文本(字符串文本){
代码区。追加文本(文本);
}
公共代码区getNode(){
返回码区;
}
公共无效设置类型(){
}
专用任务computeHighlightingAsync(){
String text=codea.getText();
任务=新任务(){
@凌驾
受保护的StyleSpans调用()引发异常{
返回computeHighlighting(文本);
}
};
执行者。执行(任务);
返回任务;
}
专用void applyHighlighting(样式跨度高亮显示){
codeArea.setStyleSpans(0,高亮显示);
}
私有静态StyleSpans computeHighlighting(字符串文本){
Matcher Matcher=关键字_PATTERN.Matcher(文本);
int lastKwEnd=0;
StyleSpansBuilder spansBuilder
=新样式spansbuilder();
while(matcher.find()){
spansBuilder.add(Collections.emptyList(),matcher.start()-lastKwEnd);
添加(Collections.singleton(“关键字”)、matcher.end()-matcher.start();
lastKwEnd=matcher.end();
}
spansBuilder.add(Collections.emptyList(),text.length()-lastKwEnd);
返回spansBuilder.create();
}
}

但是您必须从库中的RichTextFX添加JAR文件。然而,我不具备css方面的知识。因此,我无法在样式或主题方面对其进行改进(我想将Monokai Look and feel设置为syntax Text Area,但别担心,总有一天我会找到一种方法。或者有人在我之前找到了,请与我分享。)感谢大家的建议和帮助,尤其是@James_D。

我已经从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();
}

 }
import java.time.Duration;
导入java.util.Collection;
导入java.util.Collections;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
导入javafx.concurrent.Task;
导入org.fxmisc.richtext.codea;
导入org.fxmisc.richtext.PlainTextChange;
导入org.fxmisc.richtext.stylespan;
导入org.fxmisc.richtext.StyleSpansBuilder;
导入org.reactfx.EventStream;
/**
*
*@作者尼卡
*/
公共类SyntaxTextArea{
私有静态最终字符串[]关键字=新字符串[]{
“抽象”、“断言”、“布尔”、“中断”、“字节”,
“案例”、“捕获”、“字符”、“类”、“常量”,
“继续”、“默认”、“执行”、“加倍”、“其他”,
“枚举”、“扩展”、“最终”、“最终”、“浮点”,
“for”、“goto”、“if”、“implements”、“import”,
“instanceof”、“int”、“interface”、“long”、“native”,
“新”、“包”、“专用”、“受保护”、“公用”,
“返回”、“短”、“静态”、“严格FP”、“超级”,
“切换”、“同步”、“此”、“抛出”、“抛出”,
“暂时的”、“尝试的”、“无效的”、“不稳定的”、“暂时的”
};
私有静态最终模式关键字\u Pattern=Pattern.compile(\\b(“+String.join(“|”,关键字)+”\\b”);
私人密码区;
私人遗嘱执行人;
公共语法区域(){
executor=Executors.newSingleThreadExecutor();
codeArea=新的codeArea();
EventStream textChanges=codeArea.plainTextChanges();
文本更改
.演替结束(持续时间500百万)
.supplyTask(此::computeHighlightingAsync)
.最新(文本更改)
.