我需要在我尝试组织gui小部件的JavaFX应用程序中初始化以下私有成员,但是我不知道正确的语法,有人可以告诉我正确的语法:
这是我用于EnumMap的枚举
enum Connection {
Connection1,
Connection2,
Connection3,
Connection4;
}
以下是我尝试使用此地图组织的小部件选择或基于服务名称密钥的 EnumMaps ,因此以下列表中的4个复选框和标签属于一起,(我有用于服务2的类似JavaFX小部件等等。
@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;
我可以使用:
初始化顶级空mServiceWidgetMapmWidgetInfo= new HashMap<>();
我知道我需要将EnumMaps初始化为新的EnumMap&lt;&gt;(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);
}
答案 0 :(得分:1)
我终于想出了如何初始化上面的Map。在没有所有那些不必要的临时值的情况下初始化它的技巧是使用嵌套的double brace initialization,如下所示:
Failed to read artifact descriptor for org.glassfish.tyrus:tyrus-container-grizzly-server:jar:1.4: Could not transfer artifact org.glassfish.tyrus:tyrus-container-grizzly-server:pom:1.4 from/to central (http://repo1.maven.org/maven2): repo1.maven.org: unknown error: Unknown host repo1.maven.org: unknown error -> [Help 1]
然后我可以迭代集合中的所有小部件 - 例如初始化工具提示帮助并设置默认标签,如下所示:
// 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));
}});
}};