如何用一段代码编一个计算器程序?
答案:1 悬赏:40 手机版
解决时间 2021-06-01 06:02
- 提问者网友:聂風
- 2021-05-31 15:58
如何用一段代码编一个计算器程序?
最佳答案
- 五星知识达人网友:深街酒徒
- 2021-05-31 17:10
import java.awt.*; import java.lang.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; import java.text.DecimalFormat; public class Calculator implements ActionListener buttonMC = new JButton("MC"); buttonMC.setForeground(new Color(255, 0, 0)); buttonMR = new JButton("MR"); buttonMR.setForeground(new Color(255, 0, 0)); buttonMS = new JButton("MS"); buttonMS.setForeground(new Color(255, 0, 0)); buttonMAdd = new JButton("M+"); buttonMAdd.setForeground(new Color(255, 0, 0)); buttonDot = new JButton("."); buttonDot.setForeground(new Color(0, 0, 255)); buttonAddAndSub = new JButton("+/-"); buttonAddAndSub.setForeground(new Color(0, 0, 255)); buttonAdd = new JButton("+"); buttonAdd.setForeground(new Color(255, 0, 0)); buttonSub = new JButton("-"); buttonSub.setForeground(new Color(255, 0, 0)); buttonMul = new JButton("*"); buttonMul.setForeground(new Color(255, 0, 0)); buttonDiv = new JButton("/"); buttonDiv.setForeground(new Color(255, 0, 0)); buttonMod = new JButton("%"); buttonMod.setForeground(new Color(0, 0, 255)); buttonSqrt = new JButton("sqrt"); buttonSqrt.setForeground(new Color(0, 0, 255)); buttonDao = new JButton("1/x"); buttonDao.setForeground(new Color(0, 0, 255)); buttonEqual = new JButton("="); buttonEqual.setForeground(new Color(255, 0, 0)); //将所有行为与监听绑定 panel3.setLayout(new GridLayout(4, 6)); panel3.add(buttonMC); buttonMC.addActionListener(this); panel3.add(button[7]); button[7].addActionListener(this); panel3.add(button[8]); button[8].addActionListener(this); panel3.add(button[9]); button[9].addActionListener(this); panel3.add(buttonDiv); buttonDiv.addActionListener(this); panel3.add(buttonSqrt); buttonSqrt.addActionListener(this); panel3.add(buttonMR); buttonMR.addActionListener(this); panel3.add(button[4]); button[4].addActi
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯