我使用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)
{
}
我剪了一些无用的部件以减小尺寸。 Eclipse绘制的图表就是这个:
你认为这是正确的吗?我想知道因为我认为ActionListeners被认为是子类,并且主方法中的ActionListener也没有显示,但也许只是我不了解类图的工作原理。
答案 0 :(得分:0)
您的图表似乎正确无误。您在方法中创建的所有变量都不会出现在此图表中。只有您在顶部(或方法外部但在类定义内)定义的变量才会出现在图表中:
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
实际上是interface
:
http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html
您必须定义它,否则您无法使用它。子类是具有父类的类:
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
答案 1 :(得分:0)
看起来对我来说。您定义的ActionListener是受保护属性a1和a2的匿名类。基本上匿名类正在做的是对ActionListener类进行子类化。这些新的未命名类设置为a1和a2。这就是他们在课堂图中表现出来的原因。另外,main方法中没有显示的方法是匿名ActionListener是主函数的局部变量。
以下是Oracle:关于匿名类(http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html)
的一些信息希望这有帮助,祝你的节目顺利。