程序抛异常,菜鸟无法解决,求教

lsn7777x 2011-05-19 04:25:21
import java.awt.*;
import java.awt.event.*;

public class Calculator {
public static void main(String[] args){
new Calculator();
}
TextField [] tf = {new TextField(""),new TextField(""),new TextField("")};
int i1;
int i2;
Calculator(){
Frame f = new Frame("calculator");
Panel p1 = new Panel();
Panel p2 = new Panel();
Label [] l = {new Label("number"),new Label("number"),new Label("result")};
Button [] b = {new Button("+"),new Button("-"),new Button("*"),new Button("/")};
p1.setBackground(Color.green);
p2.setBackground(Color.cyan);
for(int i = 0;i<l.length;i++){
p1.add(l[i]);
p1.add(tf[i]);
}
for(int i = 0;i<b.length;i++){
p2.add(b[i]);
}

f.setSize(600,400);
f.setLayout(new GridLayout(2,1));
f.setBackground(Color.yellow);
f.setVisible(true);
f.add(p1);
f.add(p2);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
change(tf[0],tf[1]);
b[0].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
tf[2].setText(String.valueOf(i1+i2));
}
});
b[1].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
tf[2].setText(String.valueOf(i1-i2));
}
});
b[2].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
tf[2].setText(String.valueOf(i1*i2));
}
});
b[3].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
tf[2].setText(String.valueOf(i1/i2));
}
});
}
public void change(TextField t1,TextField t2){
String s1 = t1.getText();
String s2 = t2.getText();
i1 = Integer.parseInt(s1);
i2 = Integer.parseInt(s2);
}
}
...全文
66 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsn7777x 2011-05-19
  • 打赏
  • 举报
回复
写个判断,会不会还有这个异常,我试着这样写了一下,可以
code:
import java.awt.*;
import java.awt.event.*;

public class Calculator {
public static void main(String[] args){
new Calculator();
}
TextField [] tf = {new TextField(10),new TextField(10),new TextField(20)};
Calculator(){
Frame f = new Frame("calculator");
Panel p1 = new Panel();
Panel p2 = new Panel();
Label [] l = {new Label("integer number"),new Label("integer number"),new Label("result")};
Button [] b = {new Button("+"),new Button("-"),new Button("*"),new Button("/"),new Button("%")};
p1.setBackground(Color.green);
p1.setLayout(new GridLayout(3,2,5,40));
p2.setBackground(Color.cyan);
p2.setLayout(new FlowLayout(FlowLayout.LEFT,30,10));
for(int i = 0;i<l.length;i++){
p1.add(l[i]);
p1.add(tf[i]);
}
for(int i = 0;i<b.length;i++){
p2.add(b[i]);
}

f.setSize(600,400);
f.setLayout(new GridLayout(2,1));
f.setBackground(Color.yellow);
f.setVisible(true);
f.add(p1);
f.add(p2);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

b[0].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
String s1 = tf[0].getText();
String s2 = tf[1].getText();
int i1 = Integer.parseInt(s1);
int i2 = Integer.parseInt(s2);
tf[2].setText(String.valueOf(i1+i2));
}
});
b[1].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
String s1 = tf[0].getText();
String s2 = tf[1].getText();
int i1 = Integer.parseInt(s1);
int i2 = Integer.parseInt(s2);
tf[2].setText(String.valueOf(i1-i2));
}
});
b[2].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
String s1 = tf[0].getText();
String s2 = tf[1].getText();
int i1 = Integer.parseInt(s1);
int i2 = Integer.parseInt(s2);
tf[2].setText(String.valueOf(i1*i2));
}
});
b[3].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
String s1 = tf[0].getText();
String s2 = tf[1].getText();
int i1 = Integer.parseInt(s1);
int i2 = Integer.parseInt(s2);
tf[2].setText(String.valueOf(i1/i2));
}
});
b[4].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
String s1 = tf[0].getText();
String s2 = tf[1].getText();
int i1 = Integer.parseInt(s1);
int i2 = Integer.parseInt(s2);
tf[2].setText(String.valueOf(i1%i2));
}
});
}

}


我想问一下,为什么这样就每异常了,请高手指教。
zn85600301 2011-05-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lsn7777x 的回复:]

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integ……
[/Quote]

类型转化问题 判断是否为null 或者“” 如果成立 跳过转化为int的代码
lsn7777x 2011-05-19
  • 打赏
  • 举报
回复
那要怎么该,文本框里不想有初始值,有什么别的办法么
Mourinho 2011-05-19
  • 打赏
  • 举报
回复
38行change(tf[0],tf[1]);出错,开始的时候文本框是空的,所以不能把""转换成int,第67行
i1 = Integer.parseInt(s1);。
要学会看出错信息进行debug.
lsn7777x 2011-05-19
  • 打赏
  • 举报
回复
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at Calculator.change(Calculator.java:67)
at Calculator.<init>(Calculator.java:38)
at Calculator.main(Calculator.java:6)
wl_ldy 2011-05-19
  • 打赏
  • 举报
回复
把出错信息帖出来啊。。。

23,406

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧