简体   繁体   English

我代码中的类图

[英]Class Diagram from my code

I used a plug-in of Eclipse to create the class diagram of this code: 我使用Eclipse的插件来创建此代码的类图:

 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: Eclipse绘制的图就是这个图:

在此处输入图片说明

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. 我想知道是因为我认为ActionListeners被认为是子类,并且也没有显示main方法中的ActionListener,但也许只是我不了解类图的工作原理。

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 : ActionListener实际上是一个interface

http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html 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 http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

It looks right to me. 在我看来不错。 The ActionListeners you have defined are anonymous classes for your protected attributes a1, and a2. 您定义的ActionListener是用于受保护属性a1和a2的匿名类。 Basically what the anonymous classes are doing is subclassing the ActionListener class. 基本上,匿名类在做什么是将ActionListener类子类化。 These new, unnamed classes are set to a1, and a2. 这些新的未命名类设置为a1和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. 同样,未显示main方法中的那个原因的原因是匿名ActionListener是main函数的局部变量。

Here is some information that Oracle: has about anonymous classes ( http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html ) 这是Oracle:关于匿名类的一些信息( http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

Hope this help, good luck with your programming. 希望对您有所帮助,祝您编程愉快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM