简体   繁体   English

install4j:更改标签颜色

[英]install4j: Change label color

I have three Run executable or batch file actions that invokes my launch scripts, tails into the log files respectively, and verifies that each service has started entirely. 我有三个Run executable or batch file操作,分别调用我的启动脚本,分别拖入日志文件并验证每个服务是否已完全启动。 I would have used the native launch action in install4j but these services need to start in order - synchronously - and take up to 30 seconds per service. 我本可以在install4j中使用本机启动操作,但是这些服务需要按顺序启动-同步-每个服务最多需要30秒。

After each action, I have a Run script action that verifies the return code and determines whether the rest of the scripts should execute as well. 在执行每个操作之后,我将执行一个Run script操作,该操作将验证返回代码并确定是否还应执行其余脚本。 On the form itself, it shows the installed components and I would like to add an icon and change the color of each label after it successfully launches. 在表单本身上,它显示了已安装的组件,我想在成功启动后添加一个图标并更改每个标签的颜色。

If this has confused you, please see the screen shot below to get a better under standing. 如果您对此感到困惑,请查看下面的屏幕截图,以更好地了解自己。

屏幕截图install4j

And as always, thanks for your support. 和往常一样,感谢您的支持。

Chris 克里斯

The trick is how to get the form environment of a screen from a "Run script" action. 诀窍是如何从“运行脚本”操作获取屏幕的表单环境。 A screen that can contain form components is an instance of com.install4j.api.screens.FormPanelContainer and that class provides access to the com.install4j.api.formcomponents.FormEnvironment . 可以包含表单组件的屏幕是com.install4j.api.screens.FormPanelContainer一个实例,该类提供对com.install4j.api.formcomponents.FormEnvironment访问。

In a "Run script" action, you can do this: 在“运行脚本”操作中,您可以执行以下操作:

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;

with the appropriate values for "screenId" and "componentId". 并为“ screenId”和“ componentId”使用适当的值。

for a more reusable solution add 为了获得更可重用的解决方案,请添加

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

to the static code on the "Installer->Custom Code & resources" step (install4j 6+) and call 到“安装程序->自定义代码和资源”步骤(install4j 6+)上的静态代码,然后调用

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

in your "Run script" actions. 在“运行脚本”操作中。


For setting an icon , you have to define the "Icon" property on one label component and use the "Initialization script" property to save it in the context and remove it from the label: 对于设置图标 ,您必须在一个标签组件上定义“ Icon”属性,并使用“ Initialization script”属性将其保存在上下文中并从标签中删除:

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

Then after the call to label.setForeground(); 然后在调用label.setForeground(); in the above code snippets, you can call 在以上代码段中,您可以调用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM