【发布时间】:2018-07-25 09:26:41
【问题描述】:
我创建了一个扩展 JavaFX 的 MenuBar 的类,它为我的应用程序创建了一个菜单栏。
默认情况下,我不会禁用专门的操作,例如打开/保存文件和运行模拟(它们是)。当用户运行应用程序时,他们可以在菜单 File>New> 中选择一个项目,并根据他们选择的组件来切换相应的菜单选项。
我打算这样做,让每个组件给出一个列表,列出它打开的项目,然后在创建组件时激活相应的项目。
但是,我无法从函数内部访问菜单列表(我尝试使用 this.getMenus() 进行访问,但在函数内部是唯一可以识别它的函数 this.getClass())。
有谁知道我为什么不能打电话给getMenus() 以及我怎样才能访问它?
或者,如果您对如何切换这些菜单项有更好的想法,我很想听听。我认为这不是一个好方法,但这是我们想出的最好的主意。
private void fileNew()
{
Menu fileNew = new Menu("New");
menuFile.getItems().add(fileNew);
for(String k: CLHM.keySet())
{
CComponent comp = CLHM.get(k);
if(comp.supportedFeatures().contains((new SupportsNew())))
{
MenuItem i = new MenuItem(comp.getName());
fileNew.getItems().add(i);
i.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
CComponent ctemp = CLHM.get(i.getText());
ArrayList<String> menuItems = (ArrayList) ctemp.getMenuItems();
for (String s : menuItems)
{
Scanner scanner = new Scanner(s).useDelimiter("\\s>\\s");
String menu = scanner.next();
//Menu temp = this.getMenus();
/*
Here the program will parse the string of the
Menu path (e.g. File>Open) and activate the
relevant item, if it exists.
*/
}
borderPane.setCenter((Node) ctemp);
}
});
}
}
}
【问题讨论】:
-
menuFile.getItems()是做什么的? -
非常次优的方法!走干净的路径,将逻辑分离到模型中,模型根据任何条件决定允许哪些操作,然后将 menuItems'disabled 绑定到模型的适当属性
-
@Sedrick 它在菜单栏中的文件选项下添加项目。因为文件的命名空间已经很混乱,我不得不选择奇怪的名字。
-
@kleopatra 你能解释一下这是什么意思吗?