我是JAVA的初学者,我正在尝试使用接口类和对象类编写小费计算器,但它继续编译时出现“无法找到符号”的错误。我尝试了一切来修复它但它只是保持在陈述符号。请帮忙。
public class GratuityCalculator extends JFrame
{
/* declarations */
// color objects
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
Color light_gray = new Color(192, 192, 192);
// components
JLabel billAmountJLabel;
JTextField billAmountJTextField;
JLabel gratuityAmountJLabel;
JTextField gratuityAmountJTextField;
JButton enterJButton;
JButton clearJButton;
JButton closeJButton;
// variables
double billAmount;
double gratuityAmount;
double firstNumber;
final double GRATUITY_RATE = .15;
// objects
CalculateDoubles calculateDoubles; // object class declaration
public GratuityCalculator()
{
createUserInterface();
}
public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(Color.white);
contentPane.setLayout(null);
/* initialize components */
billAmountJLabel = new JLabel();
billAmountJLabel.setBounds(50, 50, 120, 20);
billAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
billAmountJLabel.setText("Enter First Number:");
billAmountJLabel.setForeground(black);
billAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(billAmountJLabel);
billAmountJTextField = new JTextField();
billAmountJTextField.setBounds(225, 50, 50, 20);
billAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
billAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
billAmountJTextField.setForeground(black);
billAmountJTextField.setBackground(white);
billAmountJTextField.setEditable(true);
contentPane.add(billAmountJTextField);
gratuityAmountJLabel = new JLabel();
gratuityAmountJLabel.setBounds(50, 80, 150, 20);
gratuityAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
gratuityAmountJLabel.setText("Enter Second Number:");
gratuityAmountJLabel.setForeground(black);
gratuityAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(gratuityAmountJLabel);
gratuityAmountJTextField = new JTextField();
gratuityAmountJTextField.setBounds(225, 80, 50, 20);
gratuityAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
gratuityAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
gratuityAmountJTextField.setForeground(black);
gratuityAmountJTextField.setBackground(white);
gratuityAmountJTextField.setEditable(false);
contentPane.add(gratuityAmountJTextField);
enterJButton = new JButton();
enterJButton.setBounds(20, 300, 100, 20);
enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
enterJButtonActionPerformed(event);
}
}
);
clearJButton = new JButton();
clearJButton.setBounds(130, 300, 100, 20);
clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);
contentPane.add(clearJButton);
clearJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
clearJButtonActionPerformed(event);
}
}
);
closeJButton = new JButton();
closeJButton.setBounds(240, 300, 100, 20);
closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
closeJButton.setText("Close");
closeJButton.setForeground(black);
closeJButton.setBackground(white);
contentPane.add(closeJButton);
closeJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
closeJButtonActionPerformed(event);
}
}
);
setTitle("Gratuity Calculator");
setSize(400, 400);
setVisible(true);
}
// main method
public static void main(String[] args)
{
GratuityCalculator application = new GratuityCalculator();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void enterJButtonActionPerformed(ActionEvent event)
{
getDoubleOne();
}
public void getDoubleOne()
{
try
{
billAmount = Double.parseDouble(billAmountJTextField.getText());
getGratuityAmount();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter bill amount!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
billAmountJTextField.setText("");
billAmountJTextField.requestFocusInWindow();
}
}
public void getGratuityAmount()
{
/* initalize the object class */
calculateDouble = new calculateDouble();
calculateDoubles.setFirstValue(billAmount);
displayGratuityAmount();
}
public void displayGratuityAmount()
{
/* call object class method */
gratuityAmountNumber = calculateDouble.getGratuityAmountNumber();
gratuityAmountJTextField.setText("" + gratuityAmountNumber);
}
public void clearJButtonActionPerformed(ActionEvent event)
{
billAmountJTextField.setText("");
billAmountJTextField.requestFocusInWindow();
gratuityAmountTextField.setText("");
}
public void closeJButtonActionPerformed(ActionEvent event)
{
GratuityCalculator.this.dispose();
}
}
class CalculateDoubles
{
/* object class variables */
double billAmount;
double gratuityAmount;
double firstNumber;
final double GRATUITY_RATE = .15;
/* first value is received by object class */
public CalculateDoubles(double billAmount)
{
firstNumber = billAmount;
}
/* return method sends sum of values to interface class */
public double getGratuityAmount()
{
return firstNumber * GRATUITY_RATE;
}
}
答案 0 :(得分:0)
有些错误......
将firstNumber添加到CalculateDoubles类:
class CalculateDoubles {/* object class non-constructor */
/* object class variables */
double billAmount;
double gratuityAmount;
double firstNumber;
...
定义GRATUITY_RATE:
double GRATUITY_RATE = 0.15;
我看到其他一些错误,似乎你没有发布所有课程内容。