【发布时间】:2019-01-25 08:27:56
【问题描述】:
我有一个名为 menuBarObject 的对象。它的类隐藏了一个名为“changed”的布尔值。如果单击子菜单,则返回 true。 在我的 Mainclass 中,我构建了 menuBarObject。我可以通过一种名为“hasChanged”的方法获取更改的值。如果主方法完成了它的工作,它将所有一个名为“contentIsBuild”的方法再次设置为 false。 如果更改返回 true,我可以编写一个活跃的侦听器吗?我可以在 menuBar 类中做到这一点。但我不喜欢那个解决方案。
我的代码放入那个监听器:
if (newCustomerIsSelected) {
//Content Area
JPanel contentArea = new JPanel();
//Selection from Sub Menu
String selectedName = menuBarObject.getSelectedCustomer();
//Selected Customer Object / Instanze of selectecCustomer Class!
SelectedCustomer selectedCustomerObject = new SelectedCustomer(selectedName);
//The seleted Customer
Customer selectedCustomer = selectedCustomerObject.getSelectedCustomer();
//Name of selected Customer
String dataOfSelectedCustomer = selectedCustomer.toString();
//Creating the content Area
setCustomerContentArea(dataOfSelectedCustomer, contentArea);
//Call Menu Bar that content is build
menuBarObject.contentIsBuild(true);
}
【问题讨论】:
-
你为什么要这样做?只需将其添加到单击项目时处理操作的侦听器中
-
在 menuBar 类中设置已更改。但是菜单栏不应该负责构建内容。会更容易,我知道。 ^^
-
创建自己的线程,在其上使用 while(!changed) 循环,不断检查“已更改”布尔变量的值
-
我建议您看看
ChangeListener,它已经在 API 中可用 - for example -
“点击了子菜单”是什么意思?是当单击一个按钮时会产生一个子菜单或一个窗口,任何单击它都会将
changed字段设置为true?还是别的什么?