gpt4 book ai didi

java - 接口(interface)类和对象类正在编译符号错误

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:11 32 4
gpt4 key购买 nike

我是 JAVA 的初学者,我正在尝试使用接口(interface)类和对象类来编写小费计算器,但它不断编译并出现错误,提示“找不到符号”。我尝试了一切方法来修复它,但它只是不断在说明符号上。请帮忙。

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

}

最佳答案

一些错误...

将firstNumber添加到CalculateDoubles类中:

class CalculateDoubles  {/* object class non-constructor */

/* object class variables */
double billAmount;
double gratuityAmount;
double firstNumber;
...

定义 GRATUITY_RATE:

double GRATUITY_RATE = 0.15;

我看到一些其他错误,看来您没有发布所有类(class)内容。

关于java - 接口(interface)类和对象类正在编译符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26963336/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com