Command Pattern Usefulness when using JComponents

后端 未结 2 1614
醉话见心
醉话见心 2020-12-18 09:05

So, I\'m developing a program using the Swing library and I obviously have buttons and menu items. Some of these are supposed to do the same stuff, and I thought using the C

2条回答
  •  甜味超标
    2020-12-18 09:37

    When two or more components are mean to do exactly the same thingy, one should look at Action, which reduces the duplicate code.

    Small example for further help :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ActionExample {
    
        private JFrame frame;
        private JButton button;
        private JMenuItem exitItem;
        private Action commonActions;
    
        private class CommonActions extends AbstractAction {
    
            public CommonActions(String title, String desc) {
                super(title);
                putValue(SHORT_DESCRIPTION, desc);
            }
    
            @Override
            public void actionPerformed(ActionEvent ae) {
                JOptionPane.showMessageDialog(frame,
                    "Closing Frame", "Information", JOptionPane.INFORMATION_MESSAGE);
                frame.dispose();
            }
        };
    
        private void displayGUI() {
            frame = new JFrame("Action Example");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);                
    
            commonActions = new CommonActions("Exit", "To Exit the Application");
    
            JPanel contentPane = new JPanel();
            button = new JButton();
            button.setAction(commonActions);
            contentPane.add(button);
    
            frame.setJMenuBar(getMenuBar());
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private JMenuBar getMenuBar() {
            JMenuBar menuBar = new JMenuBar();
            JMenu fileMenu = new JMenu("File");
    
            exitItem = new JMenuItem(commonActions);
            fileMenu.add(exitItem);
            menuBar.add(fileMenu);
    
            return menuBar;
        }
    
        public static void main(String[] args) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    new ActionExample().displayGUI();
                }
            };
            EventQueue.invokeLater(runnable);
        }
    }
    

    ADDED an example with SINGLETON PATTERN (though I am not sure of this approach(about how good this approach is))

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ActionExample {
    
        private JFrame frame;
        private JButton button;
        private JMenuItem exitItem;
    
        private void displayGUI() {
            frame = new JFrame("Action Example");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            CommonActions.setValues(frame);
    
            JPanel contentPane = new JPanel();
            button = new JButton();
            button.setAction(CommonActions.getInstance());
            contentPane.add(button);
    
            frame.setJMenuBar(getMenuBar());
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private JMenuBar getMenuBar() {
            JMenuBar menuBar = new JMenuBar();
            JMenu fileMenu = new JMenu("File");
    
            exitItem = new JMenuItem(CommonActions.getInstance());
            fileMenu.add(exitItem);
            menuBar.add(fileMenu);
    
            return menuBar;
        }
    
        public static void main(String[] args) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    new ActionExample().displayGUI();
                }
            };
            EventQueue.invokeLater(runnable);
        }
    }
    
    class CommonActions extends AbstractAction {
    
        private static CommonActions commonActions = null;
        private static JFrame frame = null;
    
        static {
            try {
                commonActions = new CommonActions("Exit", "To Exit the Application");
            } catch (Exception e) {
                throw new RuntimeException("BINGO, an error");
            }
        }
    
        private CommonActions(String title, String desc) {
            super(title);
            putValue(SHORT_DESCRIPTION, desc);
        }
    
        public static CommonActions getInstance() {
            return commonActions;
        }
    
        public static void setValues(JFrame f) {
            frame = f;
        }
    
        @Override
        public void actionPerformed(ActionEvent ae) {
            JOptionPane.showMessageDialog(frame,
                "Closing Frame", "Information", JOptionPane.INFORMATION_MESSAGE);
            frame.dispose();
        }
    }
    

提交回复
热议问题