简体   繁体   English

不同类中的Java ActionEvent

[英]Java ActionEvent in Different Class

been trying to solve this problem for over a day now, and throwing in the white flag now. 一直在努力解决这一问题超过一天,现在抛出白旗。 Taking this class at UMUC, and it's pretty much a self-study curriculum without any help so I really appreciate being able to ask this question here. 在UMUC上这堂课,这几乎是一门自学式课程,没有任何帮助,所以我非常感谢能够在这里提出这个问题。

Just going to ask this question conceptually, because I can't even get my head wrapped around the concept. 只是从概念上问这个问题,因为我什至无法理解这个概念。

I have a GUI class (subclass of JPanel) that creates a button. 我有一个创建按钮的GUI类(JPanel的子类)。 In the GUI class, the button uses ActionListener to recognize when it is clicked and performs validation tests on a textfield. 在GUI类中,按钮使用ActionListener来识别单击它的时间,并在文本字段上执行验证测试。 So far so good! 到现在为止还挺好!

Now, after the validation tests -- which ensure the input in textfield is numeric, I want to use this input to add to a variable in a different class (called Account). 现在,在进行验证测试(确保文本字段中的输入为数字)之后,我想使用此输入将其添加到其他类(称为Account)中的变量。

In the third class, which includes main method -- I have created two instances of Account class: checking and saving, as well as Frame and adding GUI to the frame. 在包含主要方法的第三个类中,我创建了Account类的两个实例:检查并保存,以及Frame和向该框架添加GUI。

Problem: (1) How do I trigger the add method in account class when button in GUI class is clicked? 问题:(1)当单击GUI类中的按钮时,如何触发帐户类中的add方法? (2) How do I ensure it applies to the specific instance of the Account class, ie, either checking or saving? (2)如何确保它适用于Account类的特定实例,即检查还是保存?

There are a number of ways you might be able to do this. 您可能有多种方法可以执行此操作。 One would be to provide a ActionListener property to your JPanel , which you would then trigger once you've validated the input from the button. 一种方法是为JPanel提供一个ActionListener属性,然后在您验证了按钮的输入后将触发该属性。 This a basic observer pattern (and you're already using it on the JButton ). 这是一个基本的观察者模式(并且您已经在JButton上使用了它)。

The problem is then how to get the information from the panel. 然后的问题是如何从面板中获取信息。 You could provide getters on the panel, but this begins to tighten the coupling in your code. 您可以在面板上提供吸气剂,但这开始加强代码中的耦合。

A slightly better solution might be to provide your own listener/observer interface which you could then pass the information you want from the GUI to the listener, further de-coupling the API 更好的解决方案可能是提供您自己的侦听器/观察器接口,然后可以将所需的信息从GUI传递给侦听器,从而进一步取消API的耦合

I'd avoid passing the Account to the GUI if possible, unless it has some reason to actually use/modify the account, it's best to keep it decoupled of the responsibility, the GUI's responsibility is to get and validate the information as best as possible and pass the processing on to the observer/listener. 我将尽可能避免将Account传递给GUI,除非有实际使用/修改帐户的理由,否则最好使其脱离责任,GUI的责任是尽可能获取并验证信息。并将处理传递给观察者/听众。

In this case, you just need to wrap the listener/observer around a particular instance of the account, so when it's triggered, it's operating on the correct account 在这种情况下,您只需要将侦听器/观察者包装在帐户的特定实例周围,因此在触发侦听器/观察者时,它将在正确的帐户上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM