一.实验目的
设计一个包含登录界面的计算器软件,该软件可以实现第一次作业中的全部功能,同时可以保存用户的历史计算记录。
二.项目功能
- 简单计算机的设计与实现
- 登陆界面
- 登录和注册功能的实现
- 使用java语言,eclipse连接数据库
- 数据库保存数据
三.使用环境
- 使用Microsoft Visio作为绘图工具
- 使用Java语言与IntelliJ IDEA Community Edition作开发工具
- 使用MySQL数据库储存数据
- 使用Navicat管理操作数据库
四.流程图
1.登录流程图

2.注册流程图

五.登录界面的前端代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>登录页面</title> <link href="./css/new_file.css" rel="stylesheet"/> </head> <body id="Body"> <form action="users" method="post" novalidate> <div id="Background" > <div id="z"> 登录:<input id="names" name="names" type="text" placeholder="请输入用户名"/> <br /> 密码:<input id="pass" name="pass" type="password" placeholder="请输入密码" /> <br /> </div> <div id="BetweenButton" > <input type="button" value="登录" onclick="login()" /> <input style="margin-left: 10px;" type="button" value="注册" onclick="register()" /> </div> </div> </form> <script> function login(){ var username = document.getElementById("names").value; var password = document.getElementById("pass").value; var user = { username: username, password: password }; var fakeResponse = {success: false }; setTimeout(function(){ if(fakeResponse.success){ alert("登录成功!"); } else{ alert("用户名或密码不正确!"); } }, 1000); } function register(){ alert("未注册!请先注册!"); } </script> </body></html>#fram{ }#Background{ height: 400px; width: 500px; background-repeat: no-repeat; background-size: 100% auto; border-radius: 10px; position: absolute; left: 20%; top: 25%; }#BetweenButton{ position: absolute; width: 200px; left: 40%; margin-top: 15px; }#Body{ background-image: url("../img/a4.jpg"); background-repeat: no-repeat; background-attachment: fixed; background-size: cover; }#names{ margin-top: 30px;}#pass{ margin-top: 30px;}#z{ margin-left: 25%; margin-top: 25%; |
登陆界面代码和效果如下


六.计算器的设计
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
1.导入模块import tkinter as tk2.实例化一个窗体对象root = tk.Tk()3.标题root.title('计算器')4.大小以及出现的位置root.geometry("295x280+150+150")5.透明度root.attributes("-alpha", 0.9)6.背景root["background"] = "#ffffff"7.标签lable1 = tk.Label(root, textvariable=result_num, width=20, height=2, font=('宋体', 20), justify='left', background='#ffffff', anchor='se')8.布局lable1.grid(padx=4, pady=4, row=0, column=0, columnspan=4)9.按钮button_clear = tk.Button(root, text='C', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: clear())button_back = tk.Button(root, text='←', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: back())button_division = tk.Button(root, text='/', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('/'))button_multiplication = tk.Button(root, text='x', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('*')) button_clear .grid(padx=4, row=1, column=0)button_back .grid(padx=4, row=1, column=1)button_division .grid(padx=4, row=1, column=2)button_multiplication .grid(padx=4, row=1, column=3) button_seven = tk.Button(root, text='7', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('7'))button_eight = tk.Button(root, text='8', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('8'))button_nine = tk.Button(root, text='9', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('9'))button_subtraction = tk.Button(root, text='—', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('-'))button_seven .grid(padx=4, row=2, column=0)button_eight .grid(padx=4, row=2, column=1)button_nine .grid(padx=4, row=2, column=2)button_subtraction .grid(padx=4, row=2, column=3) button_four = tk.Button(root, text='4', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('4'))button_four.grid(padx=4, pady=4, row=3, column=0)button_five = tk.Button(root, text='5', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('5'))button_five.grid(padx=4, row=3, column=1)button_six = tk.Button(root, text='6', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('6'))button_six.grid(padx=4, row=3, column=2)button_addition = tk.Button(root, text='+', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('+'))button_addition.grid(padx=4, row=3, column=3) button_one = tk.Button(root, text='1', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('1'))button_one.grid(padx=4, row=4, column=0)button_two = tk.Button(root, text='2', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('2'))button_two.grid(padx=4, row=4, column=1)button_three = tk.Button(root, text='3', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('3'))button_three.grid(padx=4, row=4, column=2)button_equal = tk.Button(root, text='=', width=5, height=3, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: equal())button_equal.grid(padx=4, row=4, rowspan=5, column=3) button_zero = tk.Button(root, text='0', width=12, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('0'))button_zero.grid(padx=4, pady=4, row=5, column=0, columnspan=2)button_decimal = tk.Button(root, text='.', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('.'))button_decimal.grid(padx=4, row=5, column=2) |
计算器的界面效果:

功能描述:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
1.添加数字def append_num(i): lists.append(i) result_num.set(''.join(lists))2.选择运算符号def operator(i): if len(lists) > 0: if lists[-1] in ['+', '-', '*', '/']: lists[-1] = i else: lists.append(i) result_num.set(''.join(lists))3.清零def clear(): lists.clear() result_num.set(0)4.退格def back(): del lists[-1] result_num.set(lists)5.等号def equal(): a = ''.join(lists) end_num = eval(a) result_num.set(end_num) lists.clear() lists.append(str(end_num))6.定义一个列表收集输入的内容lists = []result_num = tk.StringVar()result_num.set(0) |
运行代码,输入数据进行运算:


七.数据库的连接
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
package com.hsp.edu; import com.mysql.cj.jdbc.Driver; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties;//进一步优化,将信息写入到配置文件 public static void connect05() throws IOException, ClassNotFoundException, SQLException { //通过Properties对象获取配置文件信息 Properties properties = new Properties(); properties.load(new FileInputStream("src\\mysql.properties"));//此时已经将配置文件的信息读取到了Properties中 //获取相关信息 final String user = properties.getProperty("user");//用户 final String password = properties.getProperty("password");//密码 final String url = properties.getProperty("url");//url final String driver = properties.getProperty("driver"); Class.forName(driver);//注册驱动 final Connection connection = DriverManager.getConnection(url, user, password);//获取连接 System.out.println(connection); }package com.hsp;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;public class Jdbc02 { public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException { //前置工作:获取配置文件中的信息 Properties properties = new Properties(); properties.load(new FileInputStream("src\\mysql1.properties"));//读取信息到集合中 final String driver = properties.getProperty("driver");//获取全类名 final String url = properties.getProperty("url");//获取url final String user = properties.getProperty("user");//获取用户名 final String password = properties.getProperty("password");//获取密码 System.out.println(properties); //1.注册驱动 Class.forName(driver); //2.获取连接 final Connection connection = DriverManager.getConnection(url, user, password); //3.执行 SQL语句 //String sql1 = "CREATE TABLE news(id INT,content VARCHAR(32))"; String sql2="INSERT INTO news VALUES (1,'张三','123456'),(2,'李四','56789'),(3,'刘琴','123456'); final Statement statement = connection.createStatement(); //final int row1 = statement.executeUpdate(sql1);//返回影响的行数 final int row2 = statement.executeUpdate(sql2);//返回影响的行数 final int row3 = statement.executeUpdate(sql3); //:验证是否执行成功 /*if(row1!=0){ System.out.println("执行成功"); }else { System.out.println("执行失败"); }*/ if (row2!=0){ System.out.println("执行成功"); }else { System.out.println("执行失败"); } if(row3!=0){ System.out.println("执行成功"); }else { System.out.println("执行失败"); } //4.关闭资源 statement.close(); connection.close(); }} |