Java 突出显示JTextArea中的关键字(Netbeans)
我在Netbeans(Java)中使用了一个文本区域,我想在文本中突出显示某些关键字,类似于编程中的语法突出显示。如果不是在Netbeans中的JTextArea中,我怎么能做到这一点呢?您不能使用JTextArea突出显示单个文本片段 我建议使用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
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);
为了