诀窍是如何从“运行脚本”操作中获取屏幕的表单环境。可以包含表单组件的屏幕是com.install4j.api.screens.FormPanelContainer 的一个实例,该类提供对com.install4j.api.formcomponents.FormEnvironment 的访问。
在“运行脚本”操作中,您可以这样做:
import java.awt.EventQueue;
import java.awt.Color;
EventQueue.invokeLater(new Runnable() {
public void run() {
FormEnvironment formEnvironment =
((FormPanelContainer)context.getScreenById("screenId")).getFormEnvironment();
JComponent label = (JComponent)formEnvironment.getFormComponentById("componentId").
getConfigurationObject();
label.setForeground(Color.MAGENTA);
}
});
return true;
为“screenId”和“componentId”设置适当的值。
添加更可重用的解决方案
import java.awt.EventQueue;
import java.awt.Color;
public static void changeColor(final String screenId, final String componentId,
final Color color, final Context context)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
FormEnvironment formEnvironment =
((FormPanelContainer)context.getScreenById(screenId)).getFormEnvironment();
JComponent label = (JComponent)formEnvironment.getFormComponentById(componentId).
getConfigurationObject();
label.setForeground(color);
}
});
}
到“安装程序->自定义代码和资源”步骤(install4j 6+)上的静态代码并调用
changeColor("screenId", "componentId", java.awt.Color.GREEN, context);
在您的“运行脚本”操作中。
要设置图标,您必须在一个标签组件上定义“图标”属性,并使用“初始化脚本”属性将其保存在上下文中并从标签中删除:
context.setVariable("checkIcon", configurationObject.getIcon());
configurationObject.setIcon(null);
那么在上面的代码sn-ps中调用label.setForeground();之后,就可以调用了
label.setIcon((Icon)context.getVariable("checkIcon"));