install4j:更改标签颜色

install4j: Change label color

我有三个 Run executable or batch file 操作调用我的启动脚本,分别进入日志文件,并验证每个服务是否已完全启动。我会在 install4j 中使用本机启动操作,但这些服务需要按顺序启动 - 同步 - 每个服务最多需要 30 秒。

在每个动作之后,我有一个 Run script 动作来验证 return 代码并确定是否也应执行其余脚本。在表单本身上,它显示了已安装的组件,我想添加一个图标并在成功启动后更改每个标签的颜色。

如果这让您感到困惑,请查看下面的屏幕截图以更好地理解。

一如既往,感谢您的支持。

克里斯

诀窍是如何从 "Run script" 操作中获取屏幕的表单环境。可以包含表单组件的屏幕是 com.install4j.api.screens.FormPanelContainer 的实例,class 提供对 com.install4j.api.formcomponents.FormEnvironment.

的访问

在 "Run script" 动作中,您可以这样做:

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);
        }
    });
}

到"Installer->Custom Code & resources"步骤的静态代码(install4j 6+)并调用

changeColor("screenId", "componentId", java.awt.Color.GREEN, context);

在您的 "Run script" 操作中。


要设置图标,您必须在一个标签组件上定义"Icon" 属性 并使用"Initialization script" 属性 将其保存在上下文中并将其从标签中删除:

context.setVariable("checkIcon", configurationObject.getIcon());
configurationObject.setIcon(null);

那么在上面的代码片段中调用label.setForeground();之后,就可以调用

    label.setIcon((Icon)context.getVariable("checkIcon"));