java简单模仿win10计算器

网友投稿 297 2022-12-13


java简单模仿win10计算器

本文实例为大家分享了java实现win10计算器的具体代码,供大家参考,具体内容如下

这个小demo是我上学时的远古代码(嘻嘻嘻),今天整理代码时看到的,看着以前的代码,突然感觉这些是啥?看不懂了都,而且写得也不规范。

运行一下,还是可以的,先截张图

试了一下,bug还是有的,但是可以基本的运算,有兴趣的可以试一下

代码就贴在这里:

package com.waking.call;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.Font;

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.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

/**

* 计算器小案例

* @author waking

*

*/

public class CalculatorExample {

public static void main(String[] args) {

// TODO Auto-generated method stub

CalculatorFrame calculatorFrame=new CalculatorFrame();

}

}

class CalculatorFrame extends JFrame{

public CalculatorFrame() {//构造函数

setTitle("计算器");

setSize(340,500);

CalcultorPanel calcultorPanel=new CalcultorPanel();

getContentPane().add(calcultorPanel);//给当前Frame添加面板

setLocationRelativeTo(null);//居中

setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);

}

}

class CalcultorPanel extends JPanel{//主面板

public static boolean start=true;//新数字键输入的开始

public static String result="";//保存中间结果的参数

public static String lastcommand="=";//记录上一个运算符

public static boolean firstDigit = true;//输入第一个数

public CalcultorPanel() {

setLayout(new BorderLayout());

ShowPanel showPanel=new ShowPanel();

BTNPanel btnPanel=new BTNPanel();

add(showPanel, BorderLayout.NORTH);

add(btnPanel,BorderLayout.CENTER);

}

}

class ShowPanel extends JPanel{//结果面板

public static JLabel display1;

public static JLabel display2;

public ShowPanel() {

display1=new JLabel("",SwingConstants.RIGHT);

display2=new JLabel("0",SwingConstants.RIGHT);

display1.setFont(new Font("黑体",Font.BOLD,30));

display2.setFont(new Font("黑体",Font.BOLD,40));

display1.setPreferredSize(new Dimension(300, 80));

display2.setPreferredSize(new Dimension(300, 50));

setLayout(new BorderLayout());

add(display1,BorderLayout.NORTH);

add(display2,BorderLayout.CENTER);

}

}

class BTNPanel extends JPanel{//按钮面板

public BTNPanel() {

setLayout(new GridLayout(6, 4));

InsertAciton insertAciton=new InsertAciton();

CommandAction commandAction=new CommandAction();

addButton("%", commandAction);

addButton("√", commandAction);

addButton("pow", commandAction);

addButton("1/x", commandAction);

addButton("CE", commandAction);

addButton("C", commandAction);

addButton("<<", commandAction);

addButton("/",commandAction);

addButton("7",insertAciton);

addButton("8",insertAciton);

addButton("9",insertAciton);

addButton("*",commandAction);

addButton("4",insertAciton);

addButton("5",insertAciton);

addButton("6",insertAciton);

addButton("-",commandAction);

addButton("1",insertAciton);

addButton("2",insertAciton);

addButton("3",insertAciton);

addButton("+",commandAction);

addButton("+/-",commandAction);

addButton("0",insertAciton);

addButton(".",insertAciton);

addButton("=",commandAction);

}

void addButton(String label,ActionListener actionListener) {//添加按钮

JButton button=new JButton(label);

button.setFont(new Font("黑体",Font.BOLD,15));

button.addActionListener(actionListener);

add(button);

}

}

class InsertAciton implements ActionListener{//数字键按钮事件

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(CalcultorPanel.start==true) {

//清空文本

ShowPanel.display1.setText("");

ShowPanel.display2.setText("");

CalcultorPanel.start=false;

}

if ("0123456789.".indexOf(e.getActionCommand()) >= 0) {

if (CalcultorPanel.firstDigit) {

// 输入的第一个数字

if(e.getActionCommand().equals(".")) {//如果第一个是“.”

ShowPanel.display2.setText("0.");

}else if(e.getActionCommand().equals("0")) {//如果第一个是“0”

ShowPanel.display2.setText("0");

CalcultorPanel.firstDigit=true;

return;

}

else ShowPanel.display2.setText(e.getActionCommand());

}

else if ((e.getActionCommand().equals(".")) && (ShowPanel.display2.getText().indexOf(".") < 0)) {

// 输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面

ShowPanel.display2.setText(ShowPanel.display2.getText() + ".");

}

else //if(Double.parseDouble(ShowPanel.display2.getText())!=0)

if (!e.getActionCommand().equals(".")) {

// 如果输入的不是小数点,则将数字附在结果文本框的后面

ShowPanel.display2.setText(ShowPanel.display2.getText() + e.getActionCommand());

}

// 以后输入的肯定不是第一个数字了

CalcultorPanel.firstDigit = false;

}

}

}

class CommandAction implements ActionListener{//功能事件

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

String string = e.getActionCommand();

if(string.equals("CE")) {

ShowPanel.display1.setText("");

ShowPanel.display2.setText("0");

}else if(string.equals("C")) {

CalcultorPanel.start=true;//新数字键输入的开始

CalcultorPanel.result="";//保存中间结果的参数

CalcultorPanel.lastcommand="=";//记录上一个运算符

CalcultorPanel.firstDigit = true;//输入第一个数

ShowPanel.display1.setText("");

ShowPanel.display2.setText("0");

}else if(string.equals("<<")) {//退格

ShowPanel.display1.setText("");

String text=ShowPanel.display2.getText();

int i = text.length();

if (i > 0) {

// 退格,将文本最后一个字符去掉

text = text.substring(0, i - 1);

if (text.length() == 0) {

// 如果文本没有了内容,则初始化计算器的各种值

ShowPanel.display2.setText("0");

CalcultorPanel.firstDigit = true;

CalcultorPanel.lastcommand = "=";

} else {

// 显示新的文本

ShowPanel.display2.setText(text);

}

}

} else if(string.equals("+/-")) {

String a1=ShowPanel.display2.getText();

ShowPanel.display1.setText("");

if(a1.indexOf("-")==-1) {

if(Double.parseDouble(a1)!=0)

ShowPanel.display2.setText("-"+ShowPanel.display2.getText());

}

else {

ZeroText(Double.parseDouble(a1)*-1);

}

CalcultorPanel.result=a1;

}else{

CalcultorPanel.start=true;//新数字键的输入

if(CalcultorPanel.lastcommand.equals("=")) //做一次运算

{

CalcultorPanel.result=ShowPanel.display2.getText(); //获取操作数1

}

else {

if (CalcultorPanel.lastcommand.equals("%")) {

// 百分号运算,除以100

double a1=Double.parseDouble(CalcultorPanel.result);

a1= a1 / 100;

if (CalcultorPanel.result=="0.") //去除0.问题

ShowPanel.display1.setText(0+"/100"+"=");

else ShowPanel.display1.setText(CalcultorPanel.result+"/100=");

ZeroText(a1);

CalcultorPanel.result=a1+"";

}else if (CalcultorPanel.lastcommand.equals("√")) {

// 平方根运算

double a1=Double.parseDouble(CalcultorPanel.result);

a1 = Math.sqrt(a1);

if(CalcultorPanel.result=="0.")//去除0.问题

ShowPanel.display1.setText("√("+0+")"+"=");

rwcdcqLzz else ShowPanel.display1.setText("√"+"("+CalcultorPanel.result+")=");

ZeroText(a1);

CalcultorPanel.result=a1+"";

}else if(CalcultorPanel.lastcommand.equals("pow")) {

double a1=Double.parseDouble(CalcultorPanel.result);

double a2=Double.parseDouble(ShowPanel.display2.getText());

double s=Math.pow(a1,a2);

if (CalcultorPanel.result=="0.") {//去除0.问题

if(ShowPanel.display2.getText()=="0.")

ShowPanel.display1.setText("pow("+0+","+0+")"+"=");

else ShowPanel.display1.setText("pow("+0+","+ShowPanel.display2.getText()+")"+"=");

}else if(ShowPanel.display2.getText()=="0.") {

ShowPanel.display1.setText("pow("+CalcultorPanel.result+","+0+")"+"=");

}else

ShowPanel.display1.setText("pow"+"("+CalcultorPanel.result+","+ShowPanel.display2.getText()+")=");

ZeroText(s);

CalcultorPanel.result=a1+"";

}else if (CalcultorPanel.lastcommand.equals("1/x")) {

// 倒数运算

double a1=Double.parseDouble(CalcultorPanel.result);

if (a1 == 0.0) {

// 操作不合法

ShowPanel.display2.setText("零没有倒数");

} else {

a1 = 1 / a1;

ShowPanel.display1.setText("1"+"/"+CalcultorPanel.result+"=");

ZeroText(a1);

CalcultorPanel.result=a1+"";

}

}

else if(CalcultorPanel.lastcommand.equals("+")) {

double a1=Double.parseDouble(CalcultorPanel.result);

double a2=Double.parseDouble(ShowPanel.display2.getText());

double s=a1+a2;

if (CalcultorPanel.result=="0.") {//去除0.问题

if(ShowPanel.display2.getText()=="0.")

ShowPanel.display1.setText(0+"+"+0+"=");

else ShowPanel.display1.setText(0+"+"+ShowPanel.display2.getText()+"=");

}else if(ShowPanel.display2.getText()=="0.") {

ShowPanel.display1.setText(CalcultorPanel.result+"+"+0+"=");

}else

ShowPanel.display1.setText(CalcultorPanel.result+"+"+ShowPanel.display2.getText()+"=");

ZeroText(s);

CalcultorPanel.result=s+"";

}

else if(CalcultorPanel.lastcommand.equals("-")) {

double a1=Double.parseDouble(CalcultorPanel.result);

double a2=Double.parseDouble(ShowPanel.display2.getText());

double s=a1-a2;

if (CalcultorPanel.result=="0.") {//去除0.问题

if(ShowPanel.display2.getText()=="0.")

ShowPanel.display1.setText(0+"-"+0+"=");

else ShowPanel.display1.setText(0+"-"+ShowPanel.display2.getText()+"=");

}else if(ShowPanel.display2.getText()=="0.") {

ShowPanel.display1.setText(CalcultorPanel.result+"-"+0+"=");

}else

ShowPanel.display1.setText(CalcultorPanel.result+"-"+ShowPanel.display2.getText()+"=");

ZeroText(s);

CalcultorPanel.result=s+"";

}else if(CalcultorPanel.lastcommand.equals("*")) {

double a1=Double.parseDouble(CalcultorPanel.result);

double a2=Double.parseDouble(ShowPanel.display2.getText());

double s=a1*a2;

if (CalcultorPanel.result=="0.") {//去除0.问题

if(ShowPanel.display2.getText()=="0.")

ShowPanel.display1.setText(0+"*"+0+"=");

else ShowPanel.display1.setText(0+"*"+ShowPanel.display2.getText()+"=");

}else if(ShowPanel.display2.getText()=="0.") {

ShowPanel.display1.setText(CalcultorPanel.result+"*"+0+"=");

}else

ShowPanel.display1.setText(CalcultorPanel.result+"*"+ShowPanel.display2.getText()+"=");

ZeroText(s);

CalcultorPanel.result=s+"";

}else if (CalcultorPanel.lastcommand.equals("/")) {

// 除法运算

// 如果当前结果文本框中的值等于0

double a1=Double.parseDouble(CalcultorPanel.result);

double a2=Double.parseDouble(ShowPanel.display2.getText());

if (a2 == 0.0) {

// 操作不合法

ShowPanel.display2.setText("除数不能为零");

} else {

double s=a1/a2;

if(CalcultorPanel.result=="0.")//去除0.问题

ShowPanel.display1.setText(0+"/"+ShowPanel.display2.getText()+"=");

else ShowPanel.display1.setText(CalcultorPanel.result+"/"+ShowPanel.display2.getText()+"=");

ZeroText(s);

CalcultorPanel.result=s+"";

}

}

} CalcultorPanel.lastcommand=e.getActionCommand();//获取当前的运算符

CalcultorPanel.firstDigit = true;

}

}

public void ZeroText(double s) {

long t1;

double t2;

t1 = (long) s;

t2 = s-t1;

if (t2 == 0) {

ShowPanel.display2.setText(String.valueOf(t1));

} else {

ShowPanel.display2.setText(String.valueOf(s));

}

}

}

不要介意我把类写在一起,因为那时我为什么写在一起,我现在也不知道了呀!

有兴趣的你可以研究研究,感谢您的观看。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Jenkins配置自动发送邮件过程图解
下一篇:SpringBoot整合Dubbo zookeeper过程解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~