Java 架构 - 关于 ActionListener 约定的问题

标签 java swing architecture conventions

我正在制作一个显示图形和操作图形的用户界面。该类扩展了 JFrame 并实现了 ActionListener。然后,ActionListener 调用不同的类来根据操作操作图形。当类(class)只有很少的 ActionListener 时,这很有效;然而,现在类(class)变得难以管理。

我知道为了封装的目的,最好在用户界面类中包含 ActionListener,因为它需要访问界面的非静态组件。不过,封装性和可读性之间似乎存在冲突。

我建议将类分解为一个用于接口(interface)的类和第二个用于 ActionListener 的类,并静态访问接口(interface)组件。我想知道这是否遵循基本的设计惯例?而且,如果这是一种可接受的方法,您会将主类放在用户界面类还是 ActionListener 类中?

最佳答案

Not a duplicate question... but my answer should help with your question .

简单来说,我的偏好是让 JFrame 类不实现 ActionListener,然后在 JFrame 中使用许多命名内部类来实现 ActionListener。

我会将 main 放在一个类中......并将其称为 Main。

这里是一些我喜欢的示例代码:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;


public class Main
{
    private Main()
    {
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        final FooFrame frame;

        frame = new FooFrame();
        frame.setupGUI();
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

然后是 GUI:

import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


public class FooFrame
    extends JFrame
{
    private final JButton incrementBtn;
    private final JButton decrementBtn;
    private int value;

    {
        incrementBtn = new JButton("++");
        decrementBtn = new JButton("--");
    }

    private class IncrementListener
        implements ActionListener
    {

        public void actionPerformed(final ActionEvent evt)
        {
            increment();
        }

    }

    private class DecrementListener
        implements ActionListener
    {

        public void actionPerformed(final ActionEvent evt)
        {
            decrement();
        }

    }

    public void setupGUI()
    {
        final LayoutManager layout;

        layout = new FlowLayout();
        setLayout(layout);
        setupListeners();
        addComponents();
    }

    private void setupListeners()
    {
        incrementBtn.addActionListener(new IncrementListener());
        decrementBtn.addActionListener(new DecrementListener());
    }

    private void addComponents()
    {
        add(incrementBtn);
        add(decrementBtn);
    }

    private void increment()
    {
        value++;
        System.out.println("value = " + value);
    }

    private void decrement()
    {
        value--;
        System.out.println("value = " + value);
    }
}

关于Java 架构 - 关于 ActionListener 约定的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1857695/

相关文章:

c++ - 设计应用程序以支持配置更改而无需重新启动

c# - 多个类和表单的样式/架构健全性检查

asp.net-mvc - 在 ASP.NET MVC 中将实体映射到模型并执行业务逻辑

java - IntelliJ终端jdk设置

java - 如何在启用换行的情况下获取 JTextArea 中的可见行数?

java - 将现有 Swing 项目导入 NetBeans

java - 单击 MenuItem 时如何创建 jTextField

java - 优化文件搜索

java - 在 JDBC 中设置语句提取大小或使用 LIMIT 子句触发 SQL 查询有什么区别?

java - imgur 的 "authorization callback url?"是什么,我的应该是什么?