从我们的Java教科书中编写程序作为练习。我完全按照书中的说明来使用它,但是它一直给我“找不到符号错误”的提示。 [英] Writing a program from our Java textbook as an exercise. I have it exactly as the book has it, but it keeps giving me "Cannot find symbol errors"

查看:108
本文介绍了从我们的Java教科书中编写程序作为练习。我完全按照书中的说明来使用它,但是它一直给我“找不到符号错误”的提示。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Java教科书中编写一个程序,作为编写类似程序的练习。我已经按照它在我们的书中所写的内容(从Java开始,从控制结构到数据结构,第三版,第833页)(已四重选中!),每次都抛出8无法在同一行上找到符号错误,并且我无法弄清楚自己在做什么错。

I am writing a program out from my Java textbook as practice for writing our own similar program. I have it written exactly as it appears in our book (Starting Out With Java, From Control Structures Through Data Structures, 3rd Edition, Page 833) (quadruple checked!) and every time, it throws 8 "cannot find symbol" errors on the same lines, and I cannot figure out what I am doing wrong.

这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 *  The OrderCalculatorGUI class creates the GUI for the Brandi's Bagel House application
 */

public class OrderCalculatorGUI extends JFrame
{
    private BagelPanel bagels;
    private ToppingPanel toppings;
    private CofeePanel coffee;
    private GreetingPanel banner;
    private JPanel buttonPanel;
    private JButton calcButton;
    private JButton exitButton;
    private final double TAX_RATE = 0.06;

    /**
     * Constructor
     */

    public OrderCalculatorGUI()
    {
        //Display a title.
        setTitle("Order Calculator");

        //Specify an action for the close button.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //create a orderLayout manager.
        setLayout(new BorderLayout());

        // Create the custom panels.
        banner = new GreetingPanel();
        bagels = new BagelPanel();
        toppings = new ToppingPanel();
        coffee = new CoffeePanel();

        // Create the button panel.
        buildButtonPanel();

        //Add the components to the content pane.
        add(banner, BorderLayout.NORTH);
        add(bagels, BorderLayout.WEST);
        add(toppings, BorderLayout.CENTER);
        add(coffee, BorderLayout.EAST);
        add(buttonPanel, BorderLayout.SOUTH);

        //Pack the contents of the window and display it.
        pack();
        setVisible(true);
    }

    /**
     * The buildButtonPanel method builds the button panel.
     */

    private void buildButtonPanel()
    {
        //Create a panel for the buttons.
        buttonPanel = new JPanel();

        //Create the buttons.
        calcButton = new JButton("Calculate");
        exitButton = new JButton("Exit");

        //Register the action listeners.
        calcButton.addActionListener(new CalcButtonListener());
        exitButton.addActionListener(new ExitButtonListener());

        //Add the buttons to the button panel.
        buttonPanel.add(calcButton);
        buttonPanel.add(exitButton);
    }

    /**
     * Private inner class that handles the event when
     * the user clicks the Calculate button.
     */

    private class CalcButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            // Variables to hold the subtotal, tax, and total
            double subtotal, tax, total;

            //Calculate the subtotal.
            subtotal = bagels.getBagelCost() +
                       toppings.getToppingCost() +
                       coffee.getCoffeeCost();

            //Calculate the sales tax.
            tax = subtotal * TAX_RATE;

            //Calculate the total.
            total = subtotal * TAX_RATE;

            //Calculate the total.
            total = subtotal + tax;

            //Display the charges.
            JOptionPane.showMessageDialog(null,
                String.format("Subtotal: $%,.2f\n" +
                             "Tax: $%,.2f\n" +
                             "Total: $%,.2f",
                             subtotal, tax, total));   
        }

    }

    /**
     * Private inner class that handles the event when
     * the user clicks the Exit button.
     */

    private class ExitButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }

    /**
     * main method
     */

    public static void main(String[] args)
    {
        new OrderCalculatorGUI();
    }
}

这是cmd提示符后的控制台输出运行 javac OrderCalculatorGUI.java

And here is the console output in the cmd prompt after running "javac OrderCalculatorGUI.java"

OrderCalculatorGUI.java:11: error: cannot find symbol
    private BagelPanel bagels;
            ^
  symbol:   class BagelPanel
  location: class OrderCalculatorGUI
OrderCalculatorGUI.java:12: error: cannot find symbol
    private ToppingPanel toppings;
            ^
  symbol:   class ToppingPanel
  location: class OrderCalculatorGUI
OrderCalculatorGUI.java:13: error: cannot find symbol
    private CofeePanel coffee;
            ^
  symbol:   class CofeePanel
  location: class OrderCalculatorGUI
OrderCalculatorGUI.java:14: error: cannot find symbol
    private GreetingPanel banner;
            ^
  symbol:   class GreetingPanel
  location: class OrderCalculatorGUI
OrderCalculatorGUI.java:36: error: cannot find symbol
        banner = new GreetingPanel();
                     ^
  symbol:   class GreetingPanel
  location: class OrderCalculatorGUI
OrderCalculatorGUI.java:37: error: cannot find symbol
        bagels = new BagelPanel();
                     ^
  symbol:   class BagelPanel
  location: class OrderCalculatorGUI
OrderCalculatorGUI.java:38: error: cannot find symbol
        toppings = new ToppingPanel();
                       ^
  symbol:   class ToppingPanel
  location: class OrderCalculatorGUI
OrderCalculatorGUI.java:39: error: cannot find symbol
        coffee = new CoffeePanel();
                     ^
  symbol:   class CoffeePanel
  location: class OrderCalculatorGUI
8 errors

在这一点上,我有些困惑。我不确定发生了什么问题,我犯了什么愚蠢的错误或其他奇怪的异常情况。感谢您的协助。

At this point I am a bit stumped. I am not sure what went wrong, what silly mistake I'm making, or other weird anomaly is happening. I appreciate any assitance.

谢谢!

推荐答案

根据您的错误,表示无法找到表示您尚未定义Bangel面板,Greeting面板等类的符号。因此,要使用此符号,必须首先定义这些类或导入相应的包。
建议您查看课程资料并找到合适的代码,然后重试,或者如果找不到,请在此处附上本书代码的图片。 b谢谢,希望能对您有所帮助。快乐的编码/

As per your error, it says that it is unable to find the symbol that means you have not defined the class Bangel panel, Greeting panel, etc. so in order to use this, you must first define these classes or import the respective package. I suggest you to please review your course material and find the suitable code then try again or if you are unable to find then attach the picture of the book`s code here. Thank you, Hope this will help you. Happy coding/

这篇关于从我们的Java教科书中编写程序作为练习。我完全按照书中的说明来使用它,但是它一直给我“找不到符号错误”的提示。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆