实验进度1(5.10版本)

目前还是处于学习状态,跟着步骤来

  1. 学习控件和可视化编程
    本来是Calculator 简单计算器
    一说起计算器,我不禁想起了上学期学Java,实验课大作业就是完成一个计算器
    现在又花了一段时间复习(好久没用感觉连myeclipse都快不会用了)
    然后我个人设计的比较复杂
    大概是这个样子的:在这里插入图片描述

  2. 连接数据库,实现注册登录
    Login也就是简单的用户登录窗口
    第一步:在数据库中建表,保存信息(用户名和登录密码)
    在这里插入图片描述

下面是基于Java的程序设计
第二步:编写用户注册窗口以及登录窗口
第三步:封装操作类

。。这部分目前还在写,bug不断还是没办法拿出来emmm
感觉越写越多。[惨.jpg]

附录
——————
计算器代码部分

import java.awt.event.*;
import java.lang.Math;
import java.awt.*;
import javax.swing.*;
public class Counter extends JFrame implements ActionListener//布局  接收操作事件的侦听器接口
{
 JPanel panel0 = new JPanel();
 JPanel panel1 = new JPanel();
 JPanel panel2 = new JPanel();
 JPanel panel3 = new JPanel();
 JPanel panel4 = new JPanel();
 JPanel panel5 = new JPanel();
 JPanel panel6 = new JPanel();
 JButton[] button=new JButton[20];//按钮数组
 JTextField text1 = new JTextField("",10);
 JTextField text2 = new JTextField("",10);
 int p=0,c=0;//p是运算符标识   c是继承标识
 String s1="";//输入1
 String s2="";//输入2
 String s3="";//总输入(输出也用这个)
 double x,x1,x2;//x1和x2是s1s2的格式转换,x是结果变量
 public  Counter()//构造
 {
  super("计算器");
  this.setBounds(800, 300, 500, 400);//位置
 this.setBackground(Color.LIGHT_GRAY);
 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 this.setLayout(new GridLayout(6,1,0,3));//网格布局(行列,行列间距)
 for(int i=0;i<10;i++)//数字按钮1-9
 {
  button[i]=new JButton(""+i);
  button[i].setBackground(new Color(232,232,232));
 }
 this.add(panel0);//显示行
 panel0.setLayout(new GridLayout(1,1));
 text2.setHorizontalAlignment(JTextField.RIGHT);//右对齐
 text2.setBackground(new Color(255,255,250));
 text2.setEditable(false);
 text2.setFont(new java.awt.Font("Dialog", 0, 27));
 panel0.add(text2);
 this.add(panel1);//输入行
 panel1.setLayout(new GridLayout(1,1));
 text1.setHorizontalAlignment(JTextField.RIGHT);
 text1.setBackground(new Color(255,255,250));
 text1.setEditable(false);
 text1.setFont(new java.awt.Font("Dialog", 1, 50));
 panel1.add(text1);
 this.add(panel2);
 this.add(panel3);
 this.add(panel4);
 this.add(panel5);
 panel2.setLayout(new GridLayout(1,5,4,0));
 for(int i=7;i<10;i++)
 {
  panel2.add(button[i]);
 }
 button[18]=new JButton("←");
 button[19]=new JButton("AC");
 panel2.add(button[18]);
 panel2.add(button[19]);
 panel3.setLayout(new GridLayout(1,5,4,0));
 for(int i=4;i<7;i++)
 {
  panel3.add(button[i]);
 }
 button[16]=new JButton("+");
 button[17]=new JButton("×");
 panel3.add(button[16]);
 panel3.add(button[17]);
 panel4.setLayout(new GridLayout(1,5,4,0));
 for(int i=1;i<4;i++)
 {
  panel4.add(button[i]);
 }
 button[14]=new JButton("-");
 button[15]=new JButton("÷");
 panel4.add(button[14]);
 panel4.add(button[15]);
 panel5.setLayout(new GridLayout(1,5,4,0));
 button[10]=new JButton(".");
 button[11]=new JButton("=");
 button[12]=new JButton("^2");
 button[13]=new JButton("√");
 panel5.add(button[0]);
 panel5.add(button[10]);
 panel5.add(button[11]);
 panel5.add(button[12]);
 panel5.add(button[13]);
 button[10].setBackground(new Color(232,232,232));
 for(int i=0;i<20;i++)//键位字体
 {
  button[i].setFont(new java.awt.Font("Dialog", 0, 22));
 }
 for(int i=11;i<20;i++)
 {
  if(i<18&&i>13)//加减乘除是输入键
  {
   button[i].setBackground(new Color(200,200,200));
  }
  else//都是即时响应
      button[i].setBackground(new Color(181,181,181));
 }
 for(int i=0;i<20;i++)
 {
  button[i].addActionListener(this);
 }
 this.setVisible(true);//窗口显示
}
 public void actionPerformed(ActionEvent e)
 {
  if(e.getSource()==button[19])//归零
  {
   s1="";
   s2="";
   s3="";
   p=0;
   text1.setText("");
   text2.setText("");
  }
  for(int i=0;i<10;i++)//输入数字处理
  {
   if(e.getSource()==button[i])
   {
    if(c==1)//处理继承
    {
     s1=""+i;
     x1=Double.parseDouble(s1);
     s3=s1;
     text1.setText(s3);
     c=0;
    }
    else
    {
     
     if(p==0)
     {
      
        s1=s1+i;
        x1=Double.parseDouble(s1);
        s3=s1;
        text1.setText(s3);
     }
     else
     {
      s2=s2+i;
      x2=Double.parseDouble(s2);
      s3=s3+i;
      text1.setText(s3);
     }
    }
    }
  }
  if(e.getSource()==button[10])//输入小数点处理
  {
   if(p==0)//未加运算符
   {
       int intIndex = s1.indexOf(".");
       if(intIndex == -1)//没小数点情况
       {
           if(s1.length()==0)
           {
      s1="0.";
     }
     else
     { 
      s1=s1+".";
      s3=s1;
      text1.setText(s3);
     }
       }
   }
   else//处理s2
   {
    int intIndex = s2.indexOf(".");
       if(intIndex == -1)//没小数点情况
       {
           if(s2.length()!=0)
     { 
      s2=s2+".";
      s3=s3+".";
      text1.setText(s3);
     }
       }
   }
  }
  if(e.getSource()==button[18])//删除前一个字符
  {
   if(p==0)//删s1
   {
    if(s1.length()!=0)//s1为空时,删除键不起效
    {
     s1=s1.substring(0,s1.length()-1);
     if(s1.length()==0)
     {
      text1.setText("");
     }
     else
     {
      x1=Double.parseDouble(s1);
      s3=s1;
      text1.setText(s3);
     }
    }
   }
   else
   {
    if(s2.length()==0)//删p
    {
     s3=s3.substring(0,s3.length()-1);
     p=0;
     text1.setText(s3);
    }
    else//删s2
    {
     s2=s2.substring(0,s2.length()-1);
     if(s2.length()!=0)
     {
      x2=Double.parseDouble(s2);
     }
     s3=s3.substring(0,s3.length()-1);
     text1.setText(s3);
    }
   }
  }
  if(e.getSource()==button[16])//加
  {
   if (p==0)
   {
    s3=s3+"+";
    p=1;
    c=0;
    text1.setText(s3);
   }
  }
  if(e.getSource()==button[14])//减
  {
   if (p==0)
   {
    s3=s3+"-";
    p=2;
    c=0;
    text1.setText(s3);
   }
  }
  if(e.getSource()==button[17])//乘
  {
   if (p==0)
   {
    s3=s3+"×";
    p=3;
    c=0;
    text1.setText(s3);
   }
  }
  if(e.getSource()==button[15])//除
  {
   if (p==0)
   {
    s3=s3+"÷";
    p=4;
    c=0;
    text1.setText(s3);
   }
  }
  if(e.getSource()==button[12])//平方
  {
   if(p==0)
   {
    s3=s3+"^2";
    text2.setText(s3);
    x=x1*x1;
    x=Double.parseDouble(String.format("%.4f",x));
    s1=""+x;
    s3=s1;
    text1.setText(s3);
    x1=Double.parseDouble(s1);
    c=1;
   }
  }
  if(e.getSource()==button[13])//开方
  {
   if(p==0)
   {
    s3="√ ̄"+s3;
    text2.setText(s3);
    x=Math.sqrt(x1);
    x=Double.parseDouble(String.format("%.4f",x));
    s1=""+x;
    s3=s1;
    text1.setText(s3);
    x1=Double.parseDouble(s1);
    c=1;
   }
  }
  if(e.getSource()==button[11])//等于
  {
   if(p==0)
   {
    text2.setText(s3);
    s1="";
    s2="";
    s3="";
   }
   else
   {
    if(p==1&&s2.length()!=0)
    {
     text2.setText(s3);
     x=x1+x2;
     x=Double.parseDouble(String.format("%.4f",x));
     s3=""+x;
     text1.setText(s3);
    }
    if(p==2&&s2.length()!=0)
    {
     text2.setText(s3);
     x=x1-x2;
     x=Double.parseDouble(String.format("%.4f",x));
     s3=""+x;
     text1.setText(s3);
    }
    if(p==3&&s2.length()!=0)
    {
     text2.setText(s3);
     x=x1*x2;
     x=Double.parseDouble(String.format("%.4f",x));
     s3=""+x;
     text1.setText(s3);
    }
    if(p==4&&s2.length()!=0)
    {
     if(x2==0)
     {
      text1.setText("除数不能为零");
     }
     else
     {
      text2.setText(s3);
      x=x1/x2;
      x=Double.parseDouble(String.format("%.4f",x));
      s3=""+x;
      text1.setText(s3);
     }
    }
    p=0;
    s1=s3;
    s2="";
    x1=Double.parseDouble(s1);
    c=1;//s1继承
   }
  }
 }
 public static void main(String[] args)
 {
  new Counter();
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值