I'm rewriting an old Swing project that I did a few months ago, a fairly simple copy-cat of MS paint. I've been doing a pretty good job of keeping the code maintainable and organized, but I've hit a wall on one class. All I need is some general tips on organization on this one class.
The way I have my program set up is that each component of my painting program (buttons for colors, eraser button, JPanel
to paint on) is actually a relative (extended from) of another swing component class (for instance PaintPanel
, the panel you paint on, extends JPanel, or colorButton
is an extension of JButton
). This just allows me to keep things encapsulated and organized.
So, I needed a JFileMenu
for the top of the program in order to save, open files, edit, etc. I created a new Class called PaintMenuBar
, extended it from JMenuBar
, and started creating the JMenus
and JMenuItems
. Now, each JMenuItem
obviously needs an ActionListener
, so I just gave each one a different ActionListener
. I'm skeptical about this because I know it can be kind of disorganized looking.
Basically, I just want somebody to let me know if this practice is all right, or if there is a better alternative that doesn't involve me making 20 new classes.
Here's the entire class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class PaintMenuBar extends JMenuBar
{
private static final long serialVersionUID = 1L;
JMenu fileMenu;
JMenu editMenu;
JMenu aboutMenu;
public PaintMenuBar()
{
setUpFileMenu();
setUpEditMenu();
setUpAboutMenu();
}
// Set up menu items & such
private void setUpFileMenu()
{
fileMenu = new JMenu("File");
// File menu items
JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
}
});
JMenuItem openMenuItem = new JMenuItem("Open");
openMenuItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
}
});
JMenuItem saveMenuItem = new JMenuItem("Save");
saveMenuItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
}
});
JMenuItem saveAsMenuItem = new JMenuItem("Save As");
saveAsMenuItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
}
});
JMenuItem printMenuItem = new JMenuItem("Print");
printMenuItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
}
});
add(fileMenu);
}
private void setUpEditMenu() // Create Edit Menu
{
editMenu = new JMenu("Edit");
}
private void setUpAboutMenu() // Create About Menu
{
aboutMenu = new JMenu("About Paint 2.0");
}
}
I am aware of some of the junk that is in this class that I still need to clean up, but just try and ignore it.