java - 编写 Java 教科书中的程序作为练习。我的内容和书上的一模一样,但它一直给我 "Cannot find symbol errors"

标签 java swing compiler-errors javac cannot-find-symbol

我正在根据我的 Java 教科书编写一个程序,作为编写我们自己的类似程序的练习。我完全按照我们的书(Starting Out With Java,From Control Structures Through Data Structures,第 3 版,第 833 页)中的方式编写它(四重检查!)并且每次都会抛出 8 "在同一行上找不到符号”错误,并且我无法弄清楚我做错了什么。

这是我的代码:

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();
    }
}

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

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

此时我有点难住了。我不确定出了什么问题,我犯了什么愚蠢的错误,或者发生了其他奇怪的异常情况。我感谢任何帮助。

谢谢!

最佳答案

根据您的错误,它表示无法找到符号,这意味着您尚未定义类 Bangel panel、Greeting panel 等。因此,为了使用它,您必须首先定义这些类或导入各自的包。 我建议您查看您的类(class) Material 并找到合适的代码,然后重试,或者如果您找不到,请在此处附上本书代码的图片。 谢谢您,希望这对您有帮助。快乐编码/

关于java - 编写 Java 教科书中的程序作为练习。我的内容和书上的一模一样,但它一直给我 "Cannot find symbol errors",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60750184/

相关文章:

java - 为了在 java swing 中使用 setToolTipText() 方法,我应该导入什么?

java - 如何将Java写的 "ArrayList<ClassNod>"转成C++程序语言?

java - 将变量从 Activity 传递到自定义 View 类

swing - 使 JXTreeTable 可编辑

java - PaintComponent 方法未在面板上显示任何内容

java - 使用泛型和扩展特征时,Groovy静态编译失败

android - 在 Android 中初始化一个大的常量数据结构

java - 使用 Eclipse 的 Android 应用程序

java - 如何使用 bufferedreader/writer 在 java 中复制图像

java - 如何防止我的 JScrollPane/JTextArea 超出包含的 JTabbedPane?