我对这种方法的工作方式有以下疑问:
protected JButton createToolbarButton(String name, final String id, final JPanel panel)
{
JButton button = new JButton(name); // Create a new JButton
// If the passed Jpanel named "panel" exist, add this to the JPanel named content (the CardLayout container)
if (panel != null)
content.add(panel, id);
else
button.setEnabled(false); // Otherwise disable this button
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
//System.out.println(panel.getClass());
if (panel instanceof SchedulingPanel)
((SchedulingPanel)panel).getTasksSettings().fireSettingsUpdated();
else if (panel instanceof EventsPanel)
((EventsPanel)panel).refreshPanel();
else if (panel instanceof ConfigurationPanel)
((ConfigurationPanel)panel).refreshPane();
showSection(id);
}
});
return button;
}
我有这个名为CreateToolbarButton的方法,它有一些输入参数,包括String id参数。
正如您在此方法中看到的,我将ActionListener 内部类添加到我的 JButton 对象(处理此按钮上的单击事件)。
在此ActionListener 内部类中,它被声明为处理单击事件的actionPerformed()方法,并在此方法的末尾调用showSection(id)方法,将 id 参数传递给似乎与createToolbarButton( )输入参数。
所以在我看来,在我的ActionListener 内部类中,我还可以看到容器方法的参数和变量(createToolbarButton())
这样对吗?为什么?我觉得有点奇怪
肿瘤坏死因子
安德烈亚