Java 中的转换错误
在我的应用程序中,我正在读取 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 GenericObject#getValueByName() 返回一个字符串,对吧?在这种情况下,您需要将字符串转换为可用于创建 Color 实例的字符串。假设字符串是“R,G,B”,然后用逗号分割字符串,将每个分量转换为整数并创建颜色:
另一种选择是定义颜色字段,其颜色名称与中预定义的静态字段匹配颜色(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:
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.
作为 42 的答案的后续,它实际上取决于颜色应该如何存储在 XML 中。还可以将颜色值保存为单个字符串(无逗号),表示颜色的十进制或十六进制值。 (十六进制对于颜色来说更容易理解,例如“FFFF00”代表黄色而不是“16776960”)
例如作为十进制(并且没有错误检查,为了记录,我喜欢使用像四十二这样的默认值)
例如作为十六进制(你需要避免溢出来处理带有 alpha 值的颜色,如 F0123456)
我什至看到十六进制值前面有一个“#”,使它们更像 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)
e.g. as hex (you need avoidOverflows to handle colors with alpha values like F0123456)
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.