预想计算器图为:
简易计算器
如何实现?
1.画出计算器框架图
2.设置button的监听事件,在监听中实现将值传入到JTextField面板上,并在触发“+”等预算符时实现运算
贴上代码画框代码 用Java里的swing和awt
import javax.swing.*; import java.awt.*; public class CaculatorWindow extends JFrame { private final String names[] = {"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"}; private JTextField textField; CaculatorWindow() { textField = new JTextField(); GridLayout gridLayout = new GridLayout(4, 4); JPanel panel = new JPanel();//定义计算器下方面板 panel.setLayout(gridLayout); JButton[] buttons = new JButton[names.length]; for (int count = 0; count