Java 使用SwingWorker在JTextPane中突出显示语法
我正在尝试在JTextPane中突出显示文本。我正在使用SwingWorker在后台进行突出显示。但我无法获得所需的输出。Java 使用SwingWorker在JTextPane中突出显示语法,java,swing,jtextpane,Java,Swing,Jtextpane,我正在尝试在JTextPane中突出显示文本。我正在使用SwingWorker在后台进行突出显示。但我无法获得所需的输出。 我的代码如下: 主类: class MultiColor { private static void displayGUI() { final JTextPane ta = new JTextPane(); JFrame frame = new JFrame("EXAMPLE"); JButton jb = new J
我的代码如下:
主类:
class MultiColor {
private static void displayGUI() {
final JTextPane ta = new JTextPane();
JFrame frame = new JFrame("EXAMPLE");
JButton jb = new JButton("Change");
JScrollPane jsp = new JScrollPane(ta);
frame.add(jsp, BorderLayout.CENTER);
frame.add(jb, BorderLayout.PAGE_END);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
Modify mm = new Modify(ta);
mm.execute();
}
});
}
public static void main(String[] a) {
Runnable r = new Runnable() {
@Override
public void run() {
displayGUI();
}
};
SwingUtilities.invokeLater(r);
}
}
class Modify extends SwingWorker<Void,Object> {
private JTextPane ta;
private StyleContext style;
private AttributeSet textStyle;
public Modify(JTextPane text) {
ta = text;
}
private void matching(String str){
style = StyleContext.getDefaultStyleContext();
textStyle = style.addAttribute(style.getEmptySet(),StyleConstants.Foreground, Color.red);
textStyle = style.addAttribute(textStyle,StyleConstants.FontSize, 15);
String regx = "\\b(class|int|void|static|final|public|private|protected|float|if|else|for|while|try|catch|boolean|import|return)\\b";
String input = str;
Pattern p = Pattern.compile(regx);
Matcher m = p.matcher(input);
while(m.find())
ta.getStyledDocument().setCharacterAttributes(m.start(),(m.end() - m.start()),textStyle, false);
}
@Override
protected Void doInBackground() {
matching(ta.getText());
return null;
}
@Override
protected void done() {
}
}
而修改类是:
class MultiColor {
private static void displayGUI() {
final JTextPane ta = new JTextPane();
JFrame frame = new JFrame("EXAMPLE");
JButton jb = new JButton("Change");
JScrollPane jsp = new JScrollPane(ta);
frame.add(jsp, BorderLayout.CENTER);
frame.add(jb, BorderLayout.PAGE_END);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
Modify mm = new Modify(ta);
mm.execute();
}
});
}
public static void main(String[] a) {
Runnable r = new Runnable() {
@Override
public void run() {
displayGUI();
}
};
SwingUtilities.invokeLater(r);
}
}
class Modify extends SwingWorker<Void,Object> {
private JTextPane ta;
private StyleContext style;
private AttributeSet textStyle;
public Modify(JTextPane text) {
ta = text;
}
private void matching(String str){
style = StyleContext.getDefaultStyleContext();
textStyle = style.addAttribute(style.getEmptySet(),StyleConstants.Foreground, Color.red);
textStyle = style.addAttribute(textStyle,StyleConstants.FontSize, 15);
String regx = "\\b(class|int|void|static|final|public|private|protected|float|if|else|for|while|try|catch|boolean|import|return)\\b";
String input = str;
Pattern p = Pattern.compile(regx);
Matcher m = p.matcher(input);
while(m.find())
ta.getStyledDocument().setCharacterAttributes(m.start(),(m.end() - m.start()),textStyle, false);
}
@Override
protected Void doInBackground() {
matching(ta.getText());
return null;
}
@Override
protected void done() {
}
}
类修改扩展SwingWorker{
私人JTTA;
私人风格背景风格;
私有属性集文本样式;
公共修改(JTextPane文本){
ta=文本;
}
私有空匹配(字符串str){
style=StyleContext.getDefaultStyleContext();
textStyle=style.addAttribute(style.getEmptySet(),StyleConstants.Foreground,Color.red);
textStyle=style.addAttribute(textStyle,StyleConstants.FontSize,15);
String regx=“\\b(class | int | void | static | final | public | private | protected | float | if | else | for | while | try | catch | boolean | import | return)\\b”;
字符串输入=str;
Pattern p=Pattern.compile(regx);
匹配器m=p.Matcher(输入);
while(m.find())
ta.getStyledDocument().setCharacterAttributes(m.start(),(m.end()-m.start()),textStyle,false);
}
@凌驾
受保护的Void doInBackground(){
匹配(ta.getText());
返回null;
}
@凌驾
受保护的void done(){
}
}
我的输出是:
我想显示具有指定文本样式的所有关键字。如何获得所需的输出。您的高亮显示偏移量似乎已关闭
有关可能的原因和简单的解决方案,请参阅。doInBackground是workers线程,不指定在Swing GUI中更改任何内容,您需要使用publish()或setProcess