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

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

问题描述

我想提出一个用户界面,显示图形和操纵图形。该类扩展JFrame中实现的ActionListener。该ActionListener的然后调用不同的类来操作取决于操作图。这个工作而类有少数的ActionListeners;然而,现在类变得难以管理。

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?

推荐答案

不是一个重复的问题...但我的答案应该你的问题帮助。

短的夏日,我的preference将是有JFrame类没有实现ActionListener,然后有一个数量的命名内部类withing那些实现ActionListener JFrame中。

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.

我将使主要的一类本身就是......并把它称为主。

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

下面是一些示例code,因为我喜欢做的方式:

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

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

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