Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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
Java 突出显示JTextArea中的关键字(Netbeans)_Java_Swing_Netbeans_Syntax Highlighting_Jtextarea - Fatal编程技术网

Java 突出显示JTextArea中的关键字(Netbeans)

Java 突出显示JTextArea中的关键字(Netbeans),java,swing,netbeans,syntax-highlighting,jtextarea,Java,Swing,Netbeans,Syntax Highlighting,Jtextarea,我在Netbeans(Java)中使用了一个文本区域,我想在文本中突出显示某些关键字,类似于编程中的语法突出显示。如果不是在Netbeans中的JTextArea中,我怎么能做到这一点呢?您不能使用JTextArea突出显示单个文本片段 我建议使用JTextPane,这样您就可以使用样式化属性 基本代码如下所示: JTextPane textPane = new JTextPane(); textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\ns

我在Netbeans(Java)中使用了一个文本区域,我想在文本中突出显示某些关键字,类似于编程中的语法突出显示。如果不是在Netbeans中的JTextArea中,我怎么能做到这一点呢?

您不能使用JTextArea突出显示单个文本片段

我建议使用
JTextPane
,这样您就可以使用样式化属性

基本代码如下所示:

JTextPane textPane = new JTextPane();
textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );
StyledDocument doc = textPane.getStyledDocument();

//  Define a keyword attribute

SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);

//  Change attributes on some text

doc.setCharacterAttributes(0, 5, keyWord, false);

不能使用JTextArea突出显示单个文本

我建议使用
JTextPane
,这样您就可以使用样式化属性

基本代码如下所示:

JTextPane textPane = new JTextPane();
textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );
StyledDocument doc = textPane.getStyledDocument();

//  Define a keyword attribute

SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);

//  Change attributes on some text

doc.setCharacterAttributes(0, 5, keyWord, false);

不能使用JTextArea突出显示单个文本

我建议使用
JTextPane
,这样您就可以使用样式化属性

基本代码如下所示:

JTextPane textPane = new JTextPane();
textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );
StyledDocument doc = textPane.getStyledDocument();

//  Define a keyword attribute

SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);

//  Change attributes on some text

doc.setCharacterAttributes(0, 5, keyWord, false);

不能使用JTextArea突出显示单个文本

我建议使用
JTextPane
,这样您就可以使用样式化属性

基本代码如下所示:

JTextPane textPane = new JTextPane();
textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );
StyledDocument doc = textPane.getStyledDocument();

//  Define a keyword attribute

SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);

//  Change attributes on some text

doc.setCharacterAttributes(0, 5, keyWord, false);
为了