将组件分成类 [英] Separating components into classes

查看:33
本文介绍了将组件分成类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个GUI"应用程序,目的是使代码清晰,并将这些GUI"组件分成类(您将在下面看到我的意思),现在这与菜单栏很相配,但我现在想知道,因为我想为他们实现 ActionListener,最好在哪个类中执行它,例如在菜单栏类的这种情况下,在他的类中或在 Main 中实现动作侦听器而不是实现将使用我们想要执行操作的方法的抽象方法?

I'm creating a "GUI" application, with aim to make a code clear and by separating those "GUI" components into classes (you'll see below what I mean), which for now goes well with menu bar, but I'm wonder now, since I want to implement ActionListener to them, where it is best to do it in which class, for example in this case of menu bar class, in his class or to implement action listener in Main and than implement abstract method which will use methods we want to perform action?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;

public final class Main extends JFrame {

    private MenuBar menuBar;

    public Main() {
        setTitle("GUI");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        initGUI();
    }

    public void initGUI() {
        menuBar = new MenuBar(this);
        // this is last update before I started posting this, I decided to
        // access exitMenu variable and than to addActionListener to it from a here, which is currently not working
        menuBar.exitMenu.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        new Main();
    }

}

菜单栏类:

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

public final class MenuBar {

    public JMenu newMenu;
    public JMenu aboutMeMenu;
    public JMenu exitMenu;

    public MenuBar(JFrame jFrame) {
        JMenuBar menubar = new JMenuBar();

        initNew(menubar);
        initAboutMe(menubar);
        initExit(menubar);

        jFrame.setJMenuBar(menubar);
    }

    public void initNew(JMenuBar menubar) {
        newMenu = new JMenu("New");  
        menubar.add(newMenu);
    }

    public void initAboutMe(JMenuBar menubar) {
        aboutMeMenu = new JMenu("About Me");  
        menubar.add(aboutMeMenu);
    }

    public void initExit(JMenuBar menubar) {
        exitMenu = new JMenu("Exit");
        menubar.add(exitMenu);
    }


}

推荐答案

一种标准方法是让 Main 类或与 Main 相当接近"的某个类处理所有或大部分操作.通常会有一个基于 action.getActionCommand() 的巨大 switch 语句,它将控制权传递给适当的函数.我个人不喜欢这种方法,但它确实有一个优点,即遵循起来相当简单.

One standard approach is to have your Main class, or some class fairly "close" to Main, handle all or most of the actions. There will typically be a giant switch statement based upon action.getActionCommand() that passes control to the appropriate function. I for one do not like this approach, but it does have the advantage that it's fairly simple to follow.

IMO 更优越的方法是创建 AbstractActions 并连接到它们.它们可以以对您的应用程序有意义的任何方式进行组织.好的是它们被很好地封装,感觉更类似 OO"等等.此外,多个 GUI 元素可以共享一个动作.例如按钮和菜单都可以调用SaveFileAction"操作.并且可以集中启用和禁用操作.如果没有要保存的文件,则可以很容易地禁用SaveFileAction".

IMO the far superior approach is to create AbstractActions and hook up to them. They can be organized in whatever way makes sense for your application. What's nice is that they are nicely encapsulated, feel much more "OO-like", etc.... Also, multiple GUI elements can share an action. e.g. both a button and a menu can invoke the "SaveFileAction" action. And actions can be centrally enabled and disabled. If there is no file to save, that "SaveFileAction" can be fairly easily disabled.

这篇关于将组件分成类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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