gpt4 book ai didi

java - JText组件 : how to update an existing StyledDocument elements

转载 作者:行者123 更新时间:2023-12-02 12:40:09 25 4
gpt4 key购买 nike

我使用两个 DefaultStyledDocument 编写了两个 Pane diff 类。

现在我想让用户就地更改用于显示删除和插入的颜色(以及粗体),而不需要重新比较或保留中间比较结果。

我面临的问题是 insertString 复制属性,因此我不能随后更新 Style 实例。

我尝试了 DefaultStyledDocument.setLogicalStyle(int, Style) 但视觉上没有任何变化。

您是否看到一种(简单/便宜)无需重新创建整个文档即可进行样式更新的方法?

最佳答案

逻辑样式适用于整个段落。据我所知,无法将样式应用于段落中的字符并让 Swing 自动监听样式中的更改。

您可以做的是将样式用作常规属性集,然后使用 ElementIterator 扫描样式元素,因为样式的名称存储在 AttributeSet.NameAttribute 下属性键:

void updateHighlight(StyledDocument doc,
AttributeSet newStyle) {

Object styleName = newStyle.getAttribute(AttributeSet.NameAttribute);

ElementIterator i = new ElementIterator(doc);
for (Element e = i.first(); e != null; e = i.next()) {
AttributeSet attr = e.getAttributes();
Object name = attr.getAttribute(AttributeSet.NameAttribute);
if (styleName.equals(name)) {
int start = e.getStartOffset();
int end = e.getEndOffset();
doc.setCharacterAttributes(start, end - start,
newStyle, false);
}
}
}

这是否符合“简单/便宜”的标准由您决定。

这是一个演示:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;

import javax.swing.Action;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class StyleChanger {
public static void main(String[] args) {
EventQueue.invokeLater(() -> show());
}

private static final String HIGHLIGHT =
StyleChanger.class.getName() + ".highlight";

static void show() {
JTextPane textPane = new JTextPane();

Style normal = textPane.getStyle(StyleContext.DEFAULT_STYLE);
Style highlight = textPane.addStyle(HIGHLIGHT, normal);
StyleConstants.setBackground(highlight, Color.YELLOW);

class Fragment {
final AttributeSet style;
final String text;

Fragment(AttributeSet style,
String text) {
this.style = style;
this.text = text;
}
}

Fragment[] fragments = {
new Fragment(highlight, "Space"),
new Fragment(normal, ": the final frontier. These are the "),
new Fragment(highlight, "voyages"),
new Fragment(normal, " of the starship "),
new Fragment(highlight, "Enterprise"),
new Fragment(normal, ". Its five-year "),
new Fragment(highlight, "mission"),
new Fragment(normal, ": to explore strange "),
new Fragment(highlight, "new worlds"),
new Fragment(normal, "; to seek out "),
new Fragment(highlight, "new life"),
new Fragment(normal, " and new civilizations; to "),
new Fragment(highlight, "boldly go"),
new Fragment(normal, " where no man has gone before!"),
};

for (Fragment fragment : fragments) {
textPane.setCharacterAttributes(fragment.style, true);
textPane.replaceSelection(fragment.text);
}

Action change = new AbstractAction("Change Highlight\u2026") {
private static final long serialVersionUID = 1;

@Override
public void actionPerformed(ActionEvent event) {
Color color = JColorChooser.showDialog(
textPane.getTopLevelAncestor(),
"Highlight Color",
textPane.getBackground());

if (color != null) {
StyleConstants.setBackground(highlight, color);
updateHighlight(textPane.getStyledDocument(), highlight);
}
}
};

JButton changeButton = new JButton(change);

JPanel buttonPane = new JPanel();
buttonPane.add(changeButton);

JFrame frame = new JFrame("Style Changer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new JScrollPane(textPane));
frame.getContentPane().add(buttonPane, BorderLayout.PAGE_END);

frame.setSize(450, 300);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

private static void updateHighlight(StyledDocument doc,
AttributeSet newStyle) {

Object styleName = newStyle.getAttribute(AttributeSet.NameAttribute);

ElementIterator i = new ElementIterator(doc);
for (Element e = i.first(); e != null; e = i.next()) {
AttributeSet attr = e.getAttributes();
Object name = attr.getAttribute(AttributeSet.NameAttribute);
if (styleName.equals(name)) {
int start = e.getStartOffset();
int end = e.getEndOffset();
doc.setCharacterAttributes(start, end - start,
newStyle, false);
}
}
}
}

关于java - JText组件 : how to update an existing StyledDocument elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44971180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com