我知道这在stackoverflow之前已被质疑,但我找不到解决我的问题。 我想从ButtonClick事件中分离出一些代码,我在ButtonClick上动态创建节点并将它们添加到父AnchorPane。 在我的节点上是按钮,我的按钮上的这些事件用CommandPattern进行处理。 我创建不同的ButtonEvents,这取决于我创建的节点。
目前工作正常的代码是:
@FXML void addErosionNode(ActionEvent event){ DragNode nde = new DragNode(); /* id = nde.getId(); name = new String("Erosion"); Erosion cmd; cmd = new Erosion(); nodeList.add(new NodeList<String, String, Command>(name, id, cmd)); */ setupNode(nde); nde.setNodeWithTwoCircles(); }但是我想将代码放在另一个Method中的/ ** /内,所以我可以用setupNode(nde, name);替换代码setupNode(nde, name);
并尝试使用此方法:
public void setupNode(DragNode nde, String name){ id = nde.getId(); Class clazz; className = new String ("application.bvfunc." + type); //This will be e.g. application.bvfunc.Erosion which is the class I want to use try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { System.out.println("fail"); } clazz cmd; cmd = new clazz(); nodeList.add(new NodeList<String, String, Command>(name, id, cmd)); nde.nodeLayout(); rightAnchor.getChildren().add(nde); buildDragHandlers(); }但clazz不能解析为类型。
我该如何更换
Erosion cmd; cmd = new Erosion();用clazz创建我的节点的名称? 喜欢这个:
clazz cmd; cmd = new clazz();I know this has been questioned before on stackoverflow, but I could not find a solution for my problem. I want to separate some Code from ButtonClick event, where I dynamically create Nodes on ButtonClick and add them to a parent AnchorPane. On my Nodes are Buttons, these Events on my Button are handled with a CommandPattern. I create different ButtonEvents, depending on the Node I created.
The Code which works fine so far is:
@FXML void addErosionNode(ActionEvent event){ DragNode nde = new DragNode(); /* id = nde.getId(); name = new String("Erosion"); Erosion cmd; cmd = new Erosion(); nodeList.add(new NodeList<String, String, Command>(name, id, cmd)); */ setupNode(nde); nde.setNodeWithTwoCircles(); }But I want to put the Code between /**/ inside another Method, so I can replace the Code with setupNode(nde, name);
And try to use this method:
public void setupNode(DragNode nde, String name){ id = nde.getId(); Class clazz; className = new String ("application.bvfunc." + type); //This will be e.g. application.bvfunc.Erosion which is the class I want to use try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { System.out.println("fail"); } clazz cmd; cmd = new clazz(); nodeList.add(new NodeList<String, String, Command>(name, id, cmd)); nde.nodeLayout(); rightAnchor.getChildren().add(nde); buildDragHandlers(); }But clazz cannot be resolved to a type.
How can I replace
Erosion cmd; cmd = new Erosion();with clazz I create with the name of my Node? Like this:
clazz cmd; cmd = new clazz();最满意答案
您可以不使用反射,而是创建一个将其命令的名称映射到构造函数的映射:
private static final Map<String, Supplier<Command>> commandMap = new HashMap<>(); static { commandMap.put("Erosion", Erosion::new); commandMap.put("Dilation", Dilation::new); }然后在你的其他代码中:
public void setupNode(DragNode nde, String name){ id = nde.getId(); Command cmd = commandMap.get(name).get(); // <-- Calls the constructor nodeList.add(new NodeList<String, String, Command>(name, id, cmd)); nde.nodeLayout(); rightAnchor.getChildren().add(nde); buildDragHandlers(); }You can, instead of using reflection, create a map that maps the names of their commands to the constructors:
private static final Map<String, Supplier<Command>> commandMap = new HashMap<>(); static { commandMap.put("Erosion", Erosion::new); commandMap.put("Dilation", Dilation::new); }Then in your other code:
public void setupNode(DragNode nde, String name){ id = nde.getId(); Command cmd = commandMap.get(name).get(); // <-- Calls the constructor nodeList.add(new NodeList<String, String, Command>(name, id, cmd)); nde.nodeLayout(); rightAnchor.getChildren().add(nde); buildDragHandlers(); }更多推荐
发布评论