newbie's question about Command Pattern

I

iclinux

Hi, I'm new to Java, and when learning Command Pattern, I come across
two questions. I'll show my code first, there're three files named
Command.java, CommandHolder.java, and GUItest.java.

// Command.java
package GUItest;

public interface Command {
public void Execute();
}

// CommandHolder.java
package GUItest;
public interface CommandHolder
{
public void setCommand(Command comd);
public Command getCommand();
}

// Guitest.java
package GUItest;
import java.awt.Button;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;

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

public class GUItest
{
public class CmdMenu extends JMenuItem implements CommandHolder {
protected Command menuCommand;
protected JFrame frame;

public CmdMenu(String name, JFrame frm) {
super(name);
frame = frm;
}

public void setCommand(Command comd) {
menuCommand = comd;
}

public Command getCommand() {
return menuCommand;
}
}

class CmdButton extends Button implements CommandHolder {
protected Command btnCommand;

public CmdButton(String caption) {
super(caption);
}

public void setCommand(Command comd) {
btnCommand = comd;
}

public Command getCommand() {
return btnCommand;
}

}

public class FileCommand implements Command {
JFrame frame;

public FileCommand(JFrame fr) {
frame = fr;
}

public void Execute() {
FileDialog fDlg = new FileDialog(frame, "Open file");
fDlg.setVisible(true);
}
}

public class ExitCommand implements Command {
public void Execute() {
System.exit(0);
}
}

public class RedCommand implements Command {
private JFrame frame;

private JPanel pnl;

public RedCommand(JFrame fr, JPanel p) {
frame = fr;
pnl = p;
}

public void Execute() {
pnl.setBackground(Color.red);
}
}

protected JMenuBar mnuBar;
protected JMenu mnuFile;
protected CmdMenu mnuOpen;
protected CmdMenu mnuExit;

public class Window extends JFrame {
public Window(int left, int top, int width, int height) {
super("TEST");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(240, 240);

mnuBar = new JMenuBar();
setJMenuBar(mnuBar);

mnuFile = new JMenu("File");
mnuBar.add(mnuFile);

mnuOpen = new CmdMenu("Open...", this);
mnuFile.add(mnuOpen);
mnuOpen.setCommand(new FileCommand(this));

mnuExit = new CmdMenu("Exit", this);
mnuFile.add(mnuExit);
mnuExit.setCommand(new ExitCommand());

mnuOpen.addActionListener(new WindowActionListener());
mnuExit.addActionListener(new WindowActionListener());

}
}

public class WindowActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
CommandHolder obj = (CommandHolder) e.getSource();
obj.getCommand().Execute();
}

}

public GUItest() {
new Window(0, 0, 500, 500);
}

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

/////////////////////////////////END/////////////////////////////////
um, the code above has a little problem, that is the menu won't display
before maximizing the form, and I don't know why yet. any suggestion?

And my main question is: when adding AcctionListeners, is there a much
concise way instead of the follow?
mnuOpen.addActionListener(new WindowActionListener());
mnuExit.addActionListener(new WindowActionListener());
...
because all components use WindowActionListener, if I add some new ones
latter, I don't want to add some code looks like
XXX..addActionListener(new WindowActionListener()).

Best Regards,
iclinux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top