从我们的Java教科书编写程序作为练习。我完全按照书中的说明使用它,但是它一直给我“找不到符号错误” [重复]

问题描述 投票:0回答:1
我正在从Java教科书中编写程序,作为编写类似程序的练习。我已经按照书中的内容(

从Java开始,从控制结构到数据结构,第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

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

谢谢!

我正在从Java教科书中编写程序,作为编写类似程序的练习。我写的与我们的书中所写的完全一样(从Java开始,来自控件结构...

java swing compiler-errors javac cannot-find-symbol
1个回答
0
投票
根据您的错误,它表示无法找到表示您尚未定义Bangel面板,Greeting面板等的符号。因此,要使用此符号,必须首先定义这些类或导入相应的类包。

[建议您阅读课程材料并找到合适的代码,然后重试,或者如果找不到,请在此处附上本书代码的图片。

© www.soinside.com 2019 - 2024. All rights reserved.