【问题标题】:Cannot find Symbol = new找不到符号 = 新
【发布时间】:2010-06-17 06:46:43
【问题描述】:

Java 在抱怨!

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

问题:为什么我会收到此错误?...一切似乎都是正确的。

这是编码:

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

Bar() 是

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!");

                    }
                });
            }
        });

    }
}

【问题讨论】:

    标签: java class constructor jframe


    【解决方案1】:

    问题是你有一个需要 JFrame 的构造函数:

    public Bar(final JFrame frame)
    

    但你关心的是不带参数地调用它:

    JPanel panel = new Bar();
    

    您需要向 Bar 传递一个 JFrame 的实例。

    【讨论】:

    • 我不明白。请解释
    • JPanel 面板 = new Bar(new JFrame()); ...但是你需要更多的东西才能让它工作(虽然这会让它编译)。您仍然需要使框架可见等...
    • 好的,可以。现在我从哪里开始添加代码以使框架可见并设置大小,因为这个 new Frame() 不完全有一个变量名
    • 我会给它一个名字,并将变量传递给构造函数。
    【解决方案2】:

    您正在调用 Bar() 构造函数,但您没有无参数构造函数。您需要传递 JFrame 参数。

    【讨论】:

      【解决方案3】:

      除了 TofuBeer 答案 - 考虑使用像 eclipse 或 netbeans 之类的 IDE(仅举两个例子)。这些 IDE 将在键入代码时显示此类错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-11
        • 1970-01-01
        • 2013-09-12
        • 2013-06-06
        • 2017-10-01
        • 1970-01-01
        • 2015-05-10
        • 2014-01-05
        相关资源
        最近更新 更多