使用静态工厂类生成 GUI 组件 - 如何以及在何处添加所需的侦听器?

发布于 2024-09-16 00:30:52 字数 700 浏览 1 评论 0 原文

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

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

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

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

或者应该将侦听器添加到调用类中?

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

我已尝试以下操作

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

并收到错误:

此处不允许使用“void”类型。

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.

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; 
    }
}

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?

Or should the listeners be added in the calling class?

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

I've tried the following

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

and got an error:

"void" type not allowed here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萤火眠眠 2024-09-23 00:30:52

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

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

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

您将看到有许多尝试来解决 GUI 编程和设计问题。该主题非常广泛,解决方案也多种多样。

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

It's because addActionListener returns void. Try:

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

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.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文