java显示文本_在java中突出显示文本

你最好使用JTextPane或JEditorPane,而不是JTextArea.

文本区域是“普通”文本组件,这意味着虽然它可以以任何字体显示文本,但所有文本都使用相同的字体.

因此,JTextArea不是进行任何文本格式化的便捷组件.

相反,使用JTextPane或JEditorPane,可以很容易地更改加载文本的任何部分的样式(突出显示).

更新:

以下代码突出显示了文本的所需部分.

这不是你想要的.它只是在文本中找到了确切的短语.

但我希望如果您应用算法,您可以轻松实现

修改它以满足您的需求.

import java.lang.reflect.InvocationTargetException;

import javax.swing.*;

import javax.swing.text.*;

import java.awt.*;

public class LineHighlightPainter {

String revisedText = "Extreme programming is one approach "

+ "of agile software development which emphasizes on frequent"

+ " releases in short development cycles which are called "

+ "time boxes. This result in reducing the costs spend for "

+ "changes, by having multiple short development cycles, "

+ "rather than one long one. Extreme programming includes "

+ "pair-wise programming (for code review, unit testing). "

+ "Also it avoids implementing features which are not included "

+ "in the current time box, so the schedule creep can be minimized. ";

String token = "Extreme programming includes pair-wise programming";

public static void main(String args[]) {

try {

SwingUtilities.invokeAndWait(new Runnable() {

public void run() {

new LineHighlightPainter().createAndShowGUI();

}

});

} catch (InterruptedException ex) {

// ignore

} catch (InvocationTargetException ex) {

// ignore

}

}

public void createAndShowGUI() {

JFrame frame = new JFrame("LineHighlightPainter demo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTextArea area = new JTextArea(9, 45);

area.setLineWrap(true);

area.setWrapStyleWord(true);

area.setText(revisedText);

// Highlighting part of the text in the instance of JTextArea

// based on token.

highlight(area, token);

frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);

frame.pack();

frame.setVisible(true);

}

// Creates highlights around all occurrences of pattern in textComp

public void highlight(JTextComponent textComp, String pattern) {

// First remove all old highlights

removeHighlights(textComp);

try {

Highlighter hilite = textComp.getHighlighter();

Document doc = textComp.getDocument();

String text = doc.getText(0, doc.getLength());

int pos = 0;

// Search for pattern

while ((pos = text.indexOf(pattern, pos)) >= 0) {

// Create highlighter using private painter and apply around pattern

hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter);

pos += pattern.length();

}

} catch (BadLocationException e) {

}

}

// Removes only our private highlights

public void removeHighlights(JTextComponent textComp) {

Highlighter hilite = textComp.getHighlighter();

Highlighter.Highlight[] hilites = hilite.getHighlights();

for (int i = 0; i < hilites.length; i++) {

if (hilites[i].getPainter() instanceof MyHighlightPainter) {

hilite.removeHighlight(hilites[i]);

}

}

}

// An instance of the private subclass of the default highlight painter

Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);

// A private subclass of the default highlight painter

class MyHighlightPainter

extends DefaultHighlighter.DefaultHighlightPainter {

public MyHighlightPainter(Color color) {

super(color);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值