Question

I used a plug-in of Eclipse to create the class diagram of this code:

 public class ButtonGrid 
   {
   private static int difficulty, moveleft, Counter, treasure_x , treasure_y;
   private static String message;
   JTextField tf = new JTextField();
   public static JTextField tf2 = new JTextField();
   JFrame frame = new JFrame(); //creation of the main game window
   JPanel panel = new JPanel();
   JPanel panel2 = new JPanel(new FlowLayout());
   JPanel panel3 = new JPanel();
   JLabel hint = new JLabel("Hint:");
   JButton[][] grid; //grid buttons


   public ButtonGrid (int width, int length)
      {

      }

      ActionListener al = new ActionListener() //Action listener for the buttongrid
         {
          public void actionPerformed(ActionEvent e)
            {



            }
         };

        ActionListener al2 = new ActionListener() // Action listener for the reset button
            {
             public void actionPerformed (ActionEvent e)
                {

                  }
                }
            };


      public static void main (String[] args)
         { 

         }

I cut some useless parts to reduce the size. The diagram that Eclipse draw is this one:

enter image description here

Do you think it's correct? I'm wondering because i thougth the ActionListeners were considered sub-classes, and also the ActionListener in the main method is not showed, but maybe it's just me not understanding how class diagrams work.

Was it helpful?

Solution

It looks right to me. The ActionListeners you have defined are anonymous classes for your protected attributes a1, and a2. Basically what the anonymous classes are doing is subclassing the ActionListener class. These new, unnamed classes are set to a1, and a2. That is why they show up the way they do in the class diagram. Also the reason that the one in your main method isn't showing up, is that anonymous ActionListener is a local variable to your main function.

Here is some information that Oracle: has about anonymous classes (http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html)

Hope this help, good luck with your programming.

OTHER TIPS

Your diagram seems correct. None of the variables you create inside the methods will appear in this diagram. Only the variables you define on the top (or outside the methods but inside the class definition) will appear in the diagram:

   private static int difficulty, moveleft, Counter, treasure_x , treasure_y;
   private static String message;
   JTextField tf = new JTextField();
   public static JTextField tf2 = new JTextField();
   JFrame frame = new JFrame(); //creation of the main game window
   JPanel panel = new JPanel();
   JPanel panel2 = new JPanel(new FlowLayout());
   JPanel panel3 = new JPanel();
   JLabel hint = new JLabel("Hint:");
   JButton[][] grid; //grid buttons

  ActionListener al = new ActionListener() //Action listener for the buttongrid
  {
      //defintion of this ActionListner 
  };

ActionListener al2 = new ActionListener() // Action listener for the reset button
{
    //definition of this ActionListener
};

ActionListener is actually an interface:

http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html

You must define it or else you can't use it. A subclass is a class that has a parent class:

http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top