我的程序假设制作一个GUI来计算输入数字的平方根。我无法弄清楚为什么这段代码不能编译。我不断收到以下错误消息:
无法找到符号 symbol:方法getText(double)
我做错了什么?
import java.awt.event.ActionEvent; //Next group of lines import various Java classes
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.text.*;
public class SquareRoot extends JFrame
{
public static void main(String[] args) {
//Creates Window
JFrame frame = new JFrame();
frame.setSize(450, 300);
frame.setTitle("Find the Square Root");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel Numberlbl = new JLabel("Enter a number:");
final JTextField NumberField = new JTextField(10);
NumberField.setText("");
JLabel Answerlbl = new JLabel("Square Root of your number is:");
final JTextField AnswerField = new JTextField(10);
AnswerField.setText("");
JLabel ButtonLabel = new JLabel("Calculate Square Root");
JButton button = new JButton("√");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,2));
panel.add(Numberlbl);
panel.add(NumberField);
panel.add(ButtonLabel);
panel.add(button);
panel.add(Answerlbl);
panel.add(AnswerField);
frame.add(panel);
class CalculateListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
double NumberX = Double.parseDouble(NumberField.getText());
double Answer = Math.sqrt(NumberX);
AnswerField.setText(Answer);
}
}
ActionListener listener = new CalculateListener();
button.addActionListener(listener);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
答案 0 :(得分:4)
我得到的唯一编译错误是AnswerField.setText()行 - 如果你查看setText()的API参考,它需要一个字符串,但你试图传递一个double。
查看NumberFormat类,将double正确转换为字符串。一个更简单的选项是使用Double对象(与double数据类型相反,注意大小写),并使用其toString()方法。一个简单的方法是把它写成(“”+ Answer),因为它会自动为你转换它。
答案 1 :(得分:1)
代码将无法编译,因为方法setText(String text)需要一个String参数,并且你给它一个double。
要使代码正常工作,请使用:
AnswerField.setText(String.valueOf(Answer));
答案 2 :(得分:1)
final Double answer = Math.sqrt(NumberX);
AnswerField.setText(answer.toString());
答案 3 :(得分:0)
这个编译。你走了:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SquareRoot extends JFrame
{
public static void main(String[] args) {
//Creates Window
JFrame frame = new JFrame();
frame.setSize(450, 300);
frame.setTitle("Find the Square Root");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel numberlbl = new JLabel("Enter a number:");
final JTextField numberField = new JTextField(10);
numberField.setText("");
JLabel answerlbl = new JLabel("Square Root of your number is:");
final JTextField answerField = new JTextField(10);
answerField.setText("");
JLabel buttonLabel = new JLabel("Calculate Square Root");
JButton button = new JButton("√");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,2));
panel.add(numberlbl);
panel.add(numberField);
panel.add(buttonLabel);
panel.add(button);
panel.add(answerlbl);
panel.add(answerField);
frame.add(panel);
class CalculateListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
double numberX = Double.parseDouble(numberField.getText());
double answer = Math.sqrt(numberX);
answerField.setText(""+answer);
}
}
ActionListener listener = new CalculateListener();
button.addActionListener(listener);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}