我有一个声明如下的JTextPane:
JTextPane box = new JTextPane();
JScrollPane scroll = new JScrollPane();
StyledDocument doc = box.getStyledDocument();
scroll.setViewportView(box);
scroll = new JScrollPane(box);
Run Code Online (Sandbox Code Playgroud)
我正在附加文本如下:
public void appendChatText(String text)
{
try
{
doc.insertString(doc.getLength(), text, null);
box.setAutoscrolls(true);
box.setCaretPosition(box.getDocument().getLength());
}
catch(BadLocationException e)
{
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我还设法轻松地让JTextPane根据需要显示图像和组件,但我无法弄清楚如何将可点击文本编码到JTextPane中.例如,我希望它打印一条消息,上面写着"文件上传到服务器.接受*拒绝*",如果用户点击接受或拒绝字符串,则它会执行相应的功能.关于如何有效实现这一目标的任何想法?
如何在JTextPane中轻松编辑所选文本的样式?这方面似乎没有太多资源.即使你可以指导我获得一个很好的资源,我也非常感激.
另外,如何获取所选文本的当前样式?我试过styledDoc.getLogicalStyle(textPane.getSelectionStart());但它似乎没有起作用.
所以我创建了自己的文本窗格类(扩展JTextPane),我正在使用下面的方法向其中添加文本.但是,窗格需要可以编辑才能添加文本,但这允许用户也可以编辑窗格中的内容.
任何人都可以告诉我如何在不让用户操纵那些内容的情况下向窗格添加文本?
public void appendColor(Color c, String s) {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
int len = getDocument().getLength();
setCaretPosition(len);
setCharacterAttributes(aset, false);
replaceSelection(s);
setCaretPosition(getDocument().getLength());
}
Run Code Online (Sandbox Code Playgroud) 如何在JTextPane中实现自动换行?
我试图准确理解它是如何工作的,以便我可以修改行为.现在,如果我在JScrollPane中有一个标准的JTextPane,它会在空格处中断文本,但不会在长单词内部 - 如果有一个文本字符串没有比窗口宽的空格,它将不会换行/中断将出现一个水平滚动条.随着文本宽度的增加,ParagraphView的宽度(通过getWidth())会增加以保存文本.
Lapitsky的这篇文章说LabelView.getBreakWeight()返回带有空格的标签的View.ExcellentBreakWeight和没有空格的标签的View.GoodBreakWeight(GlyphView.java中的代码似乎证实了这一点),为什么它不会破坏?它是以某种方式返回BadBreakWeight而不是GoodBreakWeight?还是有一些布局问题?还是有错误?
这是一些代码(为了您的观看乐趣):
//somewhere inside JPanel or JFrame constructor
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
add(scrollPane);
Run Code Online (Sandbox Code Playgroud)
请注意,如果我取出滚动窗格并仅使用文本窗格(它只是在窗口外面时被剪切),它仍然不会换行.
Swing的javadoc似乎没有详细介绍一些对象(如JTextPane,View和相关对象)如何协同工作.是否还有关于此类课程设计的进一步文档,或许详细说明了每个课程的目的以及它们如何一起工作?它不公开吗?(或者我是唯一一个在这样的事情上遇到麻烦的人?或者说文档不足以限制一些典型的开发人员不会处理的事情?)
我正在尝试从JTextPane中删除所有文本.我以为你可以简单地使用:
textPane.setText("");
Run Code Online (Sandbox Code Playgroud)
这可以工作,但由于某种原因,在调用该方法后总是有一个空行.为什么这样,我该如何防止这种情况?
我先来描述下图:

我希望绿线突出显示的所有字符都打印在一列中.
String的字体是monospaced Courier New.但是,似乎空格字符不会打印为等宽字体(请参阅"虚线"线与开头的空格字符行).
要打印字符串,我在JTextPane组件上使用标准Java Print Service API:
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(myTextPane);
pj.print();
Run Code Online (Sandbox Code Playgroud)
据我所知,Java Print Service API实际上调用了myTextPane的paint()方法.因此,预览应与String的打印版本完全相同.
但事实并非如此.预览似乎没有误解等宽空间字符(参见最后一张图).预览看起来与我想要打印的文本完全相同.

有关如何强制JavaPrintServiceAPI正确打印等宽字符空间字符的任何建议吗?
有没有人对如何搜索文本文件并在JComponent中列出结果有任何想法,比如JPanel.
我一直试图让这项工作连续两天,但没有成功真的很感激回复.非常感谢提前.
我一直在尝试编写一个处理文本文件搜索查询的类.我的主要目标是在文本文件中获取包含在JTextField中输入的搜索关键字的行,并将其打印在适当的JComponent中(类似于JTextField,JTextPane,最适用的).
我希望搜索结果显示在谷歌搜索结果显示方式的列中,以便文本文件中的每一行都打印在自己的行中.我被告知最好使用ArrayList.我真的不知道该怎么做.我从各地获取了想法,这是我到目前为止所做的:
提前多多欣赏.我是Java的新手.我一整天都在努力做到这一点并且没有走得太远.我愿意尝试任何提供的东西,甚至是新方法.
// The class that handles the search query
// Notice that I've commented out some parts that show errors
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JTextPane;
public class Search {
public static String path;
public static String qri;
public Search(String dTestFileDAT, String qry) {
path = dTestFileDAT;
qri = qry;
}
public static JTextPane resultJTextPane;
public static List<String> linesToPresent = new ArrayList<String>();
public static …Run Code Online (Sandbox Code Playgroud) 我正在使用 JTextPane 创建一个文本编辑器,它允许用户更改所选文本的颜色。但是,当用户选择文本,然后选择更改颜色的选项(例如,更改为红色)时,文本不会显示为红色,直到取消选择文本。我尝试使用 setSelectedTextColor 来更改所选文本的颜色,但这不起作用,因为此后无论何时选择文本都会将文本更改为红色。有没有办法让选定的文本显示为它的实际颜色?Or like the way it works in Word where it's not the actual color of the text, but when text of different colors are selected they show up as different colors even when selected.
我使用以下代码设置 JTextPane 和将所选文本更改为红色的按钮:
JButton redButton = new JButton(new StyledEditorKit.ForegroundAction("red", Color.RED));
redButton.setFocusable(false);
buttonPanel.add(redButton);
Run Code Online (Sandbox Code Playgroud)
JTextPane 设置为内容类型 HTML 并使用 HTMLEditorKit:
p=new JTextPane();
p.setSize(300, 300);
kit = new HTMLEditorKit();
p.setEditorKit(kit);
p.setDocument(kit.createDefaultDocument());
p.setContentType("text/html");
p.setEditable(true);
Run Code Online (Sandbox Code Playgroud)
如果您需要更多源代码来理解这个问题,请告诉我。谢谢你!
我有一个StyledDocument实例,其中包含代表数字的字符串.通过覆盖字符串元素的属性,我正在使用我从LabelView派生的自定义视图.我想允许用户选择显示数字的基数,例如十进制或十六进制.这是我目前的解决方案:
public class AddressView extends LabelView {
@Override
public Segment getText(int p0, int p1) {
// get string representation of the number from the model
String stringNumber = super.getText(p0, p1).toString();
// get base from document's attributes
int base = getDocument().getProperty(BaseProperty);
// convert string to desired base
String stringNumberOverride = Integer.toString(Integer.parseInt(stringNumber), base);
// return as segment (can have a different length, JTextPane doesn't like that)
char[] strNum = stringNumberOverride.toCharArray();
return new Segment(strNum, 0, strNum.length);
}
}
Run Code Online (Sandbox Code Playgroud)
只有一个问题:选择文本不再起作用,因为返回的getText字符串没有请求的长度(p1 - p0).实现JTextPane组件以精确选择多个字符,因此使用上述解决方案,用户只能选择p1-p0字符,即使新基本可能在模型中显示数字字符串的更长字符串表示.
那么,让View显示一个与模型中的String长度不同的String的正确方法是什么?我不想仅仅因为用户希望内容的不同表示而更新模型.
编辑:这是一个独立的例子.尝试选择文本 …
我正在尝试创建简单的IDE并基于我的JTextPane着色
我为源代码着色的方法是覆盖StyledDocument中的insertString和removeString方法.
经过多次测试,我已经完成了评论和关键词.
Q1:至于我的Strings着色,我根据这个正则表达式为我的字符串着色:
Pattern strings = Pattern.compile("\"[^\"]*\"");
Matcher matcherS = strings.matcher(text);
while (matcherS.find()) {
setCharacterAttributes(matcherS.start(), matcherS.end() - matcherS.start(), red, false);
}
Run Code Online (Sandbox Code Playgroud)
这种方式有99%的时间可用,除非我的字符串包含一个特定类型的字符串,其中有一个"\代码内部.这会弄乱我的整个颜色编码.任何人都可以纠正我的正则表达式来修复我的错误吗?
Q2:对于整数和十进制着色,基于此正则表达式检测数字:
Pattern numbers = Pattern.compile("\\d+");
Matcher matcherN = numbers.matcher(text);
while (matcherN.find()) {
setCharacterAttributes(matcherN.start(), matcherN.end() - matcherN.start(), magenta, false);
}
Run Code Online (Sandbox Code Playgroud)
通过使用正则表达式"\ d +",我只处理整数而不是浮点数.此外,作为另一个字符串的一部分的整数是匹配的,这不是我想要的IDE内部.哪个是用于整数颜色编码的正确表达式?
以下是输出的屏幕截图:

感谢您提前帮助!