处理MVC模型,jbuttons和ActionListener的getSource()方法 [英] Dealing with MVC model, jbuttons and ActionListener's getSource() method
问题描述
所以我在使用MVC编写程序时偶然发现了这个问题.
我在View
类中有一个私有 JButton
.我编写了将侦听器添加到所有相应按钮的方法.但是,当我尝试对ActionPerformed()
部分进行编码时,会引发关于JButton
不可见的错误.
So i just stumbled upon this problem while coding my program using MVC.
I have a private JButton
in the View
class. I wrote the method to add the listener to all respective buttons. However, when i'm trying to code the ActionPerformed()
part it throws an error about JButton
not being visible.
将JButton
设置为公开可以完全解决问题,但这是正确的做法吗?是否有另一种设置ActionListener
而不公开JButton
的方法?
Setting JButton
to public solves the problem completly, but is it the right thing to do? Is there another way of setting the ActionListener
without making the JButton
public?
public class learningView extends JFrame {
private JButton viewButton = new JButton("View Resources");
public void addButtonListener(ActionListener listenerForButtons) {
viewButton.addActionListener(listenerForButtons);
saveButton.addActionListener(listenerForButtons);
addButton.addActionListener(listenerForButtons);
}
}
public class learningController {
private learningModel theModel;
private learningView theView;
public learningController(learningModel theModel, learningView theView) {
this.theModel = theModel;
this.theView = theView;
this.theView.addButtonListener(new buttonListener());
}
class buttonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == theView.viewButton) {// This is where problem arises
}
}
}
}
用于视图和控制器类的Hastebin(无模型),以方便使用. http://www.hastebin.com/ecawolusal.avrasm
Hastebin for view and controller classes (without model) for the convienience. http://www.hastebin.com/ecawolusal.avrasm
推荐答案
由于viewButton
具有对LearningVew
的私有访问权限,因此只能从该类上下文中访问它.
Since viewButton
has private access to LearningVew
, it will simply be inaccessible out of that classes context.
现在,在更改访问级别之前,您可以考虑更改方法.
Now, before you change the access level, you might consider changing your approach.
与其在每个按钮上添加ActionListener
来通知外部消息源,不如让视图来监视按钮本身可能更简单.
Rather then adding an ActionListener
to each button which notifies an external source, it might be simpler to have the view to monitor the buttons itself.
在深入探讨如何破坏MVC之前,我们的想法是让视图为所涉及的按钮引发一个更简单,更专用的事件,例如,viewButton
可以引发viewWasActivated
等等,然后控制器将响应.
Before you get up in arms over how this would break the MVC, the idea would be to then have the view raise a simpler, more dedicated event for the button in question, for example, viewButton
could raise viewWasActivated
or something, to which the controller would then respond.
这将需要您为视图和控制器定义一个interface
合同,以便他们知道他们能够相互传递的信息以及可能触发的事件.这样可以保护视图控件,并且意味着您不需要暴露不必要的内容.
This would require you to define a interface
contract for both the view and the controller so they knew what information they were capable of passing to each other and what events might be triggered. This protects the view controls and means you don't need to expose the unnecessarily.
详细说明了这里.
另一种选择是使用按钮的actionCommand
属性,而不是将按钮的引用与事件源进行比较,但是您首先需要检查动作事件的源是按钮...我个人不喜欢批量" ActionListener
,它们很快就会变得凌乱...
The other choice would be to use the actionCommand
property of the buttons instead of comparing references of the buttons to the event source, but you would first need to check that the source of the action event was a button ... and I personally don't like "bulk" ActionListener
s, they get messy real quickly...
这篇关于处理MVC模型,jbuttons和ActionListener的getSource()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!