English 中文(简体)
Java Architecture - Question about ActionListener Conventions
原标题:

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.

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.

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?

最佳回答

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

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.

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

and then the 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);
    }
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签