Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在JLabel中搜索文本?_Java_Html_Swing_Jsoup - Fatal编程技术网

Java 如何在JLabel中搜索文本?

Java 如何在JLabel中搜索文本?,java,html,swing,jsoup,Java,Html,Swing,Jsoup,我有一些代码,可以访问网站,查找文本,并以JLabel的html样式打印出来。我希望能够改变文本中特定单词的颜色(可能所有单词“cow”都是绿色的)。代码如下: public void code() throws IOException { Document document = Jsoup.connect("http://www.nbcwashington.com/weather/school-closings/").get(); Elements tags = documen

我有一些代码,可以访问网站,查找文本,并以JLabel的html样式打印出来。我希望能够改变文本中特定单词的颜色(可能所有单词“cow”都是绿色的)。代码如下:

public void code() throws IOException
{
    Document document = Jsoup.connect("http://www.nbcwashington.com/weather/school-closings/").get();
    Elements tags = document.select("p");

    String txt = "<html>";
    for (Element tag : tags) {
      txt += tag.text() + "<br/>";
    }
    txt += "</html>";
    output.setText(txt);

    }
public void code()引发IOException
{
Document Document=Jsoup.connect(“http://www.nbcwashington.com/weather/school-closings/).get();
元素标签=文件。选择(“p”);
字符串txt=“”;
用于(元素标记:标记){
txt+=tag.text()+“
”; } txt+=”; output.setText(txt); }
您可以使用
.equals
方法检查值。您可以使用span标记对其进行着色

public void code() throws IOException
{
    Document document = Jsoup.connect("http://www.nbcwashington.com/weather/school-closings/").get();
    Elements tags = document.select("p");

    String txt = "<html>";
    for (Element tag : tags) {
       if(tag.text().equals("cow")){
        txt += "<span style=\"color:#00FF00\">"+tag.text()+"</span><br/>";
       }else{
        txt += tag.text() + "<br/>";
       }
    }
    txt += "</html>";
    output.setText(txt);

    }
public void code()引发IOException
{
Document Document=Jsoup.connect(“http://www.nbcwashington.com/weather/school-closings/).get();
元素标签=文件。选择(“p”);
字符串txt=“”;
用于(元素标记:标记){
if(tag.text().equals(“cow”)){
txt++=“tag.text()+”
; }否则{ txt+=tag.text()+“
”; } } txt+=”; output.setText(txt); }
我有一些代码,可以访问网站,查找文本,并以JLabel的html样式打印出来

我发现使用JTextPane和样式属性比使用HTML更容易

只需将文本作为普通文本添加到文本窗格中,即可搜索文本并根据需要更改属性:

未经测试的代码类似于:

JTextPane textPane = new JTextPane();
textPane.setText(...);

SimpleAttributSet keyword = new SimpleAttributeSet();
StyleConstants.setForeground(keyword, Color.GREEN);

StyledDocument doc = textPane.getStyledDocument();
int length = textPane.getDocument().getLength();
text = textPane.getDocument().getText(0, length);
String search = "cow";
int offset = 0;

while ((offset = text.indexOf(search, offset)) != -1)
{
    doc.setCharacterAttributes(offset, search.length(), keyword, false);
    offset += search.length();
}
还可以使用以下命令使JTextPane看起来像JLabel:

textPane.setOpaque( false );

txt+=tag.text()+“
应该类似于
txt+=tag.text().replaceAll(“cow”,“cow”)+“
。要获得更详细的帮助,请发布。顺便说一句-使用
StringBuilder
比字符串串联更好(更有效)。尝试了两种方法。对于andrew,你将如何添加多个内容?有时候会说Closed或Closed加小写之类的“你怎么添加多个东西?”再次,这次声音会大一点。。要获得更详细的帮助,请发布MCVE.tnx,第三个是错误的,第二个是负一,如果只是因为懒惰的话。请省省我的麻烦(并改变它)。我没有得到第二个。你的意思是用全部替换而不是。等于吗?不,我说的是两个非代码句。。还有,当你不明白我写的东西时,请询问一下,而不是忽视我。@AndrewThompson最终我明白了。