Netbeans 6.8:动作监听器混乱
我有 2 个模块:BrowserLibrary 和 BrowserViewer。
浏览器像任何浏览器一样包含 gui。
所以在 BrowserLibrary 模块中我有一个像这样的类:
public class BrowserController implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (e.getSource().getVarName() == "navButton")
System.out.println("clicked");
}
}
在 BrowserViewer 模块的 BrowserTopComponent.java 中:
private javax.swing.JButton navButton;
navButton.addActionListener(BrowserController);
我不确定这是否是我应该追求的正确设计。 BrowserLibrary 是否应该仅用于加载第 3 方 JAR,而不用于加载其他内容?如果是这种情况,我最终会得到 3 个模块:BrowserLibrary、Browser、BrowserViewer 吗?在这种情况下,依赖关系将如何工作? Browser 是否依赖于 BrowserLibrary,而 BrowserViewer 是否依赖于 Browser ?
那么在 Browser 模块中,我会有 BrowserController 类来实现 ActionListener 吗?
另外,如何获取 e.getSource() 对象的变量名?在本例中,“navButton”是 JButton 的变量名称。
我应该使用 Lookups 吗?如何将其合并到这里?
I have 2 modules: BrowserLibrary and BrowserViewer.
Browser contains gui like any browser should.
so inside BrowserLibrary module I have a class like so:
public class BrowserController implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (e.getSource().getVarName() == "navButton")
System.out.println("clicked");
}
}
and in BrowserViewer module's BrowserTopComponent.java:
private javax.swing.JButton navButton;
navButton.addActionListener(BrowserController);
I am not sure if this is the right design I should be pursuing. Should BrowserLibrary only be used to load the 3rd party JARs and nothing else ? IF that is the case would I end up with 3 modules: BrowserLibrary, Browser, BrowserViewer ? How would the dependencies work in this case ? Does Browser depend on BrowserLibrary, and BrowserViewer depend on Browser ?
So inside Browser module, I would have BrowserController class which implements ActionListener ?
Also how can I get the variable name of the e.getSource() object ? in this case "navButton" is the variable name of the JButton.
Should I be using Lookups ? How would that be incorporated here ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模块的数量和组成应反映设计的模块化程度。一个很好的问题是“这个模块可以在没有其他模块的情况下重用吗?”如果答案是否定的,那么相关模块是紧密耦合的,并且可能应该只是一个模块。例如,处理 HTTP 协议的模块可以独立于浏览器使用。在浏览器中导航的操作可能相当以浏览器为中心,并且可能无法单独使用。
变量是运行时实例,但变量名称实际上是程序员用来引用变量的编译时工件。也就是说,所有 swing 组件都有一个可用于识别的名称属性。请注意,开发人员有责任确保唯一性。
The number and composition of modules should reflect the modularity of your design. A good question to ask is "Can this module be reused without the others?" If the answer is no, then the modules in question are tightly coupled and should probably only be one module. For example, a module that handles the HTTP protocol could be used independently of the browser. The actions for navigating in a browser are probably rather browser centric and probably cannot be used by themselves.
Variables are run time instances, but variable names are really compile-time artifacts that programmers use to refer to a variable. That said, all swing components do have a name property that can be used for identification. Do note that it is the developer;s responsibility to ensure uniqueness.