从我们的 Java 课本中编写一个程序作为练习。我和书上的一模一样,但它一直给我 "Cannot find symbol errors"
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"
我正在根据 Java 教科书编写一个程序,作为编写我们自己的类似程序的练习。我已经完全按照我们的书(Starting Out With Java,From Control Structures Through Data Structures,第 3 版,第 833 页 )(四重检查!)和每次,它都会在同一行抛出 8 "cannot find symbol" 个错误,我无法弄清楚我做错了什么。
这是我的代码:
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
在这一点上我有点难过。我不确定出了什么问题,我犯了什么愚蠢的错误,或者发生了其他奇怪的异常情况。感谢您的帮助。
谢谢!
根据你的错误,它说它找不到符号,这意味着你还没有定义 class Bangel 面板,问候面板等所以为了使用它,你必须先定义这些 classes 或导入相应的包。
我建议您查看您的课程 material 并找到合适的代码,然后重试,或者如果找不到,请在此处附上本书代码的图片。
谢谢,希望这会对你有所帮助。快乐编码/
我正在根据 Java 教科书编写一个程序,作为编写我们自己的类似程序的练习。我已经完全按照我们的书(Starting Out With Java,From Control Structures Through Data Structures,第 3 版,第 833 页 )(四重检查!)和每次,它都会在同一行抛出 8 "cannot find symbol" 个错误,我无法弄清楚我做错了什么。
这是我的代码:
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
在这一点上我有点难过。我不确定出了什么问题,我犯了什么愚蠢的错误,或者发生了其他奇怪的异常情况。感谢您的帮助。
谢谢!
根据你的错误,它说它找不到符号,这意味着你还没有定义 class Bangel 面板,问候面板等所以为了使用它,你必须先定义这些 classes 或导入相应的包。 我建议您查看您的课程 material 并找到合适的代码,然后重试,或者如果找不到,请在此处附上本书代码的图片。 谢谢,希望这会对你有所帮助。快乐编码/