永发信息网

java疑难解答9

答案:1  悬赏:10  手机版
解决时间 2021-11-17 05:02
  • 提问者网友:缘字诀
  • 2021-11-17 01:10
java疑难解答9
最佳答案
  • 五星知识达人网友:北城痞子
  • 2021-11-17 01:58
public class Add {
public static void main (String []args){
new TFrame("java加法");
}
}

class TFrame extends Frame {

TextField tf1,tf2,tf3;

TFrame (String s){
super(s);
this.setLocation(200, 200);
this.setLayout(new BorderLayout());
Panel p = new Panel(new GridLayout(1,5,1,20));
tf1 = new TextField("0",5);
tf2 = new TextField("0",5);
tf3 = new TextField("0",5);
tf1.setBackground(Color.green);
tf2.setBackground(Color.green);
tf3.setBackground(Color.green);
Label lb = new Label("+",Label.CENTER);
lb.setBackground(Color.lightGray);
Button bt = new Button("=");
bt.setBackground(Color.gray);
bt.addActionListener(new Action(this));
p.add(tf1); p.add(lb); p.add(tf2); p.add(bt); p.add(tf3);
add(p,BorderLayout.NORTH);
this.addWindowListener(new Close());
pack();
setVisible(true);
}
}

class Close extends WindowAdapter {

@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

}

class Action implements ActionListener {
TFrame f = null;
Action (TFrame f){
this.f = f;
}
public void actionPerformed (ActionEvent e){
int n1 = Integer.parseInt(f.tf1.getText());
int n2 = Integer.parseInt(f.tf2.getText());
f.tf3.setText(String.valueOf("" + (n1+n2)));
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯