在GWT中使用颜色类 [英] use Color Class in GWT

查看:115
本文介绍了在GWT中使用颜色类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想要这种颜色

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

如果我使用此导入

  import java.awt.Color; 

在客户端它给我错误:

 没有源代码可用于java.awt.Color类型;你忘了继承一个必需的模块吗?

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

解决方案

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

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


$ / code>

并使用它:

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

---更新--- $ b

控制负值的方式更复杂,大于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:我;


private static String pad(String str){
StringBuilder sb = new StringBuilder(str);
if(sb.length()<2){
sb.insert(0,'0');
}
return sb.toString();
}


I want to use Color ,in my GWT client side ,

i want this kind of color

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

if i use this import

                    import java.awt.Color;

at Client side it gives me error:

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

How can i use RGB colors in GWT client side , by NOT using CSS.

解决方案

You can write a simple RGB-to-String converter:

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

And use it:

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

---Update---

More complex way with controlling of negative value, great than 255, and 0-15 value.

  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中使用颜色类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆