Java 中的转换错误

发布于 2024-12-10 01:21:43 字数 851 浏览 0 评论 0原文

在我的应用程序中,我正在读取 .xml 文件并将数据写入 JTable 中。除了表的数据之外,.xml 文件还包含定义每行背景颜色的属性。我的单元格渲染方法看起来像这样:

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int col) {
    JComponent comp = new JLabel();

    if (null != value) {
        //reading the data and writing it in the comp
    }

    GenericTableModel model = (GenericTableModel) table.getModel();
    GenericObject go = model.getRowObject(row);

    Color test = new Color(255, 255, 255);
    if (go.getValueByName("COLOR") == null){

    }else{
        test =(Color) go.getValueByName("COLOR");
    }

    comp.setBackground(test);

    return comp;
}

.xml 文件在程序中初始化。我的问题是我不知道如何在文件中定义颜色,以便变量测试能够将其保存为颜色。我尝试将其写为“Color.white”、“white”甚至“255, 255, 255”,但当我尝试将其保存在变量中时出现转换错误。

关于如何定义文件中的颜色有什么想法吗?

In my application, I am reading a .xml file and writing the data in a JTable. Apart from the data for the table, the .xml file contains an attribute defining the background color of each row. My method for cell rendering looks something like this:


public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int col) {
    JComponent comp = new JLabel();

    if (null != value) {
        //reading the data and writing it in the comp
    }

    GenericTableModel model = (GenericTableModel) table.getModel();
    GenericObject go = model.getRowObject(row);

    Color test = new Color(255, 255, 255);
    if (go.getValueByName("COLOR") == null){

    }else{
        test =(Color) go.getValueByName("COLOR");
    }

    comp.setBackground(test);

    return comp;
}

The .xml file is initialized within the program. My problem is that I don't know how to define the color in the file so that the variable test will be able to save it as a color. I tried writing it as "Color.white", "white" and even "255, 255, 255" but i get a casting error when I try saving it in the variable.

Any ideas as to how could I define the color in the file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧夏天 2024-12-17 01:21:43

我认为 GenericObject#getValueByName() 返回一个字符串,对吧?在这种情况下,您需要将字符串转换为可用于创建 Color 实例的字符串。假设字符串是“R,G,B”,然后用逗号分割字符串,将每个分量转换为整数并创建颜色:

public static Color fromString(String rgb, Color deflt) {
    String[] comp = rgb.split(",");
    if (comp.length != 3)
        return deflt;
    int rc[] = new int[3];
    for (int i = 0; i < 3; ++i) {
        rc[i] = Integer.parseInt(comp[i].trim());
        if (rc[i] < 0 || rc[i] > 255)
            return deflt;
    }
    Color c = new Color(rc[0], rc[1], rc[2]);
    return c;
}

另一种选择是定义颜色字段,其颜色名称与中预定义的静态字段匹配颜色(Color.BLACK、Color.RED 等),并使用反射来获取正确的字段,但我将其留作练习。

I take it that GenericObject#getValueByName() returns a string, right? In that case you need to convert the string to something that can be used to create a Color instance. Assuming that the string is "R,G,B", then split the string on the comma, convert each component to an integer and create a color:

public static Color fromString(String rgb, Color deflt) {
    String[] comp = rgb.split(",");
    if (comp.length != 3)
        return deflt;
    int rc[] = new int[3];
    for (int i = 0; i < 3; ++i) {
        rc[i] = Integer.parseInt(comp[i].trim());
        if (rc[i] < 0 || rc[i] > 255)
            return deflt;
    }
    Color c = new Color(rc[0], rc[1], rc[2]);
    return c;
}

The other alternative is to define the color field with color names matching the predefined static fields in Color (Color.BLACK, Color.RED, etc), and use reflection to get the correct field, but I leave that as an excercise.

烟燃烟灭 2024-12-17 01:21:43

作为 42 的答案的后续,它实际上取决于颜色应该如何存储在 XML 中。还可以将颜色值保存为单个字符串(无逗号),表示颜色的十进制或十六进制值。 (十六进制对于颜色来说更容易理解,例如“FFFF00”代表黄色而不是“16776960”)

例如作为十进制(并且没有错误检查,为了记录,我喜欢使用像四十二这样的默认值)

public static Color readColor(String decimalString) {
   return new Color(Integer.parseInt(decimalString));
}

public String writeColor(Color color) {
    return Integer.toString(color.getRGB());
}

例如作为十六进制(你需要避免溢出来处理带有 alpha 值的颜色,如 F0123456)

public static Color readColor(String hexString) {
    long avoidOverflows = Long.parseLong(hexString, 16);
    return new Color((int)long);
}

public String writeColor(Color color) {
    return Integer.toHexString(color.getRGB(), 16);
}

我什至看到十六进制值前面有一个“#”,使它们更像 HTML。因此,这实际上取决于 XML 的规范。

As a followup to forty-two's answer, it really depends on how the color is supposed to be stored in the XML. It would also be possible to save the color's value as a single string (no commas), representing either the decimal or hex value of the color. (Hex is more human-readable for colors, e.g. "FFFF00" for Yellow instead of "16776960")

e.g. as decimal (and with no error checking, for the record, I like default values like forty-two used)

public static Color readColor(String decimalString) {
   return new Color(Integer.parseInt(decimalString));
}

public String writeColor(Color color) {
    return Integer.toString(color.getRGB());
}

e.g. as hex (you need avoidOverflows to handle colors with alpha values like F0123456)

public static Color readColor(String hexString) {
    long avoidOverflows = Long.parseLong(hexString, 16);
    return new Color((int)long);
}

public String writeColor(Color color) {
    return Integer.toHexString(color.getRGB(), 16);
}

I've even seen the hex values preceded by a "#" to make them more HTML-like. So, it really depends on the spec for your XML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文