第二次作业

发布时间 2023-12-04 21:03:02作者: 小小只透明人

登录注册界面

实验目的

1.掌握软件开发的基本流程

2.掌握常用的软件开发方式和工具。

实验内容

设计一个包含登录界面的计算器软件,该软件可以实现第一次作业中的全部功能,同时可以保存用户的历史计算记录。

实验环境;java

软件登录界面

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;

public LoginFrame() {
// 设置窗口标题
setTitle("登录");

// 设置窗口大小
setSize(300, 150);

// 设置布局为流式布局
setLayout(new FlowLayout());

// 创建用户名标签和文本框
JLabel usernameLabel = new JLabel("用户名:");
usernameField = new JTextField(15);

// 创建密码标签和密码框
JLabel passwordLabel = new JLabel("密码:");
passwordField = new JPasswordField(15);

// 创建登录按钮
JButton loginButton = new JButton("登录");

// 为登录按钮添加点击事件监听器
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取输入的用户名和密码
String username = usernameField.getText();
char[] passwordChars = passwordField.getPassword();
String password = new String(passwordChars);

// 在这里可以添加验证逻辑,例如检查用户名和密码是否匹配

// 简单示例,显示一个对话框
JOptionPane.showMessageDialog(LoginFrame.this, "用户名: " + username + "\n密码: " + password);
}
});

// 将组件添加到窗口
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(loginButton);

// 设置窗口关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 设置窗口居中显示
setLocationRelativeTo(null);
}

public static void main(String[] args) {
// 在事件调用线程中创建和显示 GUI
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new LoginFrame().setVisible(true);
}
});
}
}

 接入数据库

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class RegisterFrame extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
private JPasswordField confirmPasswordField;

public RegisterFrame() {
// 设置窗口标题
setTitle("注册");

// 设置窗口大小
setSize(300, 200);

// 设置布局为流式布局
setLayout(new FlowLayout());

// 创建用户名标签和文本框
JLabel usernameLabel = new JLabel("用户名:");
usernameField = new JTextField(15);

// 创建密码标签和密码框
JLabel passwordLabel = new JLabel("密码:");
passwordField = new JPasswordField(15);

// 创建确认密码标签和确认密码框
JLabel confirmPasswordLabel = new JLabel("确认密码:");
confirmPasswordField = new JPasswordField(15);

// 创建注册按钮
JButton registerButton = new JButton("注册");

// 为注册按钮添加点击事件监听器
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取输入的用户名和密码
String username = usernameField.getText();
char[] passwordChars = passwordField.getPassword();
char[] confirmPasswordChars = confirmPasswordField.getPassword();

// 检查密码是否匹配
String password = new String(passwordChars);
String confirmPassword = new String(confirmPasswordChars);
if (!password.equals(confirmPassword)) {
JOptionPane.showMessageDialog(RegisterFrame.this, "密码不匹配");
return;
}

// 连接数据库并插入用户信息
try {
Connection connection = DriverManager.getConnection("jdbc:h2:mem:test", "sa", "");
String query = "INSERT INTO users (username, password) VALUES (?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();

// 简单示例,显示一个对话框
JOptionPane.showMessageDialog(RegisterFrame.this, "注册成功!\n用户名: " + username);
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(RegisterFrame.this, "注册失败");
}
}
});

// 将组件添加到窗口
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(confirmPasswordLabel);
add(confirmPasswordField);
add(registerButton);

// 设置窗口关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 设置窗口居中显示
setLocationRelativeTo(null);
}

public static void main(String[] args) {
// 在事件调用线程中创建和显示 GUI
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new RegisterFrame().setVisible(true);
}
});
}
}

 

软件注册界面

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RegisterFrame extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
private JPasswordField confirmPasswordField;

public RegisterFrame() {
// 设置窗口标题
setTitle("注册");

// 设置窗口大小
setSize(300, 200);

// 设置布局为流式布局
setLayout(new FlowLayout());

// 创建用户名标签和文本框
JLabel usernameLabel = new JLabel("用户名:");
usernameField = new JTextField(15);

// 创建密码标签和密码框
JLabel passwordLabel = new JLabel("密码:");
passwordField = new JPasswordField(15);

// 创建确认密码标签和确认密码框
JLabel confirmPasswordLabel = new JLabel("确认密码:");
confirmPasswordField = new JPasswordField(15);

// 创建注册按钮
JButton registerButton = new JButton("注册");

// 为注册按钮添加点击事件监听器
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取输入的用户名和密码
String username = usernameField.getText();
char[] passwordChars = passwordField.getPassword();
char[] confirmPasswordChars = confirmPasswordField.getPassword();

// 在这里可以添加注册逻辑,例如检查用户名是否已经存在,检查密码是否匹配等

// 简单示例,显示一个对话框
JOptionPane.showMessageDialog(RegisterFrame.this, "注册成功!\n用户名: " + username);
}
});

// 将组件添加到窗口
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(confirmPasswordLabel);
add(confirmPasswordField);
add(registerButton);

// 设置窗口关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 设置窗口居中显示
setLocationRelativeTo(null);
}

public static void main(String[] args) {
// 在事件调用线程中创建和显示 GUI
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new RegisterFrame().setVisible(true);
}
});
}
}

 

 

计算机功能实现

 

 

<!DOCTYPE html>
<html lang="ch-ZN">
<head>
    <meta charset="UTF-8">
    <title>计算器</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .button {
            width: 50px;
            height: 50px;
            font-size: 25px;
            margin: 2px;
            cursor: pointer;
            background: #607d8b;
            border: none;
            color: white;
        }

        .button1 {
            width: 221px;
            height: 50px;
            font-size: 25px;
            margin: 2px;
            cursor: pointer;
            background: #607d8b;
            border: none;
            color: white;
        }

        .textView {
            width: 200px;
            margin: 5px;
            font-size: 23px;
            padding: 5px;
            border: thick double #32a1ce;;
        }
        
        .main {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translateX(-50%) translateY(-50%);
        }

        html {
            background: linear-gradient(to right, #00f7ff, #d0cd77);
            height: 100%;
        }
        
    </style>
    <script>
        let a="1343.43245";
        let b=a.indexOf(".");
        console.log(b);
        let c=a.substring(0,b+3);
        console.log(c);

        function insert(num) {
            // TODO 插入
            document.form.textView.value = document.form.textView.value + num;
        }

        function equal() {
            // TODO 计算结果,并且结果保留两位小数
            let exp = document.form.textView.value;
                if (exp) {
                 let eval1 = eval(document.form.textView.value);
                // eval() 执行括号内的语句 , 记录结果
                let number = eval1.toString().indexOf(".");
                if (number!==-1){
                    // 如果是小数,保留两位小数
                    eval1=eval1.toString().substring(0,number+3);
                    // 截取
                    document.form.textView.value=eval1;
                }else {
                    // 如果不是小数,直接赋值
                    document.form.textView.value=eval1;
                }
            }
        }

        function Mclean() {
            // TODO 清理输入框的文字
            document.form.textView.value = null;
        }

        function back() {
            // TODO 删除文本框的一个字符
            let exp = document.form.textView.value;
            document.form.textView.value = exp.substring(0, exp.length - 1);
            // 截取字符串
        }

        function sqrt() {
            let num = parseFloat(document.form.textView.value);
            if (num < 0) {
                document.form.textView.value='false';
                return;
            }
                var guess = num / 2; // 初始猜测值为 num / 2
                var epsilon = 0.00001; // 定义精度值
                var previousGuess; // 用于存储上一次的猜测值
                do {
                previousGuess = guess; // 保存当前猜测值以便下一次迭代使用
                guess = (guess + num / guess) / 2; // 牛顿迭代法更新猜测值
                 } while (Math.abs(guess - previousGuess) > epsilon); // 当两次猜测值之差小于精度值时停止迭代
                document.form.textView.value=guess;
        }

    </script>
</head>
<body>
<div class="main">
    <form name="form">
        <input class="textView" name="textView" >
    </form>
    <table>
        <tr>
            <td><input type="button" class="button" value="C" onclick="Mclean()"></td>
            <td><input type="button" class="button" value="<" onclick="back()"></td>
            <td><input type="button" class="button" value="/" onclick="insert('/')"></td>
            <td><input type="button" class="button" value="x" onclick="insert('*')"></td>
        </tr>
        <tr>
            <td><input type="button" class="button" value="7" onclick="insert(7)"></td>
            <td><input type="button" class="button" value="8" onclick="insert(8)"></td>
            <td><input type="button" class="button" value="9" onclick="insert(9)"></td>
            <td><input type="button" class="button" value="-" onclick="insert('-')"></td>
        </tr>
        <tr>
            <td><input type="button" class="button" value="4" onclick="insert(4)"></td>
            <td><input type="button" class="button" value="5" onclick="insert(5)"></td>
            <td><input type="button" class="button" value="6" onclick="insert(6)"></td>
            <td><input type="button" class="button" value="+" onclick="insert('+')"></td>

        </tr>
        <tr>
            <td><input type="button" class="button" value="1" onclick="insert(1)"></td>
            <td><input type="button" class="button" value="2" onclick="insert(2)"></td>
            <td><input type="button" class="button" value="3" onclick="insert(3)"></td>
            <td rowspan="2"><input style="height: 107px" type="button" class="button" value="=" onclick="equal()"></td>

        </tr>
        <tr>
            <td colspan="2"><input style="width: 107px" type="button" class="button" value="0" onclick="insert(0)"></td>
            <td><input type="button" class="button" value="." onclick="insert('.')"></td>
        </tr>
        
        
    </table>
    <tr>
            <td><input class="button1"  value="         开方计算" onclick="sqrt()"></td>
        </tr>
</div>
</body>
</html>