用java做一个简单的计算器
首先是做界面
先定义一些基本数据类型
JTextField text = new JTextField();
JPanel p1,p2;//上下两个面板p1和p2
JButton b[] = new JButton[16];
String s;//从文本框读的字符串
添加最上面的文本框:
p1.setLayout(new BorderLayout());
text = new JTextField();
//重新定义长度和宽度
text.setPreferredSize(new Dimension(30,30));
p1.add(text);
this.add(p1,BorderLayout.NORTH);
添加下方按钮:
p2.setLayout(new GridLayout(3,5,3,3));//网格式布局,三行五列
String str [] = {
"0","1","2","3","4","5","6","7",
"8","9","+","-","*","/",".","="};
ActionListener command=new command();
for(int i = 0;i<str.length;i++)
{
b[i] = new JButton(str[i]);
b[i