Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3593217
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Java

2020-12-29 11:52:36

代码实现(本需求完全有个人实现):

import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;

@SuppressWarnings("serial")class MyComputerWin extends JFrame implements ActionListener {

JTextField expressText;  //创建一个单行文本框引用

JPanel numPanel, operPanel;// 创建两个面板引用

JPanel panel; //创建一个面板用于存放文本框与"="按钮。

//创建按钮数组用于存放按钮,并定义数组的长度

JButton[] numBtn = new JButton[12];

JButton[] operBtn = new JButton[6];

String express = "";

public void init() {

expressText = new JTextField(1); //实例化单行文本框对象

//实例化面板对象

numPanel = new JPanel();

operPanel = new JPanel();

panel = new JPanel();

//设置面板为网格布局(GridLayout),  注意: 默认是流动布局(FlowLayout)

panel.setLayout(new BorderLayout());

numPanel.setLayout(new GridLayout(4, 3));

operPanel.setLayout(new GridLayout(5, 1));

//将数字按钮添加到数字面板上

for (int i = 0; i < numBtn.length; i++) {

//创建数字按钮

if(i>=0&&i<=2) {

numBtn[i] = new JButton(""+(7+i));

}else if(i>=3&&i<=5) {

numBtn[i] = new JButton(""+(i+1));

}else if(i>=6&&i<=8) {

numBtn[i] = new JButton(""+(i-5));

}else {

numBtn[9] = new JButton(""+"00");

numBtn[10] = new JButton(""+"0");

numBtn[11] = new JButton(""+"^");

numPanel.add(numBtn[i]);

numBtn[i].addActionListener(this);

//设置符号按钮

operBtn[0] = new JButton("%");

operBtn[1] = new JButton("/");

operBtn[2] = new JButton("*");

operBtn[3] = new JButton("-");

operBtn[4] = new JButton("+");

operBtn[5] = new JButton("=");

//将符号按钮添加到符号面板上

for (int i = 0; i < operBtn.length-1; i++) {

operPanel.add(operBtn[i]);

operBtn[i].addActionListener(this);

panel.add(expressText);

panel.add(operBtn[5],BorderLayout.EAST);

operBtn[5].addActionListener(this);

//将文本框添加到窗口上,并设置为边界布局

this.add(panel, BorderLayout.NORTH);

//将面板添加到窗口上

this.add(numPanel, BorderLayout.CENTER);

public MyComputerWin(String title) {

super(title);

init();

this.setVisible(true);

this.setSize(300, 300);

this.setLocationRelativeTo(null);

this.expressText.setBackground(Color.YELLOW);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

@Override

public void actionPerformed(ActionEvent e) {

String s = e.getActionCommand();  //获得发生该事件的相关命令,在这里就是获得按钮事件上的数字。

express += s; //将获得的命令保存起来

expressText.setText(express); //将获得的命令放入文本框中。

//因为s代表的是单个按钮事件上的命令,所以这里判断s获得的命令是不是"="

if ("=".equals(s)) {

String[] nums = express.split("\\p{Punct}");// 5+6= 正则表达式 \\p{Punct}代表的是标点符号,这里就是通过标点符号(+,-,*,/.....)来进行分割,然后得到我们想要计算的数字。

   String[] opers =express.split("\\d+"); //利用正则表达式中的多位数字来进行分解。然后得到我们想要的运算符号

String oper=opers[1]; //定义一个String类型的变量存储符号

double num1=Integer.parseInt(nums[0]);

double num2=Integer.parseInt(nums[1]);

   double result=1;

   switch(oper) {

   case "+":express+=num1+num2;break;

   case "-":express+=num1-num2;break;

   case "*":express+=num1*num2;break;

   case "/":if(num2!=0) {

    express+=num1/num2;

}else {

express+="分母为0,不能计算!!";

} break;

case "%":express+=num1/100*num2;break;

case "^":for(int i=0;i

result*=num1;

express+=result;break;

//express+=result;

expressText.setText(express);

express="";  //计算完之后将数据清空

public class MyComputer {

public static void main(String[] args) {

new MyComputerWin("我的计算器");

 

阅读(17033) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~