gwt - 在 GWT 中使用颜色类

标签 gwt

我想在我的 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

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

最佳答案

您可以编写一个简单的 RGB 到字符串转换器:

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();
  }

关于gwt - 在 GWT 中使用颜色类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15403471/

相关文章:

java - GWT 上传表单导致浏览器在 Chrome 中卡住页面

ajax - 单元测试/集成测试 GXT 代码的最佳方法是什么?

gwt - 在 UIBinder 中居中 GWT 元素

java - 方括号后面跟着大括号难道不会导致数组初始化错误吗?

GWT 序列化不应返回接口(interface) : what about parameters and contained objects?

javascript - 从浏览器地址栏执行 Javascript 方法 - GWT

java - 总重量。标签不可见时不占用空间

java - 将 libgdx 项目导出为 HTML - 您是否忘记继承所需的模块?

java - SplitLayoutPanel - 创建后如何更改子项宽度

java - 在 GWT 应用程序中更新客户端站点数据的最佳方法