这是3月中旬写的计算器,通过与之前刚刚学习的登录界面相结合,通过登录界面进入计算器。
首先,谈一谈感想,对于计算器模块,基本上都是自己在冥思苦想,花了一天弄出来的,真心不容易啊,界面还是相当好看的。
计算器的button很多,当时采用的是空布局,对每个button的位置和内容是一个个设置的,十分的繁琐。而这可以通过用面板来实现,比如数字键盘构造一个网格布局的面板。
而且,计算器只实现了加减乘除,取余,倒数,取反,根号这些功能,对于实数只能输出数字,而不能进行运算。并且,整数的运算第二个数只能是个位数,当然,这个可以用将运算符及前面的数清空来实现。
//登录界面 (仿QQ界面) import java.awt.Color; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class LoginFrame { public void last(){ //创建窗体 JFrame jf = new JFrame(); //设置边框 jf.setTitle("只为不凡而来!"); jf.setDefaultCloseOperation(3); jf.setLayout(null); //设置窗体颜色 jf.setBackground(Color.BLUE); //设置窗体位置及大小 jf.setBounds(490,240,380,290); jf.setResizable(false);//设置禁止用户调整窗体的大小 JLabel label1 = new JLabel("帐号"); jf.add(label1); label1.setBounds(110,120,30,22); JTextField name= new JTextField (); jf.add(name); name.setBounds(150,120,150,22); JLabel label2 = new JLabel("密码"); jf.add(label2); label2.setBounds(110,150,30,22); JPasswordField password= new JPasswordField(10); jf.add(password); password.setBounds(150,150,150,22); JButton loginbutton = new JButton("登录"); jf.add(loginbutton); loginbutton.setBounds(110,215,150,30); JCheckBox box = new JCheckBox("记住密码"); jf.add(box); box.setBounds(110,180,80,15); JCheckBox box1= new JCheckBox("自动登录"); jf.add(box1); box1.setBounds(200,180,80,15); ImageIcon icon1 = new ImageIcon("img/qizai.jpg"); JLabel imgLabel2 = new JLabel(icon1); imgLabel2.setBounds(20,120,80,80); jf.add(imgLabel2); jf.setVisible(true); ImageIcon icon = new ImageIcon("img/天空.jpg"); JLabel imgLabel = new JLabel(icon); imgLabel.setBounds(0,0,400,300); jf.add(imgLabel); LoginListener m = new LoginListener (name,password); loginbutton.addActionListener(m); jf.setVisible(true); } public static void main(String[] args) { LoginFrame f1 = new LoginFrame(); f1.last(); } }
//登录界面的监听器,计算器的入口 import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class LoginListener implements ActionListener { private JTextField name ; private JPasswordField password; public LoginListener (JTextField name,JPasswordField password){ this.name=name; this.password=password; } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String nm=name.getText(); String ps=password.getText(); if((nm.equals("5") && ps.equals("1")) || (nm.equals("123456789") && ps.equals("12345"))){ JFrame jf1= new JFrame(); jf1.setTitle("英雄联盟计算器!"); //设置边框 jf1.setDefaultCloseOperation(3); jf1.setLayout(null); //设置窗体的布局方式为空布局 jf1.setBackground(Color.GRAY); //设置窗体颜色 jf1.setBounds(480,150,370,450); //设置窗体位置及大小 jf1.setResizable(false); //设置禁止用户调整窗体的大小 JTextField field= new JTextField (15); jf1.add(field); field.setBounds(10,10,340,150); field.setEditable(false); field.setOpaque(false); JButton b21= new JButton("←"); jf1.add(b21); b21.setBounds(10,170,60,40); ButtonListener m21 = new ButtonListener(field); b21.addActionListener(m21); jf1.setVisible(true); JButton b20= new JButton("CE"); jf1.add(b20); b20.addActionListener(m21); b20.setBounds(80,170,60,40); JButton b19= new JButton("C"); jf1.add(b19); b19.addActionListener(m21); b19.setBounds(150,170,60,40); JButton b18= new JButton("±"); jf1.add(b18); b18.addActionListener(m21); b18.setBounds(220,170,60,40); JButton b17= new JButton("√"); jf1.add(b17); b17.addActionListener(m21); b17.setBounds(290,170,60,40); JButton b7= new JButton("7"); jf1.add(b7); b7.addActionListener(m21); b7.setBounds(10,220,60,40); JButton b8= new JButton("8"); jf1.add(b8); b8.addActionListener(m21); b8.setBounds(80,220,60,40); JButton b9= new JButton("9"); jf1.add(b9); b9.addActionListener(m21); b9.setBounds(150,220,60,40); JButton b10= new JButton("/"); jf1.add(b10); b10.addActionListener(m21); b10.setBounds(220,220,60,40); JButton b16= new JButton("%"); jf1.add(b16); b16.addActionListener(m21); b16.setBounds(290,220,60,40); JButton b4= new JButton("4"); jf1.add(b4); b4.addActionListener(m21); b4.setBounds(10,270,60,40); JButton b5= new JButton("5"); jf1.add(b5); b5.addActionListener(m21); b5.setBounds(80,270,60,40); JButton b6= new JButton("6"); jf1.add(b6); b6.addActionListener(m21); b6.setBounds(150,270,60,40); JButton b11= new JButton("*"); jf1.add(b11); b11.addActionListener(m21); b11.setBounds(220,270,60,40); JButton b15= new JButton("1/x"); jf1.add(b15); b15.addActionListener(m21); b15.setBounds(290,270,60,40); JButton b1= new JButton("1"); jf1.add(b1); b1.addActionListener(m21); b1.setBounds(10,320,60,40); JButton b2= new JButton("2"); jf1.add(b2); b2.addActionListener(m21); b2.setBounds(80,320,60,40); JButton b3= new JButton("3"); jf1.add(b3); b3.addActionListener(m21); b3.setBounds(150,320,60,40); JButton b12= new JButton("-"); jf1.add(b12); b12.addActionListener(m21); b12.setBounds(220,320,60,40); JButton b14= new JButton("="); jf1.add(b14); b14.addActionListener(m21); b14.setBounds(290,320,60,90); JButton b0= new JButton("0"); jf1.add(b0); b0.addActionListener(m21); b0.setBounds(10,370,130,40); JButton b23= new JButton("."); jf1.add(b23); b23.addActionListener(m21); b23.setBounds(150,370,60,40); JButton b13= new JButton("+"); jf1.add(b13); b13.addActionListener(m21); b13.setBounds(220,370,60,40); ImageIcon icon3 = new ImageIcon("img/blue.jpg"); JLabel imgLabel3= new JLabel(icon3); imgLabel3.setBounds(0,0,400,420); jf1.add(imgLabel3); jf1.setVisible(true); } else JOptionPane.showMessageDialog(null, "账号或者密码错误"); } }
//计算器的监听器 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JTextField; public class ButtonListener implements ActionListener { private String content; int number; double re,ff; int a=0,b,z=0; double rec,sq,poi=0; String m,p="",mi="",mul="",div="",rem="",po=""; double neg; private JTextField jt; //定义运算函数 double plus ( double m, double n ) { return m+n; } double minus(double m ,double n) { return m-n; } double multiply(double m,double n) { return m*n; } double divide(double m,double n) { return m/n; } int remainder (int m,int n) { return m%n; } double reciproc (double m) { return (double) 1/m; } double negate(double m) { m=-m; return m; } public ButtonListener(JTextField jt){ this.jt=jt; } public void actionPerformed(ActionEvent e) { content=jt.getText(); //获取输入框中的文本 try{ //ff = Float.parseFloat(e.getActionCommand()); number=Integer.parseInt(e.getActionCommand()); //将事件源转化为数字进行判断 }catch(Exception ex){ } if(0<=number && number<10){ //判断事件源为数字 b=number; if(content.equals(p)){ //通过判断选择与之对应的函数进行运算 re=plus(a,b); //加法运算 } if(content.equals(mi)){ re=minus(a,b); //减法运算 } if(content.equals(mul)){ re=multiply(a,number); //乘法运算 } if(content.equals(div)){ re=divide(a,number); //除法运算 } if(content.equals(rem)){ re=remainder(a,number); //取余运算 } if(content.equals(po)){ jt.setText(""+poi); //小数点运算 } jt.setText(content+number); } if(e.getActionCommand().equals("+")){ jt.setText(content+e.getActionCommand()); m=e.getActionCommand(); a=Integer.parseInt(content); jt.setText(content+m); p=content+m; } else if(e.getActionCommand().equals("-")){ jt.setText(content+e.getActionCommand()); m=e.getActionCommand(); a=Integer.parseInt(content); jt.setText(content+m); mi=content+m; } else if(e.getActionCommand().equals("*")){ jt.setText(content+e.getActionCommand()); m=e.getActionCommand(); a=Integer.parseInt(content); jt.setText(content+m); mul=content+m; } else if(e.getActionCommand().equals("/")){ jt.setText(content+e.getActionCommand()); m=e.getActionCommand(); a=Integer.parseInt(content); jt.setText(content+m); div=content+m; } else if(e.getActionCommand().equals("%")){ jt.setText(content+e.getActionCommand()); m=e.getActionCommand(); a=Integer.parseInt(content); jt.setText(content+m); rem=content+m; } else if(e.getActionCommand().equals("1/x")){ a=Integer.parseInt(content); rec=reciproc(a); jt.setText("reciproc("+content+")="+rec); } else if(e.getActionCommand().equals("±")){ a=Integer.parseInt(content); neg=negate(a); jt.setText("reciproc("+content+")="+neg); } else if(e.getActionCommand().equals("√")){ a=Integer.parseInt(content); sq=Math.sqrt(a); jt.setText("sqrt("+content+")="+sq); } else if(e.getActionCommand().equals(".")){ m=e.getActionCommand(); a=Integer.parseInt(content); po=content+m; jt.setText(po); } //以下还未实现 else if(e.getActionCommand().equals("C")){ jt.setText(content+m); } else if(e.getActionCommand().equals("CE")){ jt.setText(content+m); } else if(e.getActionCommand().equals("←")){ jt.setText(content+e.getActionCommand()); m=e.getActionCommand(); a=Integer.parseInt(content); jt.setText(content+m); mi=content+m; } else if(e.getActionCommand().equals("=")){ jt.setText(content+e.getActionCommand()+re); } } }
相关推荐
在本项目中,"c#带登陆界面的计算器全部源代码"是一个实现了用户登录功能并与计算器功能集成的C#应用程序。这个程序允许用户通过输入账户和密码进行身份验证,然后才能使用计算器的功能。以下是该程序涉及到的主要...
在这个计算器项目中,Swing被用来创建窗口、按钮、文本框等UI组件,构建出用户友好的交互界面。例如,JFrame用于创建主窗口,JButton用于创建操作按钮,JTextField用于显示计算结果和接收用户输入,JOptionPane则...
- **功能介绍**:登录界面验证后进入计算器界面,通过按钮事件监听进行计算,退出界面提供程序关闭选项。 4. **详细说明与调试** - **程序流程**:通过绘制程序流程图来清晰展示用户交互逻辑和内部计算过程。 - ...
登陆界面的实现则涉及到另一个窗口或对话框的设计。通常,我们会使用`QDialog`类来创建这个界面,包含用户名和密码输入框,以及登录和取消按钮。使用`QLineEdit`类创建输入框,`QPushButton`创建按钮,同时设置好...
* 三个窗体界面:一个登陆界面 Login、一个主窗体 LabMain、一个计算器窗体 Calculator * 登陆界面要求包括课程名称、指导老师、学生班级名称、时间;一个进入主界面的按钮 * 主界面包括菜单一项,显示为“实验一” ...
使用MFC实现的一个登录界面,有学生端和管理员端两个用户端,输入不同的账号密码可以进入不同的界面(弹出不同的模态窗口)。运行此程序可能需要VS相应环境支持。学生端账号密码皆为123456;管理员端密码皆为000000...
在这个“简单登陆 计算机”的项目中,我们将探讨如何构建一个基础的GUI登录界面以及一个简易的计算器功能。 首先,GUI登录界面通常包括用户名输入框、密码输入框和登录按钮。用户名输入框让用户输入他们的账户名,...
按钮可能分别引导用户进入如天气查询、记事本、计算器或日程管理等实用功能。 在Android开发中,实现这样的应用涉及以下几个关键知识点: 1. **Activity与Intent**: Android中的Activity是用户界面的基本单元,每...
1. **登陆窗口 (w_denglu)**:此窗口负责验证用户身份,只有在输入正确的用户名和密码后,用户才能进入系统。如果输入信息错误,系统会显示错误提示。 2. **菜单 (w_main)**:主菜单界面,提供到各个功能窗口的入口...
进入后,你可以用方向键移动光标选择CMOS设置界面上的选项,然后按Enter进入副选单,用ESC键来返回上一级菜单,用PAGE UP和PAGE DOWN键来选择具体选项,F10键保留并退出BIOS设置。 1.STANDARD CMOS SETUP(标准...
62.增加咨客系统开房后可以修改预定人(进入"包厢预定"-"查询"界面修改,有权限控制,使用前请先配置员工权限). 63.增加员工预定人组设置,在定位人中只显示属于预定人组的员工. 64.增加包厢查询可查当前的包厢消费...
人力资源管理软件解决登陆界面服务器名无法显示全的问题(感谢山中人) 优化了人力资源管理软件保存到excel数值类型的问题(感谢54HAPPY) 增加了合同管理功能(人力资源软件) 解决人力资源管理软件工资帐套选择的公式...
因此,开发这样一套管理软件成为很有必要的事情,故决定选择学生成绩分析管理系统来作为本人的毕业设计,在很短的时间里开发出一套界面友好,功能强大,使用简单的适用于各规模学院的学生成绩分析管理系统。...
3.科学计算器;4.绘制函数图形;5.常用数学公式;6.万年历。 (四)家庭财务 1.家庭财务管理:记录每月的收入/支出;银行存款;股票投资;家庭购物。条理清析。 2.每日基金管理:输入基金的买入价格、申购费率...
当您进入秘奥软件以后,在系统的主界面可以看到一行菜单栏,其中包括“采购管理”、“前台管理”、“销售管理”、“仓库管理”、“生产管理”、“财务管理”、“客户关系管理”、“报表统计”、“辅助工具”、“系统...