没有为add()找到合适的方法

时间:2012-07-09 13:51:48

标签: java compiler-errors

我正在为朋友在NetBeans中开发一个Java应用程序。这是代码:

public class ReportGenerator extends JFrame implements ActionListener {
//GUI components
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem newItem;
    private JMenuItem openItem;
    private JMenuItem exitItem;

private ReportGeneratorSetup setup;
private ReportGeneratorProgram application;

public ReportGenerator()
{
    menuBar = new JMenuBar();
    fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    exitItem = new JMenuItem("Exit");
    fileMenu.add(exitItem);
    exitItem.addActionListener(this);
    menuBar.add(fileMenu);
    this.setJMenuBar(menuBar);
    this.setSize(1000,1200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);

    setup = new ReportGeneratorSetup(this);
    application = new ReportGeneratorProgram(this,setup);

    //this.add(setup);  
    this.validate();
}

public static void main(String[] args) {

    ReportGenerator rg = new ReportGenerator();
}  
public void switchToPanel(String panel)
{
    this.getContentPane().removeAll();

    if(panel.equals("Eval"))
    {
                    application.setupComponents();
        this.getContentPane().add(application);

    }
    else if(panel.equals("Setup"))
    {
        this.getContentPane().add(setup);
    }

    this.invalidate();
    this.validate();
}

@Override
public void actionPerformed(ActionEvent arg0)
{
    if(arg0.getSource() instanceof JMenuItem)
    {
        JMenuItem item = (JMenuItem)arg0.getSource();

        if(item.getText().equals("Exit"))
        {
            dispose();
            System.exit(0);
        }
    }
}

}

我收到的错误是:

no suitable method found for add(reportgenerator.ReportGeneratorSetup)
    method java.awt.Container.add(java.awt.Component,java.lang.Object,int) is not applicable
      (actual and formal argument lists differ in length)
    method java.awt.Container.add(java.awt.Component,java.lang.Object) is not applicable
      (actual and formal argument lists differ in length)
    method java.awt.Container.add(java.awt.Component,int) is not applicable
      (actual and formal argument lists differ in length)
    method java.awt.Container.add(java.lang.String,java.awt.Component) is not applicable
      (actual and formal argument lists differ in length)
    method java.awt.Container.add(java.awt.Component) is not applicable
      (actual argument reportgenerator.ReportGeneratorSetup cannot be converted to java.awt.Component by method invocation conversion)
    method java.awt.Component.add(java.awt.PopupMenu) is not applicable
      (actual argument reportgenerator.ReportGeneratorSetup cannot be converted to java.awt.PopupMenu by method invocation conversion)

谁能告诉我为什么会这样?非常感谢。

1 个答案:

答案 0 :(得分:3)

您的班级ReportGeneratorSetup需要是某种组件,很可能需要延长JPanelJComponent