Java Architecture - Question about ActionListener Conventions

前端 未结 1 1526
我寻月下人不归
我寻月下人不归 2021-01-03 15:02

I am making a user interface which shows graphs and manipulates graphs. The class extends JFrame implements ActionListener. The ActionListener then calls different classes t

1条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 15:47

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

    0 讨论(0)
提交回复
热议问题