急等高手解答 Swing JTextPane 的Style问题

franksinger 2007-10-31 03:25:54
写了个小程序,就一个类,代码如下:
现在遇到个很困惑的问题,查找到要高亮显示的词后,清空jpane,再次输入的时候有时候会改变style,全部成了需要高亮显示的style。该如何解决?如何重置style?急等!

public class TextCheckerV1 extends JFrame implements MouseListener {
private JMenuBar bar = null;
private JMenu clearMenu = null;
private JMenu exitMenu = null;
private JMenu checkMenu = null;
private JMenu fileMenu = null;
private String strText = null;
private JTextPane pane = null;
private DefaultStyledDocument doc = null;
private StyleContext sc = null;

private Style defaultStyle = null;
private Style frankStyle = null;

List<String> chars = null;

public TextCheckerV1() {
this.sc = new StyleContext();
this.doc = new DefaultStyledDocument(sc);
this.pane = new JTextPane(doc);

defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
frankStyle = sc.addStyle("FrankStyle", defaultStyle);
StyleConstants.setForeground(frankStyle, Color.red);
StyleConstants.setBackground(frankStyle, Color.yellow);
StyleConstants.setLeftIndent(frankStyle, 8);
StyleConstants.setRightIndent(frankStyle, 8);
StyleConstants.setFirstLineIndent(frankStyle, 8);
StyleConstants.setFontFamily(frankStyle, "Arial");
StyleConstants.setFontSize(frankStyle, 12);

bar = new JMenuBar();
this.setJMenuBar(bar);

fileMenu = new JMenu("About");
fileMenu.setMnemonic('A');

checkMenu = new JMenu("Check");
checkMenu.setMnemonic('C');

clearMenu = new JMenu("Clear");
clearMenu.setMnemonic('R');

exitMenu = new JMenu("Exit");
exitMenu.setMnemonic('E');

fileMenu.addMouseListener(this);
checkMenu.addMouseListener(this);
clearMenu.addMouseListener(this);
exitMenu.addMouseListener(this);

bar.add(fileMenu);
bar.add(checkMenu);
bar.add(clearMenu);
bar.add(exitMenu);

this.getContentPane().add(new JScrollPane(pane));
this.setSize(600, 500);
this.setTitle("Text Checker (ver 1.0)");
this.setLocation(200, 300);
this.setVisible(true);
}

public void mouseClicked(MouseEvent e) {
if (e.getSource() == exitMenu) {
exitSys();
} else if (e.getSource() == clearMenu) {
clearText(this.pane, this.doc);
} else if (e.getSource() == checkMenu) {
checkText(this.pane);
} else if (e.getSource() == fileMenu) {
showFile();
}
}

public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

public void exitSys() {
dispose();
System.exit(0);
}

public void showFile() {
JOptionPane.showMessageDialog(this,
"version 1.0\nauthor: franksinger",
"test", 1);
}

public void clearText(JTextPane jpane, DefaultStyledDocument ddoc) {
if (jpane.getText() != null && (!jpane.getText().equals(""))) {
jpane.setText("");
}

jpane.setEditable(true);
jpane.requestFocus();
}

public void checkText(JTextPane jpane) {
if (jpane.getText() == null || jpane.getText().equals("")) {
return;
} else {
strText = getText(pane.getText());

if (jpane.getText() != null && (!jpane.getText().equals(""))) {
try {
chars = getCharArray();

for (int n = 0; n < chars.size(); n++) {
int charLen = chars.get(n).length();
for (int j = 0; j < strText.length() - (charLen - 1); j++) {
String ch = strText.substring(j, j + charLen);

if (chars.get(n).compareTo(ch) == 0) {
try {
doc.setCharacterAttributes(j, charLen,
frankStyle, false);
} catch (Exception ble) { // BadLocationException
System.out
.println("Exception happens while highlighting!");
ble.printStackTrace();
}
}
}
}
} catch (Exception e) { // BadLocationException
// System.out.println("Exception happens while
// highlighting!");
System.out.println(e.getMessage());
e.printStackTrace();
}
}
jpane.setEditable(false);
}
}

public List<String> getCharArray() {
ArrayList<String> charArray = new ArrayList<String>();
charArray.add("color");
charArray.add("labor");
charArray.add("maximize");
charArray.add("minimize");
charArray.add("prioritize");
charArray.add("specialized");
charArray.add("customize");
charArray.add("theater");
charArray.add("analyzes");
charArray.add("optimize");
charArray.add("favor");
charArray.add("zation");
charArray.add("utilize");
charArray.add("center");
charArray.add("samsung");
charArray.add("program");
charArray.add("organize");
return charArray;
}

public String getText(String str) {
String strNew = str.replaceAll("[\n]", "").toLowerCase();
return strNew;
}

public static void main(String[] args) {
TextCheckerV1 myTest = new TextCheckerV1();
}
}
...全文
101 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧