Java Reference
In-Depth Information
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class SimplePaint extends JFrame implements ActionListener {
private final String ACTION_NEW = "New Image";
private final String ACTION_LOAD = "Load Image";
private final String ACTION_SAVE = "Save Image";
private final SimplePaintPanel paintPanel = new SimplePaintPanel();
public SimplePaint() {
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Simple Paint");
initMenu();
this.getContentPane().add(paintPanel);
pack();
setVisible(true);
}
private void initMenu() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem mnuNew = new JMenuItem(ACTION_NEW);
JMenuItem mnuLoad = new JMenuItem(ACTION_LOAD);
JMenuItem mnuSave = new JMenuItem(ACTION_SAVE);
mnuNew.setActionCommand(ACTION_NEW);
mnuLoad.setActionCommand(ACTION_LOAD);
mnuSave.setActionCommand(ACTION_SAVE);
mnuNew.addActionListener(this);
mnuLoad.addActionListener(this);
mnuSave.addActionListener(this);
menu.add(mnuNew);
menu.add(mnuLoad);
menu.add(mnuSave);
menuBar.add(menu);
this.setJMenuBar(menuBar);
}
@Override
public void actionPerformed(ActionEvent ev) {
switch (ev.getActionCommand()) {
case ACTION_NEW:
paintPanel.clear();
break;
case ACTION_LOAD:
doLoadImage();
break;
case ACTION_SAVE:
doSaveImage();
break;
}
Search WWH ::




Custom Search