Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 命令设计模式简单GUI空指针_Java_Swing_Design Patterns_Command Pattern - Fatal编程技术网

Java 命令设计模式简单GUI空指针

Java 命令设计模式简单GUI空指针,java,swing,design-patterns,command-pattern,Java,Swing,Design Patterns,Command Pattern,我有这些类:一个JPanel扩展、一个接口和3个JmenuItem类 public class RedFrame extends javax.swing.JFrame implements ActionListener { private JMenuBar jMenuBar1; private JPanel jPanel1; private fileExitCommand jMenuItem3; private fileOpenCommand jMenuItem2; private btnRed

我有这些类:一个JPanel扩展、一个接口和3个JmenuItem类

public class RedFrame extends javax.swing.JFrame implements ActionListener {
private JMenuBar jMenuBar1;
private JPanel jPanel1;
private fileExitCommand jMenuItem3;
private fileOpenCommand jMenuItem2;
private btnRedCommand jMenuItem1;
private JMenu jMenu1;

/**
 * Auto-generated main method to display this JFrame
 */
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            RedFrame inst = new RedFrame();
            inst.setLocationRelativeTo(null);
            inst.setVisible(true);
        }
    });
}

public RedFrame() {
    super();
    initGUI();
}

private void initGUI() {
    try {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        {
            jPanel1 = new JPanel();
            getContentPane().add(jPanel1, BorderLayout.CENTER);
        }
        {
            jMenuBar1 = new JMenuBar();
            setJMenuBar(jMenuBar1);
            {
                jMenu1 = new JMenu();
                jMenuBar1.add(jMenu1);
                jMenu1.setText("Meniu");
                {
                    jMenuItem1 = new btnRedCommand(jPanel1, "RED");
                    jMenu1.add(jMenuItem1);

                }
                {
                    jMenuItem2 = new fileOpenCommand("Open");
                    jMenu1.add(jMenuItem2);

                }
                {
                    jMenuItem3 = new fileExitCommand("Exit");
                    jMenu1.add(jMenuItem3);

                }
            }
        }
        jMenuItem1.addActionListener(this);
        jMenuItem2.addActionListener(this);
        jMenuItem3.addActionListener(this);
        pack();
        setSize(300 * 16 / 9, 300);
    } catch (Exception e) {
        // add your error handling code here
        e.printStackTrace();
    }
}

@Override
public void actionPerformed(ActionEvent event) {
     Execute();

}

 }


我希望调用在3个JMenuItems中实现的Execute方法,具体取决于从菜单中选择了哪个jMenuItem。我怎样才能正确地做到这一点?3个jMenuItems是否需要一个包装器类?

对于这些简单的GUI任务来说,这种模式有些过分,但是在您的
ActionListener
中,您可以执行以下操作:

Command command = (Command) event.getSource();
command.Execute();
说明:由于每个自定义
JMenuItem
实现了
命令
接口,因此可以将它们转换为这样的类型&从而利用
Execute
方法

发生
NullPoinerException
的原因是
JPanel
实例未在
命令
构造函数中赋值:

public btnRedCommand(JPanel p, String text) {
   this.p = p;
   ...

向每个菜单项添加一个特定的ActionListener(总共3个不同的ActionListener),然后忘记命令。如果您必须执行的代码只是更改了GUI,那么您不需要抽象层。@ignis想这样做是为了了解它是如何完成的,GUI只是为了演示。耶,就是这样做的。3个不同的任务,3个不同的听众。没有命令接口,除非你必须处理一个执行一些应用程序逻辑的外部类。你的代码让我很困惑,因为你使用的是非标准命名约定,而且你似乎无缘无故地有代码块。另外,这一行是否会编译,
Execute()?我没有看到一个方法可以匹配这个。考虑编辑你的代码,使方法名称都开始小写,类名都开始大写,块被用在他们应该使用的地方,等等。“我们都会有一个更容易理解它的时间。”霍弗·富勒费尔斯谢谢你的关注。问题解决了。注意您关于命名约定和块的建议。非常感谢。我在btnRedCommand的Execute方法中得到一个空指针异常。如何将引用传递到JPanel?我使用这个简单的GUI只是为了演示。太棒了!这个.p=p真漂亮!今天我学到了一些新东西!谢谢现在一切都好了。你能解释一下其他两行代码吗。既然Command是一个接口,那么那里发生了什么?我知道这不是实例化。你所说的实例化发生在你创建菜单项时。是的,直到现在才看到“接口=(接口)…”!谢谢你的新知识!我有一个起点
Command command = (Command) event.getSource();
command.Execute();
public btnRedCommand(JPanel p, String text) {
   this.p = p;
   ...