命令模式使用JComponents时的有用性 [英] Command Pattern Usefulness when using JComponents

查看:144
本文介绍了命令模式使用JComponents时的有用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在使用Swing库开发一个程序,我显然有按钮和菜单项。其中一些应该做同样的事情,我认为使用命令模式应该是这样做的,例如。我有一个保存按钮和保存菜单项,他们必须实现相同的保存算法。
命令模式似乎是可以的,但是我无法得到谁的接收者。不是一个comand应该在一个实现某种接收器接口的对象上工作,这样你可以在不同的接收器上使用不同的命令来将它们连接起来呢?在我的实现模式中看起来好像没有接收者。
另外一个疑问是,一个命令是以一个单例来实现的,因为你可以从同一个项目的不同部分调用它的功能,而且只需要一次实例化一次,并使其静态调用? / p>

谢谢。

解决方案


我显然有按钮和菜单项,其中一些应该做同样的事情,


正如@nIcEcOw所说,那就是动作 s。 是一个命令对象。除了执行所需命令的功能之外, Action 可能具有关联的图标,键盘快捷键,工具提示文本等。可以使用 Action 对象来完全初始化工具栏按钮或菜单项组件。


所以基本上Swing通过使用 Actions



来使用命令模式的概念

对于OP的问题


命令模式似乎可以,但是我无法得到谁的接收者。


对于接收器,维基使用文本编辑器作为示例,并定义了接收方如此


接收者,目标对象:即将被复制的对象,粘贴,移动等。接收器对象拥有由命令的execute方法调用的方法。接收器通常也是目标对象。例如,如果接收者对象是游标,并且该方法称为moveUp,那么可以预期游标是moveUp操作的目标。另一方面,如果代码由命令对象本身定义,则目标对象将完全是不同的对象。


命令格式的主要更多组件如下所示:



总是与命令模式相关联的四个术语是命令,接收器,调用者和客户端。


客户端,源代码,Invoker:按钮,工具栏按钮或菜单项目点击,用户按下的快捷键。


所以把它放在一起:


  1. MenuItem(客户端)调用它

  2. Action(命令对象)它调用 actionPerformed ,反过来

  3. 调用接收器上的方法。 b $ b

wiki a rticle 是一个很好的阅读与Java示例


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 Command Pattern should be the way to do it, e.g. I have a "save" button and a "save" menu item and they have to implement the same saving algorithm. Command Pattern seems to be ok but I can't get who's the receiver in all that. Isn't a comand supposed to work on an object which implements some sort of "receiver interface", so that you can use different commands on different receivers coupling them aribtrarily? It looks like there's no "receiver" in my implementation of the pattern. Another doubt i have is should a command be implemented as a singleton, since you could potentially call its functions from differents parts of the same project, and it would be handly to instantiate it only once and make it statically invokable?

Thank you.

解决方案

" I obviously have buttons and menu items. Some of these are supposed to do the same stuff,"

As @nIcEcOw noted, that's what Actions are for. This Answer Shows exactly this.

As stated in the How to use Actions :

An Action can be used to separate functionality and state from a component. For example, if you have two or more components that perform the same function, consider using an Action object to implement the function. An Action object is an action listener that provides not only action-event handling, but also centralized handling of the state of action-event-firing components such as tool bar buttons, menu items, common buttons, and text fields. The state that an action can handle includes text, icon, mnemonic, enabled, and selected status.

An There only three Actions. Ont to open, save, and new. Each Action has an ActionCommand, and icon, and and action to perform. Both the JMenuItem and JToolBar button share the same Action and do the same thing. Here is the code you can run.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class ActionTest {

    public ActionTest() {
        ImageIcon openIcon = new ImageIcon(
                ActionTest.class.getResource("/resources/image/open.gif"));
        ImageIcon saveIcon = new ImageIcon(
                ActionTest.class.getResource("/resources/image/save.gif"));
        ImageIcon newIcon = new ImageIcon(
                ActionTest.class.getResource("/resources/image/new.gif"));

        Action openAction = new AbstractAction("Open", openIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Open File");
            }
        };
        Action saveAction = new AbstractAction("Save", saveIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Save File");
            }
        };
        Action newAction = new AbstractAction("New", newIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("New File");
            }
        };

        JMenuItem openMenuItem = new JMenuItem(openAction);
        JMenuItem saveMenuItem = new JMenuItem(saveAction);
        JMenuItem newMenuItem = new JMenuItem(newAction);

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.add(newMenuItem);
        menuBar.add(fileMenu);

        JToolBar toolBar = new JToolBar();
        toolBar.add(Box.createHorizontalGlue());
        toolBar.setBorder(new LineBorder(Color.LIGHT_GRAY, 1));
        toolBar.add(newAction);
        toolBar.add(openAction);
        toolBar.add(saveAction);

        JFrame frame = new JFrame("Toolbar and Menu Test");
        frame.setJMenuBar(menuBar);
        frame.add(toolBar, BorderLayout.PAGE_START);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ActionTest();
            }
        });
    }
}


As stated in the quote from the above mentioned tutorial, you can do more than just add an image and an action command to the Action. You can use it to set mnemonics and accelorators. Here is a custom Action class that takes

  1. An action command String
  2. an icon
  3. a description for tooltips
  4. a mnemonic
  5. and a key accelorator.

    private class MyAction extends AbstractAction {
    
        String name;
    
        public MyAction(String name, Icon icon) {
            super(name, icon);
            this.name = name;
        }
    
        public MyAction(String name, Icon icon, String desc,
                        Integer mnemonic, KeyStroke accelorator) {
            super(name, icon);
            putValue(Action.SHORT_DESCRIPTION, desc);
            putValue(Action.MNEMONIC_KEY, mnemonic);
            putValue(Action.ACCELERATOR_KEY, accelorator);
            this.name = name;
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            switch (name) {
                case "Open":
                    System.out.println("Open");
                    break;
                case "New":
                    System.out.println("New");
                    break;
                case "Save":
                    System.out.println("Save");
                    break;
            }
        }
    }
    

Here's an instantiation of this Action

Action newAction = new MyAction("New", newIcon,
            "Creates a new file",
            new Integer(KeyEvent.VK_N),
            KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));

And here's the new result. You will see the actionCommand in the menu, along with the key mnemonics and accelerators, tooltips, and you will see the jtoolbar buttons share the same traits. You will also see in the final code, that never once once a component created. All you do is add the Action to the JToolBar and the JMenu and let them work their magic.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ActionInterfaceDemo extends JFrame {

    public ActionInterfaceDemo() {
        ImageIcon openIcon = new ImageIcon(ActionInterfaceDemo.class.getResource("/resources/image/open.gif"));
        ImageIcon saveIcon = new ImageIcon(ActionInterfaceDemo.class.getResource("/resources/image/save.gif"));
        ImageIcon newIcon = new ImageIcon(ActionInterfaceDemo.class.getResource("/resources/image/new.gif"));

        Action openAction = new MyAction("Open", openIcon,
                "Opens a file",
                new Integer(KeyEvent.VK_O),
                KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
        Action saveAction = new MyAction("Save", saveIcon,
                "Saves a file",
                new Integer(KeyEvent.VK_S),
                KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        Action newAction = new MyAction("New", newIcon,
                "Creates a new file",
                new Integer(KeyEvent.VK_N),
                KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        setJMenuBar(menuBar);
        menuBar.add(fileMenu);

        fileMenu.add(newAction);
        fileMenu.add(openAction);
        fileMenu.add(saveAction);


        JToolBar toolBar = new JToolBar("Alignment");
        toolBar.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        toolBar.add(Box.createHorizontalGlue());
        toolBar.add(newAction);
        toolBar.add(openAction);
        toolBar.add(saveAction);

        add(toolBar, BorderLayout.PAGE_START);
        add(new JScrollPane(new TextArea(10, 40)), BorderLayout.CENTER);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Action Interface Demo");
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    private class MyAction extends AbstractAction {

        String name;

        public MyAction(String name, Icon icon) {
            super(name, icon);
            this.name = name;
        }

        public MyAction(String name, Icon icon, String desc,
                Integer mnemonic, KeyStroke accelorator) {
            super(name, icon);
            putValue(Action.SHORT_DESCRIPTION, desc);
            putValue(Action.MNEMONIC_KEY, mnemonic);
            putValue(Action.ACCELERATOR_KEY, accelorator);
            this.name = name;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            switch (name) {
                case "Open":
                    System.out.println("Open");
                    break;
                case "New":
                    System.out.println("New");
                    break;
                case "Save":
                    System.out.println("Save");
                    break;
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new ActionInterfaceDemo();
            }
        });
    }
}


UPDATE

The better explain the relationship of Action and Command Patterns

As noted in Command Pattern

The command pattern is a commonly used pattern which encapsulates a method call or action-like code into a single class. The advantages of being able to package a method (or methods) into a class become evident when you have multiple invokers for a single action (for example a button and a menu item may perform the same action).

In Swing and Borland Delphi programming, an Action is a command object. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on. A toolbar button or menu item component may be completely initialized using only the Action object.

So basically Swing uses the concept of the command pattern through the use of Actions

As for OP's question

"Command Pattern seems to be ok but I can't get who's the receiver in all that."

As for the receiver, the wiki uses a text editor as an example and defines the receiver as such

Receiver, Target Object: the object that is about to be copied, pasted, moved, etc. The receiver object owns the method that is called by the command's execute method. The receiver is typically also the target object. For example, if the receiver object is a cursor and the method is called moveUp, then one would expect that the cursor is the target of the moveUp action. On the other hand, if the code is defined by the command object itself, the target object will be a different object entirely.

The main more components of a Command Pattern are stated as follows

Four terms always associated with the command pattern are command, receiver, invoker and client.

Client, Source, Invoker: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user.

So to put it all together:

  1. The MenuItem (client) invokes it
  2. Action (command object) which calls it actionPerformed which in turn
  3. Invokes an method on the receiver.

The wiki article is good read with a Java example

这篇关于命令模式使用JComponents时的有用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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