import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class JSQ extends WindowAdapter implements MouseListener{
Frame fr = new Frame("计算器_2018001483_高梦阳");
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
TextField tf = new TextField("0"); // 建立显示屏
Button b1 = new Button("1"); // 构建数字键
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Button b0 = new Button("0");
Button badd = new Button("+"); //构建符号
Button bsub = new Button("-");
Button bmul = new Button("x");
Button bdiv = new Button("÷");
Button bequ = new Button("=");
Button bac = new Button("AC");
Button bc = new Button("C");
Button bdot = new Button (".");
double intNum1 = 0.0; //设置三个存储空间
double intNum2 = 0;
double intTotal = 0; //得数存储空间
String strNum1 = "";
String strNum2 = "";
String strID = "";
public void go(){
fr.setLayout(gbl); // 设置布局管理器
tf.setFont(new Font("Times New Roman", Font.PLAIN, 80)); //对显示器设置(定义字体、歪斜程度、字体大小(后续还需要修改))
gbc.fill = GridBagConstraints.BOTH;
//第1行 ,显示屏
gbc.gridwidth = GridBagConstraints.REMAINDER; // 宽度从头到尾都填充一样的(全大写单词)
gbc.gridheight = 1; // 高度一个单元格,向下吃或者向后吃时,1变成2
gbc.insets = new Insets(0, 0, 0, 0); // 左上角与单元格左上角紧贴在一起没有缝 左上角对齐
gbc.weightx = 1; // 控件与单元格比较大小
gbc.weighty = 1; // 同上
gbc.gridx = 0; //第1列
gbc.gridy = 0; //第1行
addComponent(tf); //填充显示器
//第2行
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 0; //第1列
gbc.gridy = 1; //第2行
addComponent(b7);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 1; //第2列
gbc.gridy = 1; //第2行
addComponent(b8);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 2; //第3列
gbc.gridy = 1; //第2行
addComponent(b9);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 3; //第4列
gbc.gridy = 1; //第2行
addComponent(badd);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 4; //第5列
gbc.gridy = 1; //第2行
addComponent(bac);
//第3行
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 0; //第1列
gbc.gridy = 2; //第3行
addComponent(b4);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 1; //第2列
gbc.gridy = 2; //第3行
addComponent(b5);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 2; //第3列
gbc.gridy = 2; //第3行
addComponent(b6);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 3; //第4列
gbc.gridy = 2; //第3行
addComponent(bsub);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 4; //第5列
gbc.gridy = 2; //第3行
addComponent(bc);
//第4行
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 0; //第1列
gbc.gridy = 3; //第4行
addComponent(b1);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 1; //第2列
gbc.gridy = 3; //第4行
addComponent(b2);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 2; //第3列
gbc.gridy = 3; //第4行
addComponent(b3);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 3; //第4列
gbc.gridy = 3; //第4行
addComponent(bmul);
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 4; //第5列
gbc.gridy = 3; //第4行
addComponent(bequ);
//第5行
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 0; //第1列
gbc.gridy = 4; //第5行
addComponent(b0);
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 2;
gbc.weighty = 1;
gbc.gridx = 1; //第2列
gbc.gridy = 4; //第5行
addComponent(bdot);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 3; //第4列
gbc.gridy = 4; //第5行
addComponent(bdiv);
fr.addWindowListener(this);
//设置监听 数字键和运算键
b1.addMouseListener(this);
b2.addMouseListener(this);
b3.addMouseListener(this);
b4.addMouseListener(this);
b5.addMouseListener(this);
b6.addMouseListener(this);
b7.addMouseListener(this);
b8.addMouseListener(this);
b9.addMouseListener(this);
b0.addMouseListener(this);
badd.addMouseListener(this);
bsub.addMouseListener(this);
bmul.addMouseListener(this);
bdiv.addMouseListener(this);
bequ.addMouseListener(this);
bac.addMouseListener(this);
bc.addMouseListener(this);
//小数点点击事件
bdot.addMouseListener(this);
fr.setSize(600, 400);
fr.setVisible(true);
}
public void addComponent(Component c){
gbl.setConstraints(c, gbc);
fr.add(c);
}
public void windowClosing(WindowEvent e){
fr.dispose();
}
// 设置五个鼠标动作
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){} //进入
public void mouseExited(MouseEvent e){} //退出
public void mouseClicked(MouseEvent e){
if(e.getSource() == b1){
if(strID.equals("")){
strNum1 += 1;
strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
tf.setText(strNum1);
}else{
strNum2 += 1;
strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
tf.setText(strNum2);
}
}
if(e.getSource() == b2){
if(strID.equals("")){
strNum1 += 2;
strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
tf.setText(strNum1);
}else{
strNum2 += 2;
strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
tf.setText(strNum2);
}
}
if(e.getSource() == b3){
if(strID.equals("")){
strNum1 += 3;
strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
tf.setText(strNum1);
}else{
strNum2 += 3;
strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
tf.setText(strNum2);
}
}
if(e.getSource() == b4){
if(strID.equals("")){
strNum1 += 4;
strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
tf.setText(strNum1);
}else{
strNum2 += 4;
strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
tf.setText(strNum2);
}
}
if(e.getSource() == b5){
if(strID.equals("")){
strNum1 += 5;
strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
tf.setText(strNum1);
}else{
strNum2 += 5;
strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
tf.setText(strNum2);
}
}
if(e.getSource() == b6){
if(strID.equals("")){
strNum1 += 6;
strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
tf.setText(strNum1);
}else{
strNum2 += 6;
strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
tf.setText(strNum2);
}
}
if(e.getSource() == b7){
if(strID.equals("")){
strNum1 += 7;
strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
tf.setText(strNum1);
}else{
strNum2 += 7;
strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
tf.setText(strNum2);
}
}
if(e.getSource() == b8){
if(strID.equals("")){
strNum1 += 8;
strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
tf.setText(strNum1);
}else{
strNum2 += 8;
strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
tf.setText(strNum2);
}
}
if(e.getSource() == b9){
if(strID.equals("")){
strNum1 += 9;
strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
tf.setText(strNum1);
}else{
strNum2 += 9;
strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
tf.setText(strNum2);
}
}
if(e.getSource() == b0){
if(strID.equals("")){
strNum1 += 0;
strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
tf.setText(strNum1);
}else{
strNum2 += 0;
strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
tf.setText(strNum2);
}
}
if(e.getSource() == badd){
if(!strID.equals("") && !"".equals(strNum1) && !"".equals(strNum2)){
Double num1 = Double.parseDouble(strNum1);
Double num2 = Double.parseDouble(strNum2);
Double total = 0.0;
if(strID.equals("+")){
total = num1 + num2;
}else if(strID.equals("-")){
total = num1 - num2;
}else if(strID.equals("*")){
total = num1 * num2;
}else if(strID.equals("/")){
total = num1 / num2;
}
// 处理double失真
DecimalFormat df = new DecimalFormat("0.000000");
String temp = df.format(total);
while(true){
if( temp.lastIndexOf("0") > temp.lastIndexOf(".")
&& temp.lastIndexOf(".") != -1
&& (temp.lastIndexOf("0") == temp.length() -1 || temp.lastIndexOf(".") == temp.length() -1)){
temp = temp.substring(0,temp.length()-1);
}else{
break;
}
}
strNum1 = temp.substring(0, Math.min(temp.length(), 10));
tf.setText(strNum1);
strNum2="";
strID = "";
}
strID = "+";
}
if(e.getSource() == bsub){
if(!strID.equals("") && !"".equals(strNum1) && !"".equals(strNum2)){
Double num1 = Double.parseDouble(strNum1);
Double num2 = Double.parseDouble(strNum2);
Double total = 0.0;
if(strID.equals("+")){
total = num1 + num2;
}else if(strID.equals("-")){
total = num1 - num2;
}else if(strID.equals("*")){
total = num1 * num2;
}else if(strID.equals("/")){
total = num1 / num2;
}
// 处理double失真
DecimalFormat df = new DecimalFormat("0.000000");
String temp = df.format(total);
while(true){
if( temp.lastIndexOf("0") > temp.lastIndexOf(".")
&& temp.lastIndexOf(".") != -1
&& (temp.lastIndexOf("0") == temp.length() -1 || temp.lastIndexOf(".") == temp.length() -1)){
temp = temp.substring(0,temp.length()-1);
}else{
break;
}
}
strNum1 = temp.substring(0, Math.min(temp.length(), 10));
tf.setText(strNum1);
strNum2="";
strID = "";
}
strID = "-";
}
if(e.getSource() == bmul){
if(!strID.equals("") && !"".equals(strNum1) && !"".equals(strNum2)){
Double num1 = Double.parseDouble(strNum1);
Double num2 = Double.parseDouble(strNum2);
Double total = 0.0;
if(strID.equals("+")){
total = num1 + num2;
}else if(strID.equals("-")){
total = num1 - num2;
}else if(strID.equals("*")){
total = num1 * num2;
}else if(strID.equals("/")){
total = num1 / num2;
}
// 处理double失真
DecimalFormat df = new DecimalFormat("0.000000");
String temp = df.format(total);
while(true){
if( temp.lastIndexOf("0") > temp.lastIndexOf(".")
&& temp.lastIndexOf(".") != -1
&& (temp.lastIndexOf("0") == temp.length() -1 || temp.lastIndexOf(".") == temp.length() -1)){
temp = temp.substring(0,temp.length()-1);
}else{
break;
}
}
strNum1 = temp.substring(0, Math.min(temp.length(), 10));
tf.setText(strNum1);
strNum2="";
strID = "";
}
strID = "*";
}
if(e.getSource() == bdiv){
if(!strID.equals("") && !"".equals(strNum1) && !"".equals(strNum2)){
Double num1 = Double.parseDouble(strNum1);
Double num2 = Double.parseDouble(strNum2);
Double total = 0.0;
if(strID.equals("+")){
total = num1 + num2;
}else if(strID.equals("-")){
total = num1 - num2;
}else if(strID.equals("*")){
total = num1 * num2;
}else if(strID.equals("/")){
total = num1 / num2;
}
// 处理double失真
DecimalFormat df = new DecimalFormat("0.000000");
String temp = df.format(total);
while(true){
if( temp.lastIndexOf("0") > temp.lastIndexOf(".")
&& temp.lastIndexOf(".") != -1
&& (temp.lastIndexOf("0") == temp.length() -1 || temp.lastIndexOf(".") == temp.length() -1)){
temp = temp.substring(0,temp.length()-1);
}else{
break;
}
}
strNum1 = temp.substring(0, Math.min(temp.length(), 10));
tf.setText(strNum1);
strNum2="";
strID = "";
}
strID = "/";
}
//设置等于运算
if(e.getSource() == bequ && !"".equals(strNum1) && !"".equals(strNum2)){
Double num1 = Double.parseDouble(strNum1);
Double num2 = Double.parseDouble(strNum2);
Double total = 0.0;
if(strID.equals("+")){
total = num1 + num2;
}else if(strID.equals("-")){
total = num1 - num2;
}else if(strID.equals("*")){
total = num1 * num2;
}else if(strID.equals("/")){
total = num1 / num2;
}
// 处理double失真
DecimalFormat df = new DecimalFormat("0.000000");
String temp = df.format(total);
while(true){
if( temp.lastIndexOf("0") > temp.lastIndexOf(".")
&& temp.lastIndexOf(".") != -1
&& (temp.lastIndexOf("0") == temp.length() -1 || temp.lastIndexOf(".") == temp.length() -1)){
temp = temp.substring(0,temp.length()-1);
}else{
break;
}
}
strNum1 = temp.substring(0, Math.min(temp.length(), 10));
tf.setText(strNum1);
strNum2="";
strID = "";
}
if(e.getSource() == bac){
strNum1 = "";
strNum2 = "";
strID = "";
tf.setText("0");
}
if(e.getSource() == bc){
if(strID.equals("")){
strNum1 = "";
tf.setText("0");
}else{
strNum2 = "";
tf.setText("0");
}
}
if(e.getSource() == bdot){
if(strID.equals("")){
if(!strNum1.contains(".")){
if("".equals(strNum1)){
strNum1 += 0 ;
}
strNum1+=".";
strNum1 = strNum1.substring(0, Math.min(strNum1.length(), 10));
tf.setText(strNum1);
}
}else{
if(!strNum2.contains(".")){
if("".equals(strNum2)){
strNum2 += 0;
}
strNum2+=".";
strNum2 = strNum2.substring(0, Math.min(strNum2.length(), 10));
tf.setText(strNum2);
}
}
}
}
public static void main(String args[]){
new JSQ().go();
}
}
java-计算器
最新推荐文章于 2024-10-01 18:51:18 发布