views:

42

answers:

3

Java is complaining!

cannot find symbol
symbol  : constructor Bar()
location: class Bar
         JPanel panel = new Bar();
                        ^

QUESTION: Why am I getting this error?...everything seems to be correct.

this is the coding:

    public class JFrameWithPanel
    {
      public static void main(String[] args)
      {
           JPanel panel = new Bar();
      }
    }

Bar( ) is

public class Bar extends JPanel
{
    public Bar(final JFrame frame)
    {
        super(new BorderLayout());
        String[] tests = { "A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package" };
        JComboBox comboBox = new JComboBox(tests);
        TextArea text = new TextArea(5, 10);
        add(new JLabel("Welcome to the CIT Test Program "));
        add(new JLabel("Please select which Test Package from the list below."));

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        JMenuItem newMenu = new JMenuItem("New  (Ctrl+N)");
        JMenuItem openMenu = new JMenuItem("Open  (Ctrl+O)");
        JMenuItem saveMenu = new JMenuItem("Save  (Ctrl+S)");
        JMenuItem exitMenu = new JMenuItem("Exit  (Ctrl+W)");
        JMenuItem cutMenu = new JMenuItem("Cut  (Ctrl+X)");
        JMenuItem copyMenu = new JMenuItem("Copy  (Ctrl+C)");
        JMenuItem pasteMenu = new JMenuItem("Paste  (Ctrl+V)");
        JMenuItem infoMenu = new JMenuItem("Help  (Ctrl+H)");
        fileMenu.add(newMenu);
        fileMenu.add(openMenu);
        fileMenu.add(saveMenu);
        fileMenu.add(exitMenu);
        editMenu.add(cutMenu);
        editMenu.add(copyMenu);
        editMenu.add(pasteMenu);
        helpMenu.add(infoMenu);
        this.add(comboBox, BorderLayout.NORTH);
        this.add(text, BorderLayout.SOUTH);
        frame.setJMenuBar(menuBar);
        add(new JButton("Select")
        {
            {
                addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        frame.dispose();
                        JOptionPane.showMessageDialog(frame, "IT WORKS!");

                    }
                });
            }
        });

    }
}
+5  A: 

The problem is that you have a constructor that expects a JFrame:

public Bar(final JFrame frame)

but you care calling it with no arguments:

JPanel panel = new Bar();

You need to pass Bar an instance of a JFrame.

TofuBeer
I dont get it. Please explain
Nick G.
JPanel panel = new Bar(new JFrame()); ... but you will need more that that to make it work (that will get it to compile though). You still need to make the frame visible etc...
TofuBeer
Ok that works. Now where would I start adding the code to make the frame visible and set size, since this new Frame() doesnt exactly have a variable name
Nick G.
figured it out.
Nick G.
I'd give it a name, and pass the variable into the constructor.
TofuBeer
A: 

You are calling the Bar() constructor, but you do not have a no arguments constructor. You need to pass the JFrame argument.

David Rabinowitz
A: 

In addition to TofuBeer answer - consider using an IDE like eclipse or netbeans (just to name two examples). Those IDE's will show errors of this kind already on typing the code.

Andreas_D