【问题标题】:use Color Class in GWT在 GWT 中使用颜色类
【发布时间】:2013-03-14 07:27:38
【问题描述】:

我想在我的 GWT 客户端使用 Color,

我想要这种颜色

                 public static Color myColor = new Color( 152, 207, 204) ;

如果我使用这个导入

                    import java.awt.Color;

在客户端它给了我错误:

             No source code is available for type java.awt.Color; did you forget to inherit a required module

如何在 GWT 客户端使用 RGB 颜色,而不使用 CSS。

【问题讨论】:

    标签: gwt


    【解决方案1】:

    你可以写一个简单的 RGB-to-String 转换器:

    public final  class Helper {
        public static String RgbToHex(int r, int g, int b){
          StringBuilder sb = new StringBuilder();
          sb.append('#')
          .append(Integer.toHexString(r))
          .append(Integer.toHexString(g))
          .append(Integer.toHexString(b));
          return sb.toString();
        }
    }
    

    并使用它:

    nameField.getElement().getStyle().setBackgroundColor(Helper.RgbToHex(50, 100, 150));
    

    ---更新---

    控制负值、大于 255 和 0-15 值的更复杂的方式。

      public static String RgbToHex(int r, int g, int b){
        StringBuilder sb = new StringBuilder();
        sb.append('#')
        .append(intTo2BytesStr(r))
        .append(intTo2BytesStr(g))
        .append(intTo2BytesStr(b));
        return sb.toString();
      }
    
      private static String intTo2BytesStr(int i) {
        return pad(Integer.toHexString(intTo2Bytes(i)));
      }
    
      private static int intTo2Bytes(int i){
        return (i < 0) ? 0 : (i > 255) ? 255 : i;
      }
    
      private static String pad(String str){
        StringBuilder sb = new StringBuilder(str);
        if (sb.length()<2){
          sb.insert(0, '0');
        }
        return sb.toString();
      }
    

    【讨论】:

    • 我认为这不适用于十进制 16 (10 hex) 以下的 R/G/B 值...例如,(5, 10, 20) 将导致包含“#5a14”的字符串......不是你想要的!
    • @Andy King 你说得对。但这里还有一些其他问题: 1. int 的负值。 2. 值 > 大于 255。
    • @FFire - 当然,您可以添加对无效值的检查;我只是指出 valid 值并未全部正确处理。 (顺便说一句,我建议,如果你要使用 StringBuilder,你应该在你的帮助程序中传递一个对象,而不是每次调用 RgbToHex 方法时创建四个 StringBuilder 实例)。
    【解决方案2】:

    您正在使用 Color 的 AWT 类。

    GWT != Java .  //so gwt compiler wont compile the awt classes 
    

    改用这个ThirdParty Color Class

    只需将该类复制到您的utility package 并在client side 上使用它。

    【讨论】:

    • 谢谢,实际上我在具有扩展行的 CellTable 中使用它,所以我以前使用它来使我的列颜色:td.style().trustedBackgroundColor("blue"); :: 现在它除了 String 什么都没有,我可以用任何方式将这些 RGB 颜色提供给我的 celltable Cell。
    • 可能该方法只采用 w3c 诽谤颜色..这里是标准颜色列表docsteve.com/DocSteve/TechNotes/color_test.html
    • 谢谢,为这个列表 +1,这会很有帮助,但最好有一些方法来放置我自己的 RGB 颜色
    • 你有没有试过我给的类中的getHexValue()方法?它会返回像“#000000”这样的颜色代码然后你可以通过我想。
    【解决方案3】:

    您在这里使用了 awt api 颜色。但 GWT 不模拟这个库。参考:

    【讨论】:

      【解决方案4】:

      这是来自 FFire 答案的RgbToHex 方法的更正确版本(此版本对于小于 16 的 r/g/b 值可以正常工作):

      public static String rgbToHex(final int r, final int g, final int b) {
        return "#" + (r < 16 ? "0" : "") + Integer.toHexString(r) + (g < 16 ? "0" : "") +
               Integer.toHexString(g) + (b < 16 ? "0" : "") + Integer.toHexString(b);
      }
      

      当然,如果您愿意,可以使用StringBuilder

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-08
        • 2011-06-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        相关资源
        最近更新 更多