import javax.swing.*;
public class Demo {
/**
* 计算器界面
* @author M4ndy
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame f = new JFrame("计算器");
f.setLayout(null);
JTextField t = new JTextField();
t.setBounds(20, 5, 250, 40);
f.add(t);
int x = 20;
int y = 55;
String[] str = {"7","8","9","+","4","5","6","-","1","2","3","*","0","C","=","."};
for(int i = 0;i<str.length;i++)
{
JButton btn = new JButton();
btn.setText(str[i]);
btn.setBounds(x, y, 60, 40);
if(x<215)
{
x+=65;
}
else
{
x = 20;
y+=45;
}
f.add(btn);
}
f.setBounds(500,200,300,300);
f.setVisible(true);
}
}
以上是全部代码,没有实现计算功能,仅仅是计算器界面,运行结果如下图所示:
代码也相对来说比较简单,后期会实现计算功能,并考虑给大家剖析代码,欢迎大家留言交流,有疑惑的地方一起解决!