简体   繁体   English

Java Swing:如何处理子类中发生的事件

[英]Java swing: How to handle events that occur in subclass

I want to build a simple calculator and have this simple construction: 我想构建一个简单的计算器,并具有以下简单的结构:

CalculatorGUI <>----- ButtonPanel <>------ JButton

Where Calculator has a JFrame and the ButtonPanel (extends JPanel). 其中Calculator具有JFrame和ButtonPanel(扩展了JPanel)。 The ButtonPanel has JButtons in it. ButtonPanel中具有JButton。

I want to have a way to pass the JButton events trough ButtonPanel such that I can catch the events in CalculatorGUI. 我希望有一种方法可以通过ButtonPanel传递JButton事件,以便可以在CalculatorGUI中捕获事件。 I was thinking about passing events trough ButtonPanel but I do not know how to do such a thing. 我当时正在考虑通过ButtonPanel传递事件,但我不知道该怎么做。 Is there another way to do this? 还有另一种方法吗?

Thanks in advance, 提前致谢,

You can use an interface to make that happen. 您可以使用界面来实现这一目标。 Create that interface in your button panel and implement that interfaceListener in your calculator GUI. 在按钮面板中创建该接口,并在计算器GUI中实现该interfaceListener。 Declare your method names in interface your calculator GUI will have to override them. 在计算器GUI的接口中声明您的方法名称必须覆盖它们。 Then in your button's event use that interface to let the GUI know about it 然后在按钮事件中,使用该界面让GUI知道它

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

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