我如何从 TextField 获取输入?
How do i get the input from TextField?
我正在尝试使用 Netbeans 制作一个简单的计算器 GUI。
如何从 angka1
、angka2
和 operator1
输入整数和字符串,以便它们可以在 private void btnHitungMouseClicked(java.awt.event.MouseEvent evt)
中使用
private void angka1ActionPerformed(java.awt.event.ActionEvent evt) {
nilai1=Integer.parseInt(angka1.getText());
}
private void angka2ActionPerformed(java.awt.event.ActionEvent evt) {
nilai2=Integer.parseInt(angka2.getText());
}
private void operator1ActionPerformed(java.awt.event.ActionEvent evt) {
operator=operator1.getText();
}
private void btnHitungMouseClicked(java.awt.event.MouseEvent evt) {
double jawaban=0;
if(operator=="+")
jawaban=nilai1+nilai2;
else if(operator=="-")
jawaban=nilai1-nilai2;
else if(operator=="*")
jawaban=nilai1*nilai2;
else if(operator=="/")
jawaban=nilai1/nilai2;
String hasil=Double.toString(jawaban);
txtHasil.setText(hasil);
这是我要制作的 GUI:
如您所见,当我在上面的 TextField 中键入数字和运算符时,work.I 似乎并不期望 GUI 在 TextField 中给出正确的结果。请帮忙。
试试这个;
private void btnHitungMouseClicked(java.awt.event.MouseEvent evt) {
nilai1=Integer.parseInt(angka1.getText());
nilai2=Integer.parseInt(angka2.getText());
operator=operator1.getText();
double jawaban=0;
if(operator=="+")
jawaban=nilai1+nilai2;
else if(operator=="-")
jawaban=nilai1-nilai2;
else if(operator=="*")
jawaban=nilai1*nilai2;
else if(operator=="/")
jawaban=nilai1/nilai2;
else{}
String hasil=Double.toString(jawaban);
txtHasil.setText(hasil);
operator=='+' 不比较字符串。它检查对象。
通过右键单击 Hitung 按钮创建 MouseClicked 事件 Events>Mouse>MouseClicked
private void HitungMouseClicked(java.awt.event.MouseEvent evt) {
nilai1 = Integer.parseInt(angka1.getText());
nilai2=Integer.parseInt(angka2.getText());
operator=operator1.getText();
double jawaban=0;
if(operator.equalsIgnoreCase("+"))
jawaban=nilai1+nilai2;
else if(operator.equalsIgnoreCase("-"))
jawaban=nilai1-nilai2;
else if(operator.equalsIgnoreCase("*"))
jawaban=nilai1*nilai2;
else if(operator.equalsIgnoreCase("/"))
jawaban=nilai1/nilai2;
String hasil=Double.toString(jawaban);
txtHasil.setText(hasil);
}
我正在尝试使用 Netbeans 制作一个简单的计算器 GUI。
如何从 angka1
、angka2
和 operator1
输入整数和字符串,以便它们可以在 private void btnHitungMouseClicked(java.awt.event.MouseEvent evt)
private void angka1ActionPerformed(java.awt.event.ActionEvent evt) {
nilai1=Integer.parseInt(angka1.getText());
}
private void angka2ActionPerformed(java.awt.event.ActionEvent evt) {
nilai2=Integer.parseInt(angka2.getText());
}
private void operator1ActionPerformed(java.awt.event.ActionEvent evt) {
operator=operator1.getText();
}
private void btnHitungMouseClicked(java.awt.event.MouseEvent evt) {
double jawaban=0;
if(operator=="+")
jawaban=nilai1+nilai2;
else if(operator=="-")
jawaban=nilai1-nilai2;
else if(operator=="*")
jawaban=nilai1*nilai2;
else if(operator=="/")
jawaban=nilai1/nilai2;
String hasil=Double.toString(jawaban);
txtHasil.setText(hasil);
这是我要制作的 GUI:
试试这个;
private void btnHitungMouseClicked(java.awt.event.MouseEvent evt) {
nilai1=Integer.parseInt(angka1.getText());
nilai2=Integer.parseInt(angka2.getText());
operator=operator1.getText();
double jawaban=0;
if(operator=="+")
jawaban=nilai1+nilai2;
else if(operator=="-")
jawaban=nilai1-nilai2;
else if(operator=="*")
jawaban=nilai1*nilai2;
else if(operator=="/")
jawaban=nilai1/nilai2;
else{}
String hasil=Double.toString(jawaban);
txtHasil.setText(hasil);
operator=='+' 不比较字符串。它检查对象。
通过右键单击 Hitung 按钮创建 MouseClicked 事件 Events>Mouse>MouseClicked
private void HitungMouseClicked(java.awt.event.MouseEvent evt) {
nilai1 = Integer.parseInt(angka1.getText());
nilai2=Integer.parseInt(angka2.getText());
operator=operator1.getText();
double jawaban=0;
if(operator.equalsIgnoreCase("+"))
jawaban=nilai1+nilai2;
else if(operator.equalsIgnoreCase("-"))
jawaban=nilai1-nilai2;
else if(operator.equalsIgnoreCase("*"))
jawaban=nilai1*nilai2;
else if(operator.equalsIgnoreCase("/"))
jawaban=nilai1/nilai2;
String hasil=Double.toString(jawaban);
txtHasil.setText(hasil);
}