Java 从config.properties加载setForeground的颜色
嘿,有没有办法让这段代码从config.properties加载颜色Java 从config.properties加载setForeground的颜色,java,Java,嘿,有没有办法让这段代码从config.properties加载颜色 font.setForeground(new Color(configi.getProperty("fonts_colo"))); 我的config.properties fonts_colo=44, 44, 44 有什么好方法可以做到这一点吗?如果您不使用具有某种配置支持的库,那么您必须自己完成。 为此,我建议您编写一个自己的类(例如Config.java)并在构造函数中加载整个配置(每一行由key=value组成,您可
font.setForeground(new Color(configi.getProperty("fonts_colo")));
我的config.properties
fonts_colo=44, 44, 44
有什么好方法可以做到这一点吗?如果您不使用具有某种配置支持的库,那么您必须自己完成。 为此,我建议您编写一个自己的类(例如Config.java)并在构造函数中加载整个配置(每一行由key=value组成,您可以为这一对使用一个额外的类“Attribute”,您甚至可以在其中实现解析值数据,例如:
...
if(value!=null&&key!=null&&key.equals("fonts_colo")){
String[] parts = value.split(", ");
assert(parts!=null&&parts.length == 3);
Color c = new Color(parts[0], parts[1], parts[2]);
//TODO: do something with the color
}
...
通过这种方式,您可以加载多个属性,这不需要太多的工作,并且可以重用该类。
如果你真的想使用一个库,你可以做一些基于JSON的事情,例如(即使你的配置变得更复杂,也可以使用xml)
我希望我能帮忙:)
编辑:此类类的可能实现
public class Config {
private HashMap<String, Property> properties = new HashMap<>();
public Config(File file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
for(String line; (line = br.readLine()) != null;){
if(line.contains("=")) {
String[] parts = line.split("=");
assert(parts.length==2);
properties.put(parts[0], new Property(parts[1]));
}
}
br.close();
}
public Property getProperty(String key) {
return properties.get(key);
}
/**
* Returns a color represented by the property value of the given key.
* @param key the key of the property
* @return the color, if the value had the form "r, g, b" with r,g,b as integers, else null
*/
public Color getPropertyAsColor(String key) {
Property p = getProperty(key);
if(p == null || p.valueparts == null || p.valueparts.length != 3) return null;
return new Color(Integer.parseInt(p.valueparts[0]),
Integer.parseInt(p.valueparts[1]),
Integer.parseInt(p.valueparts[2]));
}
private class Property{
private String value;
private String[] valueparts;
public Property(String value) {
if(value.contains(","))
valueparts = value.split(",");
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String[] getValues() {
if(valueparts == null)
return new String[] {value};
else return valueparts;
}
}
您有两种选择:
从属性解析输入字符串
var colFromProp = "44, 44, 44";
var rgb = Arrays.stream(colFromProp.split(","))
.map(String::trim)
.mapToInt(Integer::parseInt)
.toArray();
var c = new Color(rgb[0], rgb[1], rgb[2]);
使用另一个构造函数
您可以使用接受单个int(通常以十六进制格式给出)的构造函数,而不是获取int的列表
看
或
什么是颜色
java.awt.Color
,javafx.scene.paint.Color
,还是另一个类?import java.awt.Color代码>我更喜欢从我的config.properties使用,而不是创建另一个类,需要有方法this@Barry你到底是什么意思?Java中不支持本机属性。在您的示例中,configi显然是一个类中的对象。等一下,我给你写了一个例子。Lesiak展示了一个较短的方法,目前效果很好:)其实它是一样的,但是有函数方法,没有文件加载dzięki:P谢谢:)
var colFromProp = "44, 44, 44";
var rgb = Arrays.stream(colFromProp.split(","))
.map(String::trim)
.mapToInt(Integer::parseInt)
.toArray();
var c = new Color(rgb[0], rgb[1], rgb[2]);
var colFromProp = "0x2c2c2c";
var c = new Color(Integer.decode(colFromProp));
var colFromProp = "2c2c2c";
var c = new Color(Integer.parseInt(colFromProp, 16));