使用静态工厂类生成GUI组件 - 如何以及在何处添加所需的侦听器? [英] Using static factory classes to generate GUI components - How and where to add the required listeners?

查看:142
本文介绍了使用静态工厂类生成GUI组件 - 如何以及在何处添加所需的侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用工厂类和方法来生成GUI组件,但我不知道应该如何以及在哪个类中声明各种侦听器并将其添加到组件中。

I would like to use factory classes and methods to generate GUI components, but I don't know how and in which class the various listeners should be declared and added to the components.

如果我有一个简单的工厂类,如下面列出的那样,我应该在它返回到调用类之前向该按钮添加一个ActionListener。如果答案为是,那么如何添加监听器?

If I have a simple factory class such as that listed below should I add an ActionListener to the button before it is returned to the calling class. If the answer is "Yes" then how do I add the listener?

class GUIFactory
{
    public static JButton getJButton()
    {
        JButton aButton = new JButton();
        return aButton; 
    }
}

假设我想使用getJButton()方法来在GUI中添加5个按钮,我如何编写ActionListener以便知道点击了哪个按钮?

Suppose I wanted to use the getJButton() method to add 5 buttons to the GUI, how would I code the ActionListener so that it would know which button was clicked?

或者是否应该在调用类中添加监听器? / p>

Or should the listeners be added in the calling class?

JFrame gui = new JFrame();
gui.add(AppFactory.getJButton());

我试过以下

gui.add(GUIFactory.getJButton().addActionListener(new guiButtonListener()));

并收到错误:

$这里不允许使用b $ b

void类型。

"void" type not allowed here.


推荐答案

这是因为 addActionListener 返回void。尝试:

It's because addActionListener returns void. Try:

JButton button = GUIFactory.getJButton();
button.addActionListener(new guiButtonListener())
gui.add(button);

请注意,Swing GUI编程非常惯用。有些人可能更喜欢使用 Beans Binding (或其他绑定解决方案)来连接视图和模型彼此。有人会争辩说,使用事件总线可以产生最佳,低耦合,高度可重用的GUI组件。另请参阅 Swing应用程序框架(现已弃用,但 BSAF 处于非常好的状态)和 GUTS 框架。

Be aware that Swing GUI programming is quite idiomatic. Some may prefer using Beans Binding (or other binding solution) to connect views and models with each other. One would argue that using an Event Bus yield the best, lowly coupled, highly reusable GUI components. Have a look also at the Swing Application Framework (now deprecated, but the BSAF for is in very good condition) and GUTS framework.

您会看到有很多尝试解决GUI编程和设计问题。主题非常广泛,解决方案差别很大。

You'll see there are many attempts to address GUI programming and design issues. The topic is very broad, and solutions vary greatly.

哦,还有两个Swing Rich内容平台( NetBeans RCP Eclipse RCP )具有非常特定的API来处理GUI。例如,NetBeans RCP使用查找节点 Windows API,以保护开发人员免受Swing问题的影响。 Eclipse团队抛弃了Swing并编写了自己的SWT GUI工具包。如果你想要一些很棒的设计参考,你可能想看一下教程。

Oh, and the big two Swing Rich Content Platforms (NetBeans RCP, Eclipse RCP) have very specific APIs to deal with GUIs. As an example, NetBeans RCP uses Lookup, Nodes and Windows APIs to shield developer from Swing issues. The Eclipse team ditched Swing and wrote their own SWT GUI toolkit. You might want to look at tutorials if You'd wanted some great design references.

这篇关于使用静态工厂类生成GUI组件 - 如何以及在何处添加所需的侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆