第二次作业
一、 计算器登陆界面
流程图如下:


代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>login</title>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div class="box">
<h2>login</h2>
<div class="input-box">
<label>账号</label>
<input type="text">
</div>
<div class="input-box">
<label>密码</label>
<input type="password">
</div>
<div class="btn-box">
<a href="#">忘记密码</a>
<div>
<button>登录</button>
<button>注册</button>
</div>
</div>
</div>
</body>
</html>
@charset "utf-8";
*{
margin: 0;
padding: 0;
}
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
background: url(img2.jpg) no-repeat;
background-size: cover;
}
.box {
border-radius: 20px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 350px;
height: 380px;
border-top: 1px solid rgba(255,255,255,0.5);
border-left: 1px solid rgba(255,255,255,0.5);
border-bottom: 1px solid rgba(255,255,255,0.2);
border-right: 1px solid rgba(255,255,255,0.2);
backdrop-filter: blur(10px);
background: rgba(50,50,50,0.2);
}
.box > h2 {
margin-bottom: 20px;
color: rgba(0, 0, 0, 0.5);
}
.box .input-box {
display: flex;
flex-direction: column;
justify-content: center;
align-items: start;
margin-bottom: 10px;
}
.box .input-box > label {
margin-bottom: 5px;
color: rgba(0,0,0,1);
font-size: 13px;
}
.box .input-box > input {
box-sizing: border-box;
color: rgba(0,0,0,1);
font-size: 14px;
height: 35px;
width: 249px;
background: rgba(255,255,255,0.3);
border: 1px,solid rgba(255,255,255,0.5);
border-radius: 0.2s;
outline: none;
padding: 0 10px;
letter-spacing: 1px;
}
.box .input-box > input:focus {
border: 1px,solid rgba(255,255,255,0.8);
}
.box .btn-box {
width: 250px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: start;
}
.box .btn-box > a {
font-size: 13px;
text-decoration: none;
color: rgba(255,255,255,0.9);
transition: 0.2s;
width: 249px;
text-align: end;
}
.box .btn-box > a:hover {
justify-content: center;
align-items: start;
}
.box .btn-box > div >button {
width: 120px;
height: 35px;
border: 1px solid rgba(29,29,29,0.5);
background: rgba(29,29,29,0.5);
color: rgab(255,255,255,0.9);
color: rgba(255,255,255,1);
}
.box .btn-box > div {
display: flex;
flex-direction: row;
border-radius: 5px;
transition: 0.2s;
}
.box .btn-box > div >button :hover{
border: 1px solid rgba(29,29,29,0.5);
background: rgba(29,29,29,0.5);
}
.box .btn-box > div >button:nth-of-type(2){
margin-left: 5px;
}
登录界面如下:

二、 计算机功能实现
package cn.lidan.start;
import can.lidan.util.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=300;
public static final int FRAME_H=300;
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(Const.TITLE);//设置名称
this.setSize(Const.FRAME_W, Const.FRAME_H);//给窗体设置大小
this.setLayout(new BorderLayout());
this.setResizable(false);//设置窗体大小不可修改
this.setLocation(Const.FRAME_x, Const.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, 17));
btn.setForeground(Color.RED);
}
/*if(temp.equals("+")||temp.equals("-")||temp.equals("*")
||temp.equals("/")||temp.equals(".")||temp.equals("="))
{
bth.setFont(new Font("粗体",Font.BOLD,16));
bth.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);
}
}
package can.lidan.util;
import cn.lidan.start.caculator;
import java.awt.*;
public class Const {
public static final int FRAME_W=400;
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 static final int FRAME_x=(SCREEN_W-FRAME_W)/2;
public static final int FRAME_y=(SCREEN_H-FRAME_H)/2;
public static final String TITLE="计算器";
}
对软件进行测试,其测试截屏如下:


三、 连接数据库


代码如下:
package com.text.jdbc;
import java.sql.*;
public class jbdc {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection com = DriverManager.getConnection("jdbc:mysql://10.16.158.90:3306/db1", "root", "123456");
// DriverManager 注册驱动
// Connection 数据库连接对象 url(指定连接的路径 语法:“jdbc:mysql://ip地址:端口号/数据库名称”)
Statement stat = com.createStatement();
//执行 sql 语句的对象
String sql = "SELECT * FROM user";
ResultSet rs = stat.executeQuery(sql);
// 执行 增删改查 (DML)语句用 int executeUpdate(Sting sql);
// 执行 DQL 语句 ResultSet executeQuery(String sql);
// 对象释放 void close();
while (rs.next()){
System.out.println(rs.getInt("id") + " " + rs.getString("name"));
}
com.close();
stat.close();
com.close();
}
}

浙公网安备 33010602011771号