質問

I have these classes: a JPanel extension, an interface and 3 x JmenuItem classes.

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();

}

 }

And

public class btnRedCommand extends JMenuItem implements Command {

protected JPanel p;
protected String text;

public btnRedCommand(JPanel p, String text) {

    p.setBackground(Color.cyan);
    this.setText(text);
}

public void Execute() {
    // TODO Auto-generated method stub
    p.setBackground(Color.red);
}

}

and

public interface Command  {

public void Execute();

}

I want the Execute method which is implemented in the 3 JMenuItems to be called depending on which jMenuItem from Menu was selected. How can I properly do this? Do I need a wrapper class for the 3 jMenuItems?

役に立ちましたか?

解決

This pattern is overkill here for these simple GUI tasks, but in your ActionListener, you could do:

Command command = (Command) event.getSource();
command.Execute();

Explanation: As each of custom JMenuItem implements the Command interface, they can be cast as such & thereby avail of the Execute method.

The reason that an NullPoinerException is occurring is that the JPanel instance is not assigned in the Command constructor:

public btnRedCommand(JPanel p, String text) {
   this.p = p;
   ...
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top