插入字符串后重置文档中的属性
我有一个 JTextComponent,用户可以通过两种方式在其中输入文本:
- 他可以直接在其中键入文本。
- 使用第二个控件,他可以间接将文本插入其中。这是通过以编程方式调用 insertString() 来完成的。
以第二种方式插入的文本中使用的字体将与直接输入的字体不同。输入文本的字体将是 JTextComponent 的默认字体。
这是代码。 TODO 是我不知道该怎么做的事情。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class ResetAttributesInDocument extends JApplet {
private ButtonListener bl = new ButtonListener();
private JTextPane myJTextComponent;
public void init() {
JPanel contentPanel = new JPanel(new BorderLayout());
myJTextComponent = new JTextPane();
contentPanel.add(myJTextComponent, BorderLayout.CENTER);
JButton insertTextButton = new JButton("Insert text");
insertTextButton.addActionListener(bl);
contentPanel.add(insertTextButton, BorderLayout.SOUTH);
getContentPane().add(contentPanel);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
final Document doc = myJTextComponent.getDocument();
final int caretPosition = myJTextComponent.getCaretPosition();
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setFontFamily(set, "Courier New");
// Possibly add more attributes to set here.
try {
doc.insertString(caretPosition, "text in Courier New", set);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
// TODO Reset the attributes back to what they originally were so
// that any new text the user enters after the inserted text is in
// the original font.
}
}
}
有没有办法把属性恢复到原来的样子?
I have a JTextComponent in which a user can enter text in two ways:
- He can type text directly into it.
- Using a second control, he can indirectly insert text into it. This is done by programmatically calling insertString().
The font used in the text inserted in the second way will be different than the font that is typed in directly. The font of the text typed in will be the default font of the JTextComponent.
Here is the code. The TODO is what I don't know how to do.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class ResetAttributesInDocument extends JApplet {
private ButtonListener bl = new ButtonListener();
private JTextPane myJTextComponent;
public void init() {
JPanel contentPanel = new JPanel(new BorderLayout());
myJTextComponent = new JTextPane();
contentPanel.add(myJTextComponent, BorderLayout.CENTER);
JButton insertTextButton = new JButton("Insert text");
insertTextButton.addActionListener(bl);
contentPanel.add(insertTextButton, BorderLayout.SOUTH);
getContentPane().add(contentPanel);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
final Document doc = myJTextComponent.getDocument();
final int caretPosition = myJTextComponent.getCaretPosition();
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setFontFamily(set, "Courier New");
// Possibly add more attributes to set here.
try {
doc.insertString(caretPosition, "text in Courier New", set);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
// TODO Reset the attributes back to what they originally were so
// that any new text the user enters after the inserted text is in
// the original font.
}
}
}
Is there a way to reset the attributes back to what they originally were?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题没有意义。
首先,您声明“如果用户输入...”,这意味着用户做了某事。
然后你说你想要“做这样的事情......”,这意味着你正在代码中做一些不受用户控制的事情,因为你正在手动调用 insertString() 方法。
发布您的 SSCCE 以演示该问题并描述复制该问题的确切步骤。
每次插入符号更改位置时都会重置属性,因此您需要使用 CaretListener 来处理此问题。像这样的东西:
Your question doesn't make sense.
First you state "if the user types into ..." which implies the user does something.
Then you say you want "to do something like this...", which implies you are doing something in the code which is not under user control because you are manually invoking the insertString() method.
Post your SSCCE that demonstrates the problem and describe the exact steps to duplicate the problem.
Attributes are reset every time the caret changes position so you need to handle this with a CaretListener. Something like: