簡體   English   中英

Java-Swing:使用HTML設置所選文本顏色

[英]Java-Swing: Setting Selected Text Color with HTML

我在查找如何使用HTML設置我的JList的選定文本的顏色時遇到問題。

使用Java Swing和HTML我已經設法為我的JList中的每個String的特定部分着色,我的示例如下所示:

在此輸入圖像描述

這是理想的,因為我可以為每個條目設置多種不同的顏色!

但是,選擇文本時,只有默認的黑色文本變為白色! html顏色的文本保持其顏色而不是變為白色,這導致一些非常難以閱讀的文本的某些顏色:

在此輸入圖像描述

如何在選擇時設置文本顏色?

我已經嘗試在JList上使用JList的setSelectionForeground(Color.WHITE)方法,但它不影響html顏色的文本(盡管它確實影響了非html顏色的文本)

我還閱讀了Oracle的HTML-in-Swing教程(我首先發現了HTML着色)但找不到解決方案。

這是我的簡短示例的代碼:

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.UIManager;
import java.awt.Color;

public class JListSelectionColorTest extends JFrame {

    private String[] exampleText = {"Some example text without any color changes",
        "Some more example text without color changes",
        "Even more plain text!", 
        "<html>Uncolored Text! <font color=orange>Now some example Text with color!</font> more Uncolored Text!</html>", 
        "<html>Uncolored Text! <font color=green>And some more example text with color! Text, Text, Text!</font> more Uncolored Text!</html>",
        "<html>Uncolored Text! <font color=red>A string with red color, Text Text Text!</font> more Uncolored Text!</html>",
        "<html>Uncolored Text! <font color=blue>And finally a string with blue color, Text Text Text!</font> more Uncolored Text!</html>",
        "<html>Uncolored Text! <font color=purple><selection color=white>Testing if some html can turn the selection color white!</selection></font> more Uncolored Text!</html>"};

    public JListSelectionColorTest() {
        super("JList Selection Color Test");

        // Set the Look and Feel of the window to the Native System's Look and Feel
        // (When using the default Look and Feel the problem still persists!)
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Create a JList of Strings containing the exampleText String array
        JList<String> exampleJList = new JList<String>(exampleText);

        // Set the JList's text selection color to white
        exampleJList.setSelectionForeground(Color.WHITE); // This doesn't seem to affect the html-colored text's selection foreground

        // Add the JList to the JFrame
        add(exampleJList);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }
    public static void main(String[] args) {
        new JListSelectionColorTest();
    }
}

一種方法是使用ListCellRenderer,它使用replaceAll(...)從您選擇的String中刪除HTML代碼。 上帝我討厭在HTML中使用正則表達式,如果您的String中包含非HTML尖括號<> ,則無法使用。

import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.UIManager;

import java.awt.Color;
import java.awt.Component;

public class JListSelectionColorTest extends JFrame {

   private String[] exampleText = {
         "Some example text without any color changes",
         "Some more example text without color changes",
         "Even more plain text!",
         "<html>Uncolored Text! <font color=orange>Now some example Text with color!</font> more Uncolored Text!</html>",
         "<html>Uncolored Text! <font color=green>And some more example text with color! Text, Text, Text!</font> more Uncolored Text!</html>",
         "<html>Uncolored Text! <font color=red>A string with red color, Text Text Text!</font> more Uncolored Text!</html>",
         "<html>Uncolored Text! <font color=blue>And finally a string with blue color, Text Text Text!</font> more Uncolored Text!</html>",
         "<html>Uncolored Text! <font color=purple><selection color=white>Testing if some html can turn the selection color white!</selection></font> more Uncolored Text!</html>" };

   public JListSelectionColorTest() {
      super("JList Selection Color Test");

      try {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {
         e.printStackTrace();
      }
      JList<String> exampleJList = new JList<String>(exampleText);

      exampleJList.setCellRenderer(new MyCellRenderer());
      exampleJList.setSelectionForeground(Color.WHITE); 
      add(exampleJList);

      pack();
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }

   public static void main(String[] args) {
      new JListSelectionColorTest();
   }

   private static class MyCellRenderer extends DefaultListCellRenderer {
      // create a non-greedy regex to capture anything between angle brackets.
      private String regex = "\\<.*?\\>";

      @Override
      public Component getListCellRendererComponent(JList<?> list,
            Object value, int index, boolean isSelected, boolean cellHasFocus) {
         if (value == null) {
            return super.getListCellRendererComponent(list, value, index,
                  isSelected, cellHasFocus);
         }

         // only interested in selected Strings
         if (isSelected) {
            String valueStr = value.toString(); // get the String
            valueStr = valueStr.replaceAll(regex, "");  // extract the HTML
            value = valueStr;  // put back into value Object variable
         }
         return super.getListCellRendererComponent(list, value, index,
               isSelected, cellHasFocus);
      }
   }
}

一個更好的解決方案:使用JSoup去除HTML。
更好的是,創建一個包含HTML和非HTML字符串的類,讓List保持此類的對象,並在渲染器中交換字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM