[英]How to add the input from a textfield
Here is my code below, I will highlight what is throwing the error, I am completely lost on how to fix this... 这是我下面的代码,我将突出显示错误是什么,我完全迷失了如何解决这个问题...
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.event.ActionListener.*;
import java.awt.event.TextListener.*;
public abstract class GradeAverage extends Applet
implements ActionListener, TextListener{
Button Ok=new Button("Ok");
Button Cancel=new Button("cancel");
TextField Math=new TextField("Math Grade",2);
TextField Science=new TextField("Scien Grade",2);
TextField SocialStudies=new TextField("Social Studies Grade",2);
TextField English=new TextField("English Grade",2);
TextField Elective=new TextField("Elective Grade",2);
TextField Elective2=new TextField("2nd Elective Grade",2);
TextArea message=new TextArea();
public void init(){
GridBagLayout gbl=new GridBagLayout();
setLayout(gbl);
GridBagConstraints c=new GridBagConstraints();
c.anchor=GridBagConstraints.WEST;
c.weightx=2.0;
c.weighty=2.0;
c.fill=GridBagConstraints.HORIZONTAL;
c.insets=new Insets(10,10,10,10);
gbl.setConstraints(Math,c); add(Math);
c.fill=GridBagConstraints.NONE;
gbl.setConstraints(Science,c); add(Science);
c.fill=GridBagConstraints.HORIZONTAL;
gbl.setConstraints(SocialStudies,c); add(SocialStudies);
c.gridy=1;
gbl.setConstraints(English,c); add(English);
c.gridy=2;
gbl.setConstraints(Elective,c); add(Elective);
c.fill=GridBagConstraints.NONE;
gbl.setConstraints(Elective2,c); add(Elective2);
c.gridy=3;
c.anchor=GridBagConstraints.CENTER;
gbl.setConstraints(Ok,c); add(Ok);
gbl.setConstraints(Cancel,c); add(Cancel);
c.gridy=4;
c.gridwidth=3;
c.fill=GridBagConstraints.BOTH;
gbl.setConstraints(message,c); add(message);
Math.addTextListener(this);
Science.addTextListener(this);
SocialStudies.addTextListener(this);
English.addTextListener(this);
Elective.addTextListener(this);
Elective2.addTextListener(this);
Ok.addActionListener(this);
Cancel.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
Object Source=event.getSource();
if(Source==Ok){
Math.getText();
Science.getText();
SocialStudies.getText();
English.getText();
Elective.getText();
Elective2.getText();
do{
**message=(Math+Science+SocialStudies+English+Elective+Elective2)/6;**
system.println("Your grade average is"+message);
}while (message<0);
if(Source==Cancel){
system.ext(0);
}
}}}
The code throwing the error is marked by ** The error given is: bad operand types for binary operator '+' 抛出错误的代码标记为**给出的错误是:二元运算符'+'的错误操作数类型
Please help! 请帮忙!
Thanks 谢谢
You cannot add TextFields
together, and Java does not support overloading. 您不能将
TextFields
一起添加,Java不支持重载。 To get the value, you must first call getText()
on each TextField
, then pass it to Double.parseDouble
to convert it into a number that Java can add. 要获取该值,必须首先在每个
TextField
上调用getText()
,然后将其传递给Double.parseDouble
以将其转换为Java可以添加的数字。
You should have something like this: 你应该有这样的东西:
double gpa = (Double.parseDouble(Math.getText()) +
...
Double.parseDouble(Elective2.getText())
) / 6;
You'll need to catch NumberFormatException
if any TextField
contains text that can't be converted into a number. 如果任何
TextField
包含无法转换为数字的文本,则需要捕获NumberFormatException
。
Then you'll need to convert the GPA into text for the TextArea
you've called message
. 然后,您需要将GPA转换为您调用
message
的TextArea
文本。
message.setText(String.valueOf(gpa));
It looks like you are trying to add string variables together - try converting them to integer values first. 看起来您正在尝试将字符串变量添加到一起 - 尝试首先将它们转换为整数值。 something like
就像是
message=(Integer.parseInt(Math)+Integer.parseInt(Science)+Integer.parseInt(English)...
Here is a link to another thread on this very subject: How to convert a String to an int in Java? 这是关于这个主题的另一个线程的链接: 如何在Java中将String转换为int?
Hope this helps you! 希望这对你有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.