请教,Awt如何实现计算器功能。比如点按钮1,文本框出现1,再点2,文本框出现12
请教,Awt如何实现计算器功能。比如点按钮1,文本框出现1,再点2,文本框出现12 。这样,我现在写出来, 点1 出现1 ,点2 就把1覆盖了,
感觉 应该很简单就解决了,但我好像走到胡同里了, 新手 见谅。麻烦大家了
只要解决这一点 应该 我就能写完整了。
最好有新手仅学的这点知识,有更简单的写法。各位大神 ,请教了
import java.awt.Button;Java AWT
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class JiSuan extends Frame implements WindowListener,ActionListener{
Label l1=new Label("编辑");
Label l2=new Label("查看");
Label l3=new Label("帮助");
TextField t1=new TextField();
Button b1=new Button("back space");
Button b2=new Button("CE");
Button b3=new Button("c");
Button b4=new Button("7");
Button b5=new Button("8");
Button b6=new Button("9");
Button b7=new Button("/");
Button b8=new Button("+");
Button b9=new Button("4");
Button b10=new Button("5");
Button b11=new Button("6");
Button b12=new Button("*");
Button b13=new Button("%");
Button b14=new Button("1");
Button b15=new Button("2");
Button b16=new Button("3");
Button b17=new Button("0");
Button b18=new Button("-");
public JiSuan(){
this.setSize(350,280);
this.setTitle("计算器");
this.setLayout(null);
this.addWindowListener(this);
this.setBackground(Color.cyan);
l1.setBounds(10, 30, 40,30);
l2.setBounds(50, 30, 40,30);
l3.setBounds(90, 30, 50, 30);
t1.setBounds(10, 60, 250, 25);
b1.setBounds(20, 100, 70, 30);
b2.setBounds(130, 100, 45, 30);
b3.setBounds(220, 100, 45, 30);
b4.setBounds(20, 140, 45, 20);
b5.setBounds(80, 140, 45, 20);
b6.setBounds(140, 140, 45, 20);
b7.setBounds(200, 140, 45, 20);
b8.setBounds(260, 140, 45, 20);
b9.setBounds(20, 170, 45, 20);
b10.setBounds(80, 170, 45, 20);
b11.setBounds(140, 170, 45, 20);
b12.setBounds(200, 170, 45, 20);
b13.setBounds(260, 170, 45, 20);
b14.setBounds(20, 200, 45, 20);
b15.setBounds(80, 200, 45, 20);
b16.setBounds(140, 200, 45, 20);
b17.setBounds(200, 200, 45, 20);
b18.setBounds(260, 200, 45, 20);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
this.add(l1);
this.add(l2);
this.add(l3);
this.add(t1);
this.add(b1);
this.add(b2);
this.add(b3);
this.add(b4);
this.add(b5);
this.add(b6);
this.add(b7);
this.add(b8);
this.add(b9);
this.add(b10);
this.add(b11);
this.add(b12);
this.add(b13);
this.add(b14);
this.add(b15);
this.add(b16);
this.add(b17);
this.add(b18);
this.show();
}
//public void a(){
//String b=b14.getLabel();
//System.out.println(b);
//}
public void actionPerformed(ActionEvent a) {
// TODO Auto-generated method stub
String c="";
String b="";
while(a.getSource()!=b8){
if(a.getSource()==b14){
b=b14.getLabel();
c=c+b;
t1.setText(c);
}else if(a.getSource()==b15){
b=b15.getLabel();
c=c+b;
t1.setText(c);
}else if(a.getSource()==b16){
b=b16.getLabel();
c=c+b;
t1.setText(c);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new JiSuan();
JiSuan j=new JiSuan();
}
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}