大家好我有一个让我很困惑的问题,我有'writeMethod'这是一种Method类,'dpv'是一种propertyDescriptor,我通过getWriteMethod()检索了对象的writeMethod ,现在我的问题是如何设置'writeMethod'来写一个对象的属性(例如JLabel,JButton)这里是我的代码:
if(dpv.getPropertyType().isPrimitive()
|| dpv.getPropertyType().isInstance("Integer") )
{
Method writeMethod = dpv.getWriteMethod();
//setWriteMethod(writeMethod);<---------- Not sure about this part (doesn't work)
System.out.println(writeMethod);
PropertyValue.setEnabled(true);
SetButton.setEnabled(true);
}
else{
PropertyValue.setEnabled(false);
SetButton.setEnabled(false);
}
感谢您的帮助
答案 0 :(得分:1)
要使用该方法编写属性,您必须调用它。简单属性采用单个值 - 属性的值,因此您使用单个参数调用该方法。以下代码将按钮上的属性设置为值42:
Method writeMethod = dpv.getWriteMethod();
JButton button = ...; // the target to write to
try
{
writeMethod.invoke(button, 42);
}
catch (IllegalAccessException ex)
{
// handle these as appropriate
}
catch (IllegalArgumentException ex)
{
}
catch (InvocationTargetException ex)
{
}
你不太可能拥有它们,但如果属性是很少使用的索引属性类型,那么你需要使用这样的方法:
writeMethod.invoke(target, index, propertyValue);
这对应于setter方法
setIndexProperty(int index, PropertyType value);