4

我需要在尝试组织 gui 小部件的 JavaFX 应用程序中初始化以下私有成员,但是我不知道正确的语法,请有人告诉我正确的语法:

这是我用于 EnumMap 的枚举

enum Connection {
    Connection1,
    Connection2,
    Connection3,
    Connection4;
}

这是我尝试使用此地图或基于服务名称键的EnumMaps组织的小部件的选择,因此以下 4 个复选框和标签的列表属于一起,(我有类似的 JavaFX 小部件用于服务 2 等等。

@FXML
private CheckBox mService1CheckBox1;
@FXML
private CheckBox mService1CheckBox2;
@FXML
private CheckBox mService1CheckBox3;
@FXML
private CheckBox mService1CheckBox4;
@FXML
private Label mService1Label1;
@FXML
private Label mService1Label1;
@FXML
private Label mService1Label1;
@FXML
private Label mService1Label1;

这是我尝试使用 JavaFX 小部件初始化的私有成员

private Map<String, EnumMap<Connection, AbstractMap.SimpleEntry<Label,
CheckBox>>> mWidgetInfo;

我可以使用以下方法初始化顶级空 mServiceWidgetMap:

mWidgetInfo= new HashMap<>();

而且我知道我需要将 EnumMaps 初始化为 new EnumMap<>(Connection.class); 但我还需要将成对的小部件放在这些 EnumMaps 的值大小中,我对如何做到这一点感到困惑。

但是我不知道如何初始化 enumMap 值对。非常感谢语法帮助。

编辑 经过一段时间的努力,我想出了以下方法,但肯定必须有一种更简单的方法,比如双括号初始化或其他一些不太冗长的方法。

private void initializeServiceHeartbeatTab() {
    // @JC Todo - dynamically create base on CSV rows
    // Map<String, EnumMap<Connection, SimpleEntry<Label, CheckBox>>>
    EnumMap<Connection, SimpleEntry<Label, CheckBox>> service1Info =
        new EnumMap<>(Connection.class);
    SimpleEntry<Label, CheckBox> pair1 = new SimpleEntry<>(
        mService1Label1, mService1CheckBox1);
    SimpleEntry<Label, CheckBox> pair2 = new SimpleEntry<>(
        mService1Label2, mService1CheckBox2);
    SimpleEntry<Label, CheckBox> pair3 = new SimpleEntry<>(
        mService1Label3, mService1CheckBox3);
    SimpleEntry<Label, CheckBox> pair4 = new SimpleEntry<>(
        mService1Label4, mService1CheckBox4);
    service1Info.put(Connection.Connection1, pair1);
    service1Info.put(Connection.Connection1, pair2);
    service1Info.put(Connection.Connection1, pair3);
    service1Info.put(Connection.Connection1, pair4);

    EnumMap<Connection, SimpleEntry<Label, CheckBox>> service2Info = 
        new EnumMap<>(Connection.class);
    pair1 = new SimpleEntry<>(mService2Label1, mService2CheckBox1);
    pair2 = new SimpleEntry<>(mService2Label2, mService2CheckBox2);
    pair3 = new SimpleEntry<>(mService2Label3, mService2CheckBox3);
    pair4 = new SimpleEntry<>(mService2Label4, mService2CheckBox4);
    service2Info.put(Connection.Connection1, pair1);
    service2Info.put(Connection.Connection1, pair2);
    service2Info.put(Connection.Connection1, pair3);
    service2Info.put(Connection.Connection1, pair4);

    EnumMap<Connection, SimpleEntry<Label, CheckBox>> service2Info = 
        new EnumMap<>(Connection.class);
    pair1 = new SimpleEntry<>(mService3Label1, mService3CheckBox1);
    pair2 = new SimpleEntry<>(mService3Label2, mService3CheckBox2);
    pair3 = new SimpleEntry<>(mService3Label3, mService3CheckBox3);
    pair4 = new SimpleEntry<>(mService3Label4, mService3CheckBox4);
    service3Info.put(Connection.Connection1, pair1);
    service3Info.put(Connection.Connection1, pair2);
    service3Info.put(Connection.Connection1, pair3);
    service3Info.put(Connection.Connection1, pair4);

    mWidgetInfo = new HashMap<>();
    mWidgetInfo.put("albf", service1Info);
    mWidgetInfo.put("fms1", service2Info);
    mWidgetInfo.put("fms2", service3Info);
}
4

1 回答 1

3

我终于弄清楚了如何初始化上面的 Map。在没有所有那些不必要的临时变量的情况下初始化它的技巧是使用嵌套的双括号初始化,如下所示:

// Map<String, EnumMap<Connection, SimpleEntry<Label, CheckBox>>>
mWidgetInfo = new HashMap() {{
    put("service1", new EnumMap(Connection.class) {{
        put(Connection.Connection1, new SimpleEntry<>(mService1Label1, mService1CheckBox1));
        put(Connection.Connection2, new SimpleEntry<>(mService1Label2, mService1CheckBox2));
        put(Connection.Connection3, new SimpleEntry<>(mService1Label3, mService1CheckBox3));
        put(Connection.Connection4, new SimpleEntry<>(mService1Label4, mService1CheckBox4));
    }});
    put("service2", new EnumMap(Connection.class) {{
        put(Connection.Connection1, new SimpleEntry<>(mService2Label1, mService2CheckBox1));
        put(Connection.Connection2, new SimpleEntry<>(mService2Label2, mService2CheckBox2));
        put(Connection.Connection3, new SimpleEntry<>(mService2Label3, mService2CheckBox3));
        put(Connection.Connection4, new SimpleEntry<>(mService2Label4, mService2CheckBox4));
    }});
    put("service3", new EnumMap(Connection.class) {{
        put(Connection.Connection1, new SimpleEntry<>(mService3Label1, mService3CheckBox1));
        put(Connection.Connection2, new SimpleEntry<>(mService3Label2, mService3CheckBox2));
        put(Connection.Connection3, new SimpleEntry<>(mService3Label3, mService3CheckBox3));
        put(Connection.Connection4, new SimpleEntry<>(mService3Label4, mService3CheckBox4));
    }});
}};

然后稍后我可以遍历集合中的所有小部件 - 例如初始化工具提示帮助并设置默认标签,如下所示:

// initialize the tooltips & default labels
mWidgetInfo.forEach((k, v)-> {
    // no need to switch on the service
    // disable click events on the checkboxes
    // as they should be read only
    v.forEach((k1, widgetPair)-> {
        widgetPair.getKey().setTooltip(
            new Tooltip("Connect ID"));
        CheckBox checkBox = widgetPair.getValue();
        checkBox.setTooltip(new Tooltip("Servicing Connection"));
        checkBox.setOpacity(1);
    });
});
于 2016-06-15T18:56:02.340 回答