package calc;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class calc1 extends JFrame {
public JButton plus;
public JButton minus;
public JButton multiply;
public JButton divide;
public JTextField num1;
public JTextField num2;
public JTextField res;
public long numb1;
public long numb2;
public long result;
public String boob;
public String boob2;
public calc1(){
setSize(400,400);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
这是问题我试图从textfield'num1'获取值,将其转换为int / long / double,这样我就可以在我的听众中使用数学函数了。
num1 = new JTextField("Enter First Number:");
boob = num1.getText();
numb1 = Long.parseLong(boob);
add(num1);
这是问题我试图从textfield'num1'获取值,将其转换为int / long / double,这样我就可以在我的听众中使用数学函数了。
num2 = new JTextField("Enter Second Number:");
boob2 = num2.getText();
numb2 = Long.parseLong(boob2);
add(num2);
plus = new JButton("+");
add(plus);
minus = new JButton("-");
add(minus);
multiply = new JButton("*");
add(multiply);
divide = new JButton(":");
add(divide);
listener1 boo1 = new listener1();
listener2 boo2 = new listener2();
listener3 boo3 = new listener3();
listener4 boo4 = new listener4();
divide.addActionListener(boo1);
multiply.addActionListener(boo2);
minus.addActionListener(boo3);
plus.addActionListener(boo4);
}
public class listener1 implements ActionListener{
public void actionPerformed(ActionEvent boo1){
result = numb1/numb2;
String ahmood = String.valueOf(result);
res = new JTextField(ahmood);
add(res);
}
}
public class listener2 implements ActionListener{
public void actionPerformed(ActionEvent boo2){
result = numb1*numb2;
String ahmood = String.valueOf(result);
res = new JTextField(ahmood);
add(res);
}
}
public class listener3 implements ActionListener{
public void actionPerformed(ActionEvent boo3){
result = numb1-numb2;
String ahmood = String.valueOf(result);
res = new JTextField(ahmood);
add(res);
}
}
public class listener4 implements ActionListener{
public void actionPerformed(ActionEvent boo4){
result = numb1+numb2;
String ahmood = String.valueOf(result);
res = new JTextField(ahmood);
add(res);
}
}
public static void main(String args[]){
calc1 joss = new calc1();
joss.setVisible(true);
}
}
我尝试了很多方法,最后我得到了许多冗余变量。这是我不断得到的错误。
Exception in thread "main" java.lang.NumberFormatException: For input string: "Enter First Number:"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at calc.calc1.<init>(calc1.java:37)
at calc.calc1.main(calc1.java:135)
答案 0 :(得分:0)
您创建使用值"Enter some Number:"
初始化的输入文本字段,然后立即继续获取值(提示:您刚刚传递的值),并将其转换为Long
。当然会抛出一个NumberFormatException
。
你应该做两件事:
JLabel
中。 如果您仍然无法找到自己的方式,请尝试只使用一个文本字段的较小程序,使其正常工作,并以此为基础。