How do I organize my Actions in Swing?

后端 未结 3 1442
旧时难觅i
旧时难觅i 2020-12-06 14:39

I am currently replacing my anonymous ActionListeners

new ActionListener() {
    @Override
    public void actionPerformed(final ActionEvent event) {
                


        
3条回答
  •  囚心锁ツ
    2020-12-06 15:13

    You can keep your actions in a separate package to isolate them. Sometimes, it is useful to keep them in one class, especially if actions are related or have a common parent, for example:

    public class SomeActions {
        static class SomeActionX extends AbstractAction {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        }
    
        static class SomeActionY extends AbstractAction {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        }
    
        static class SomeActionZ extends AbstractAction {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        }
    }
    

    Then to access them:

    JButton button = new JButton();
    button.setAction(new SomeActions.SomeActionX());
    

提交回复
热议问题