流程图
登录界面流程图
计算器流程图
前端设计
Login.html代码
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="css/style.css" />
</head>
<style>
body{
background: url('img/bg.jpg') top left;//背景图片
background-size: 100%;
}
</style>
<body>
<div id="div-title">
<h3>欢迎登陆</h3>
</div>
<div id="form-border">
<form method="post">
<div id="img-control">
<img src="img/01.png" id="login-img"/>
</div>
<label class="form-label" for="account">账 号</label>
<input id="account" class="form-input" type="text" placeholder="账号" required="required"/><br>
<label class="form-label" for="password">密 码</label>
<input id="password" class="form-input" type="password" placeholder="密码" required="required"/><br>
<label id="register" class="form-label" for="password">没有账号?<a href="register.html">点击注册</a></label><br>
<input id="login" class="form-button" type="submit" value="登陆"/>
</form>
</div>
</body>
</html>
Register.html代码
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="css/style.css" />
</head>
<style>
body{
background: url(img/bg.jpg) top left;//背景图片
background-size: 100%;
}
</style>
<body>
<div id="div-title">
<h3>欢迎注册</h3>
</div>
<div id="form-border">
<form method="post">
<div id="img-control">
<img src="img/01.png" id="register-img"/>
</div>
<label class="form-label" for="account">账 号</label>
<input id="account" class="form-input" type="text" placeholder="账号" required="required"/><br>
<label class="form-label" for="password">密 码</label>
<input id="password" class="form-input" type="password" placeholder="密码" required="required"/><br>
<label id="register" class="form-label" for="password">已有账号?<a href="login.html">点击登陆</a></label><br>
<input id="login" class="form-button" type="submit" value="注册"/>
</form>
</div>
</body>
</html>
Style.css代码
img {
width: 150px;
height: 150px;
}
#login-img{
margin-left: 30px;
float: left;
}
#register-img{
float: right;
margin-right: 35px;
}
#img-control {
margin-top: 65px;
}
#form-border {
background-color: rgba(188, 176, 212, 0.6);
width: 500px;
height: 300px;
position: absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.form-input {
width: 200px;
height: 25px;
padding-left: 5px;
border-radius: 5px;
}
.form-label {
margin-left: 30px;
}
.form-button {
width: 70px;
height: 30px;
margin-top: 10px;
margin-left: 70px;
font-size: 18px;
font-family: "宋体";
}
#div-title {
text-align: center;
margin-top: 150px;
}
h3 {
font-size: 40px;
font-family: "华文彩云";
}
#password {
margin-top: 20px;
margin-bottom: 10px;
}
#register {
margin-left: 70px;
}
Test.jsp
<%@ page contentType=”text/html;charset=UTF-8”language=”java”%>
<%@ page import=”con.qf.domian.method”%>
<%
//获取对应的用户名和密码
Method m = new method();
String userName = request.getParameter(“userName”);
String passWord = request.getParameter(“passWord”);
int re = m.checkLogin(userName,passWord);
if(re==1)
{
Out.printf(“<script>alert(‘用户不存在’);window.location.heref=’login.html’</script>”);
}else if(re==2){
Out.print(“<script>alert(‘登录成功’;window.location.href=’index.html/’</script>’’);
}else{
Out.printf(“<script>alert(‘用户名或密码错误’);window.location.heref=’login.html’</script>”);
}//跳转页面
%>
插入的图片
插入的效果图
连接数据库
package com.qf.domian;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class connectionMysql {
Connection con = null;
ResultSet res = null;
//打开数据库连接
public Connection openMysql() {
try {
//jdbc链接MySQL数据库
//加载注册驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
String ulr = "jdbc:mysql://127.0.0.1:3306/student?characterEncoding=UTF-8";
String uname = "root";
String upass = "123456";
con = DriverManager.getConnection(ulr, uname, upass);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
//增删改
public int update(String sql) {
//保存返回值
int re = 0;
try {
//创建执行对象
Statement sta = con.createStatement();
//增删改 executeUpdate
re = sta.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
return re;
}
//查询
public ResultSet select(String sql) {
try {
//创建执行对象
Statement sta = con.createStatement();
//增删改 executeUpdate
res = sta.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
//破解表结构
public void passTable(String tableName){
try {
//查询executeQuery
String selectSql="select * from "+tableName ;
Statement sta = con.createStatement();
ResultSet res = sta.executeQuery(selectSql);
//从结果集中获取表结构源数据
ResultSetMetaData meta = res.getMetaData();
int sum = meta.getColumnCount();
for(int i=1;i<=sum;i++){
//获取对应的字段名
String name = meta.getColumnName(i);
//获取的字段类型
String columnTypeName = meta.getColumnTypeName(i);
System.out.println(name+":"+columnTypeName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
计算器
package p;
import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*JFrame是Java Swing GUI工具包中的一个类,用于创建窗口应用程序*/
public class caculator extends JFrame implements ActionListener {
//北面控件
private JPanel jp_north = new JPanel();
//为计算器添加输入框
private JTextField input_text = new JTextField();//创建输入框
private JButton c_Bth = new JButton("c");//创建一个按钮
//中间控件
private JPanel jp_center = new JPanel();//创建页面布局
public static final int FRAME_W=600;
public static final int FRAME_H=500;
public static final int SCREEN_W= Toolkit.getDefaultToolkit().getScreenSize().width;
public static final int SCREEN_H=Toolkit.getDefaultToolkit().getScreenSize().height;
public int frame_x=(SCREEN_W-FRAME_W)/2;
public int frame_y=(SCREEN_H-FRAME_H)/2;
public caculator() throws HeadlessException {
this.init();
this.addNorthComponet();
this.addCenterButton();
}
//进行初始化
public void init() {
this.setTitle("小福泥计算器");//设置名称
this.setSize(600, 600);//给窗体设置大小
this.setLayout(new BorderLayout());
this.setResizable(false);//设置窗体大小不可修改
this.setLocation(frame_x, frame_y);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗体关闭
}
//添加北面控件
public void addNorthComponet() {
this.input_text.setPreferredSize(new Dimension(300, 50));
jp_north.add(input_text);
this.c_Bth.setForeground(Color.RED);
jp_north.add(c_Bth);
c_Bth.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
input_text.setText("");
}
});
//添加输入框到窗体中
this.add(jp_north, BorderLayout.NORTH);
/*BorderLayout 将容器分为 EAST 、 SOUTH 、 WEST 、 NORTH 、 CENTER
五个区域,普通组件可以被放置在这 5 个区域的任意一个中 */
}
//添加中间按钮
public void addCenterButton() {
String bth_text = "123+456-789*0.=/√";
String regx = "[\\+\\-*/=.√]";
this.jp_center.setLayout(new GridLayout(5, 4));
for (int i = 0; i < 17; i++) {
String temp = bth_text.substring(i, i + 1);
JButton btn = new JButton();//创建数字按钮
btn.setText(temp);
/* if (temp.matches(regx)) {
//设置字体
btn.setFont(new Font("粗体", Font.BOLD, 500));
btn.setForeground(Color.RED);
}*/
if(temp.equals("+")||temp.equals("-")||temp.equals("*")
||temp.equals("/")||temp.equals(".")||temp.equals("="))
{
btn.setFont(new Font("粗体",Font.BOLD,20));
btn.setForeground(Color.RED);
}
btn.addActionListener(this);
jp_center.add(btn);
}
this.add(jp_center, BorderLayout.CENTER);
}
private String firstInput = null;
private String operator=null;
@Override
public void actionPerformed(ActionEvent e) {
String clickStr = e.getActionCommand();
if ("√.0123456789".indexOf(clickStr) != -1) {
this.input_text.setText(input_text.getText() + clickStr);
this.input_text.setHorizontalAlignment(JTextField.RIGHT);
//JOptionPane.showMessageDialog(this,clickStr);
} else if (clickStr.matches("[\\+\\-*/√]{1}")) {
operator = clickStr;
firstInput = this.input_text.getText();
this.input_text.setText("");
} else if (clickStr.equals("=")) {
Double a = Double.valueOf(firstInput);
Double b = Double.valueOf(this.input_text.getText());
Double result = null;
switch (operator) {
case "+":
result = a + b;
break;
case "-":
result = a - b;
break;
case "*":
result = a * b;
break;
case "/":
if (b != 0) {
result = a / b;
}
break;
}
this.input_text.setText(result.toString());
}
}
public static void main(String[] args) {
caculator Caculator = new caculator();
Caculator.setVisible(true);
}
}
设置监听
设两个空字符串,一个用来保存表达式,另一个用来保存历史记录
@Override
public void actionPerformed(ActionEvent e) { //监听功能
String strings = e.getActionCommand();//保存记录
//JOptionPane.showMessageDialog(this,strings);//监听
if ("0123456789".contains(strings)) {
if (Objects.equals(firstint, "")) { //输入新的表达式,清除掉上一个表达式结果
firstint+=strings;
this.input_text.setText(strings);
this.input_text.setHorizontalAlignment(JTextField.RIGHT);//显示到右边
}else if(strings.equals("0")){
if (Objects.equals(firstint, "0")) {
this.input_text.setText(firstint);
}else {
int index = 0;
for ( int i=firstint.length()-1;i >=0;i-- ) {
if(isSymbol(firstint.substring(index))){
index=i;
}
}
if (!firstint.substring(index+1, firstint.length() - 1).equals("0")) {
firstint += strings;
}
this.input_text.setText(firstint);
}
} else if(firstint.charAt(firstint.length()-1)==')'){ //)后输入数字补*号
firstint+="*"+strings;
this.input_text.setText(firstint);
}else {this.input_text.setText(input_text.getText() + strings);//将输入的数记录并将之前的数放到前面
this.input_text.setHorizontalAlignment(JTextField.RIGHT);//显示到右边
firstint += strings;
System.out.println(firstint);}
} else if (strings.equals(".")) {
if (Objects.equals(firstint, "")) {
if (!Objects.equals(ans, "")) {
firstint = ans + ".";
this.input_text.setText(firstint);
}else {this.input_text.setText(input_text.getText() + "0" + strings);
this.input_text.setHorizontalAlignment(JTextField.RIGHT); //自带补0
firstint = "0" + strings;}
} else if(firstint.charAt(firstint.length() - 1) == ')'){ //)后输入小数点补0
firstint =firstint+ "*0"+strings;
this.input_text.setText(firstint);
}
else if (firstint.charAt(firstint.length() - 1) == '.') { //不能连续小数点
this.input_text.setText(firstint);
this.input_text.setHorizontalAlignment(JTextField.RIGHT);
} else if (!ToPolland.isNumber(String.valueOf(firstint.charAt(firstint.length() - 1))) && !String.valueOf(firstint.charAt(firstint.length() - 1)).equals(".")) {
this.input_text.setText(input_text.getText() + "0" + strings); //前一个既不是数字也不是小数补0
this.input_text.setHorizontalAlignment(JTextField.RIGHT);
firstint = firstint + "0" + strings;
} else if (ToPolland.isNumber(String.valueOf(firstint.charAt(firstint.length() - 1)))) {//如果前面是数字之间输入
int count = 0, i = 0;
for (i = firstint.length() - 1; i > 0; i--) { //查找前面的数字串中有没有小数点
if (ToPolland.isSymbol(String.valueOf(firstint.charAt(i)))) {
break;
}//直到遇到下一个运算符时结束查找
else if (firstint.charAt(i) == '.') {
count++;
}
}
if (count == 0) { //判断前面的数字串没有小数点
this.input_text.setText(input_text.getText() + strings);
this.input_text.setHorizontalAlignment(JTextField.RIGHT);
firstint = firstint + strings;
}
}
} else if (strings.matches("[+\\-*/]")) {
if (Objects.equals(firstint, "")) { //运算符前必须有运算数
if (!Objects.equals(ans, "")) {
firstint += ans+strings;
this.input_text.setText(firstint);
}
if(strings.equals("-")){ //减号第一个输入当做负号
this.input_text.setHorizontalAlignment(JTextField.RIGHT);
this.input_text.setText("-");
firstint += strings;
}
//JOptionPane.showMessageDialog(null, "请先输入操作数");
}else if(firstint.charAt(firstint.length()-1)=='.'){ //小数点后不能跟运算符
this.input_text.setText(firstint);
}
else if (firstint.charAt(firstint.length() - 1)=='('){ //(后只能跟-号运算符
if(strings.equals("-")){
firstint+=strings;
this.input_text.setText(firstint);
}
}
else if (ToPolland.isSymbol(String.valueOf(firstint.charAt(firstint.length() - 1)))&&strings!="-") {
this.input_text.setText(firstint);
} else {
this.input_text.setText(input_text.getText() + strings);
firstint = firstint + strings;
System.out.println(firstint);
}
} else if (strings.matches("[()]{1}")) {
if (strings.equals("(")) {
if(Objects.equals(firstint, "") ||firstint.charAt(firstint.length()-1)=='('){
firstint+="(";
this.input_text.setText(firstint);
this.input_text.setHorizontalAlignment(JTextField.RIGHT);
}else if(firstint.charAt(firstint.length() - 1) == ')'){
firstint+="*"+strings;
this.input_text.setText(firstint);
} else if(ToPolland.isNumber(String.valueOf(firstint.charAt(firstint.length()-1)))){
firstint+="*"+strings;
this.input_text.setText(firstint);
}else if(ToPolland.isSymbol(String.valueOf(firstint.charAt(firstint.length()-1)))){
firstint+=strings;this.input_text.setText(firstint);
}
System.out.println(firstint);
} else if (strings.equals(")") && firstint != "") {
if(ToPolland.isSymbol(String.valueOf(firstint.charAt(firstint.length()-1)))){
this.input_text.setText(firstint);
}else if (firstint.charAt(firstint.length() - 1) != '(') {
int count1 = 0, count2 = 1;
for (int i = 0; i < firstint.length(); i++) { //找出()的个数
if (firstint.charAt(i) == '(') {
count1++;
}
if (firstint.charAt(i) == ')') {
count2++;
}
}
if (count1 >= count2) { (个数必须大于等于)个数
this.input_text.setText(input_text.getText() + strings);
firstint = firstint + strings;
System.out.println(firstint);
}
}
}
} else if (strings.equals("=")) { //计算结果
try {
if(firstint.charAt(firstint.length()-1)=='.'){
firstint=firstint.substring(0,firstint.length()-2);
}System.out.println(firstint);
StringBuilder builder = new StringBuilder();
List<String> list = toPolland(ToPolland.toList(firstint));
list.forEach(builder::append);
System.out.println("算式表达式:" + firstint);
System.out.println("转逆波兰表达式:" + builder);
System.out.println("转逆波兰表达式计算结果:" + firstint + "=" + ToPolland.calculate(list));
ans = String.valueOf(ToPolland.calculate(list));
this.input_text.setText("" + ToPolland.calculate(list));
hitory += firstint + "=" + ToPolland.calculate(list) + "\n";
firstint = "";
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "表达式错误");
firstint = "";
this.input_text.setText("");
}
} else if (strings.equals("H")) { //查看历史记录
history.setFont(new Font("宋体", Font.BOLD, 20));
this.history.setText(hitory);
} else if (strings.equals("C")) { //清空当前记录
firstint = "";
this.input_text.setText("");
} else if (strings.equals("AC")) { //清空历史记录
firstint = "";
hitory = "";
this.history.setText("");
this.input_text.setText("");
} else if (strings.equals("←") && firstint.length() != 0) { //退格
firstint = firstint.substring(0, firstint.length() - 1);
System.out.println(firstint);
this.input_text.setText("" + firstint.substring(0, firstint.length()));
}
}
}