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

lsn7777x lsn7777x 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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);
}
}
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
lsn7777x
非技术区 23,404 成员 70,506 内容