Java 架构 - 关于 ActionListener 约定的问题 [英] Java Architecture - Question about ActionListener Conventions

查看:22
本文介绍了Java 架构 - 关于 ActionListener 约定的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a user interface which shows graphs and manipulates graphs. The class extends JFrame implements ActionListener. The ActionListener then calls different classes to manipulate graphs depending on the action. This worked while the class had few ActionListeners; however, now the class is becoming unmanageable.

我知道,为了封装的利益,最好在用户界面类中有 ActionListener,因为它需要访问界面的非静态组件.但是,封装性和可读性之间似乎存在冲突.

I know that in the interest of encapsulation, it would be best to have the ActionListener within the user interface class because it needs to access non-static components of the interface. However, it seems like there is a conflict between encapsulation and readability.

我提议将类分成一个类用于接口,第二类用于 ActionListener 并静态访问接口组件.我想知道的是这是否遵循基本的设计约定?而且,如果这是一种可接受的方法,您会将主类放在用户界面类还是 ActionListener 类中?

What I am proposing is breaking the class into one class for the interface and a second for the ActionListener and accessing the interface components statically. What I want to know is does this follow basic design conventions? And, if this is an acceptable approach would you place the main class in the user-interface class or the ActionListener class?

推荐答案

不是重复的问题...但我的回答应该帮助您解决问题.

简短的总结,我的偏好是让 JFrame 类不实现 ActionListener,然后在 JFrame 中使用一些命名内部类来实现 ActionListener.

Short summery, my preference would be to have the JFrame class not implement ActionListener and then have a number of named inner classes withing the JFrame that do implement the ActionListener.

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

I would place the main in a class unto itself... and call it Main.

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

Here is some sample code for the way I like to do it:

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

然后是图形用户界面:

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 约定的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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